-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create parity_rpc client that replace
input
-> data
field
- Loading branch information
Showing
2 changed files
with
41 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package parity_rpc | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/ethereum/go-ethereum/rpc" | ||
) | ||
|
||
// DialHTTP creates a new RPC client that connects to an RPC server over HTTP with default rate limiter options. | ||
func DialHTTP(endpoint string) (*rpc.Client, error) { | ||
httpClient := new(http.Client) | ||
httpClient.Transport = &ReplaceInputToDataTransporter{roundTripperWrap: http.DefaultTransport} | ||
return rpc.DialHTTPWithClient(endpoint, httpClient) | ||
} | ||
|
||
// DialHTTPWithLimitOptions creates a new RPC client that connects to an RPC server over HTTP with specified rate limiter options. | ||
func DialHTTPWithLimitOptions(endpoint string, limitPeriod time.Duration, requestCount int) (*rpc.Client, error) { | ||
httpClient := new(http.Client) | ||
httpClient.Transport = &ReplaceInputToDataTransporter{roundTripperWrap: http.DefaultTransport} | ||
return rpc.DialHTTPWithClient(endpoint, httpClient) | ||
} | ||
|
||
type ReplaceInputToDataTransporter struct { | ||
roundTripperWrap http.RoundTripper | ||
} | ||
|
||
func (c *ReplaceInputToDataTransporter) RoundTrip(r *http.Request) (*http.Response, error) { | ||
bodyBytes, err := io.ReadAll(r.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
bodyBytes = bytes.Replace(bodyBytes, []byte(`"input"`), []byte(`"data"`), -1) | ||
r.Body = io.NopCloser(bytes.NewReader(bodyBytes)) | ||
r.ContentLength = int64(len(bodyBytes)) | ||
return c.roundTripperWrap.RoundTrip(r) | ||
} |