Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 871c779

Browse files
authored
Make NodeHTTPAPIClient compatible with chronicle (#317)
1 parent 25ba8d2 commit 871c779

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

node_http_api_client.go

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import (
77
"encoding/json"
88
"errors"
99
"fmt"
10-
"github.com/iotaledger/hive.go/serializer"
1110
"io"
1211
"io/ioutil"
1312
"net/http"
1413
"net/url"
1514
"strconv"
1615
"strings"
16+
17+
"github.com/iotaledger/hive.go/serializer"
1718
)
1819

1920
var (
@@ -57,6 +58,10 @@ const (
5758
// GET returns the tips.
5859
NodeAPIRouteTips = "/api/v1/tips"
5960

61+
// NodeAPIRouteMessageData is the route for getting message data by its messageID.
62+
// GET returns message data (json).
63+
NodeAPIRouteMessageData = "/api/v1/messages/%s"
64+
6065
// NodeAPIRouteMessageMetadata is the route for getting message metadata by its messageID.
6166
// GET returns message metadata (including info about "promotion/reattachment needed").
6267
NodeAPIRouteMessageMetadata = "/api/v1/messages/%s/metadata"
@@ -127,10 +132,14 @@ const (
127132
NodeAPIRoutePeers = "/api/v1/peers"
128133
)
129134

135+
// RequestURLHook is a function to modify the URL before sending a request.
136+
type RequestURLHook func(url string) string
137+
130138
// the default options applied to the NodeHTTPAPIClient.
131139
var defaultNodeAPIOptions = []NodeHTTPAPIClientOption{
132140
WithNodeHTTPAPIClientHTTPClient(http.DefaultClient),
133141
WithNodeHTTPAPIClientUserInfo(nil),
142+
WithNodeHTTPAPIClientRequestURLHook(nil),
134143
}
135144

136145
// NodeHTTPAPIClientOptions define options for the NodeHTTPAPIClient.
@@ -139,6 +148,8 @@ type NodeHTTPAPIClientOptions struct {
139148
httpClient *http.Client
140149
// The username and password information.
141150
userInfo *url.Userinfo
151+
// The hook to modify the URL before sending a request.
152+
requestURLHook RequestURLHook
142153
}
143154

144155
// applies the given NodeHTTPAPIClientOption.
@@ -162,6 +173,13 @@ func WithNodeHTTPAPIClientUserInfo(userInfo *url.Userinfo) NodeHTTPAPIClientOpti
162173
}
163174
}
164175

176+
// WithNodeHTTPAPIClientRequestURLHook is used to modify the URL before sending a request.
177+
func WithNodeHTTPAPIClientRequestURLHook(requestURLHook RequestURLHook) NodeHTTPAPIClientOption {
178+
return func(opts *NodeHTTPAPIClientOptions) {
179+
opts.requestURLHook = requestURLHook
180+
}
181+
}
182+
165183
// NodeHTTPAPIClientOption is a function setting a NodeHTTPAPIClient option.
166184
type NodeHTTPAPIClientOption func(opts *NodeHTTPAPIClientOptions)
167185

@@ -278,8 +296,14 @@ func (api *NodeHTTPAPIClient) Do(ctx context.Context, method string, route strin
278296
}
279297
}
280298

299+
// construct request URL
300+
url := fmt.Sprintf("%s%s", api.BaseURL, route)
301+
if api.opts.requestURLHook != nil {
302+
url = api.opts.requestURLHook(url)
303+
}
304+
281305
// construct request
282-
req, err := http.NewRequestWithContext(ctx, method, fmt.Sprintf("%s%s", api.BaseURL, route), func() io.Reader {
306+
req, err := http.NewRequestWithContext(ctx, method, url, func() io.Reader {
283307
if data == nil {
284308
return nil
285309
}
@@ -492,7 +516,19 @@ func (api *NodeHTTPAPIClient) MessageMetadataByMessageID(ctx context.Context, ms
492516
return res, nil
493517
}
494518

495-
// MessageByMessageID get a message by its message ID from the node.
519+
// MessageJSONByMessageID get a message by its message ID from the node (json).
520+
func (api *NodeHTTPAPIClient) MessageJSONByMessageID(ctx context.Context, msgID MessageID) (*Message, error) {
521+
query := fmt.Sprintf(NodeAPIRouteMessageData, hex.EncodeToString(msgID[:]))
522+
523+
res := &Message{}
524+
_, err := api.Do(ctx, http.MethodGet, query, nil, res)
525+
if err != nil {
526+
return nil, err
527+
}
528+
return res, nil
529+
}
530+
531+
// MessageByMessageID get a message by its message ID from the node (bytes).
496532
func (api *NodeHTTPAPIClient) MessageByMessageID(ctx context.Context, msgID MessageID) (*Message, error) {
497533
query := fmt.Sprintf(NodeAPIRouteMessageBytes, hex.EncodeToString(msgID[:]))
498534

0 commit comments

Comments
 (0)