Skip to content

Commit eb36e57

Browse files
author
Jon Bernard
committed
Allow for headers to be sent with HTTPRequest and provide headers back with HTTPResponse
1 parent 1943400 commit eb36e57

File tree

5 files changed

+89
-42
lines changed

5 files changed

+89
-42
lines changed

ldk/go/examples/network/loop/loop.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package loop
33
import (
44
"context"
55
"encoding/json"
6+
"net/http"
67
"time"
78

89
ldk "github.com/open-olive/loop-development-kit/ldk/go"
@@ -73,15 +74,26 @@ func (c *Loop) LoopStart(sidekick ldk.Sidekick) error {
7374
URL: "https://api.fda.gov/food/enforcement.json?search=report_date:[" + now.AddDate(0, -3, 0).Format("20060102") + "+TO+" + now.Format("20060102") + "]&limit=1",
7475
Method: "GET",
7576
Body: nil,
77+
Headers: map[string]string{
78+
"A-Header-Name": "a header value",
79+
},
7680
})
7781
if err != nil {
7882
c.logger.Error("received error from callback", err)
7983
return err
8084
}
8185

82-
var data apiResponse
83-
8486
if response.ResponseCode == 200 {
87+
var headers http.Header
88+
var data apiResponse
89+
90+
if err := json.Unmarshal(response.Headers, &headers); err != nil {
91+
c.logger.Error("Error unmarshaling response headers", err)
92+
return err
93+
}
94+
95+
c.logger.Info("Response headers", headers)
96+
8597
if err := json.Unmarshal(response.Data, &data); err != nil {
8698
c.logger.Error("Error unmarshaling response payload", err)
8799
return err

ldk/go/networkClient.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ type NetworkClient struct {
1616
type HTTPResponse struct {
1717
ResponseCode int
1818
Data []byte
19+
Headers []byte
1920
}
2021

2122
// HTTPRequest is the structure received from HttpRequest
2223
type HTTPRequest struct {
23-
URL string
24-
Method string
25-
Body []byte
24+
URL string
25+
Method string
26+
Body []byte
27+
Headers map[string]string
2628
}
2729

2830
func (n *NetworkClient) HTTPRequest(ctx context.Context, req *HTTPRequest) (*HTTPResponse, error) {
@@ -31,6 +33,7 @@ func (n *NetworkClient) HTTPRequest(ctx context.Context, req *HTTPRequest) (*HTT
3133
Url: req.URL,
3234
Method: req.Method,
3335
Body: req.Body,
36+
Headers: req.Headers,
3437
})
3538

3639
if err != nil {
@@ -40,5 +43,6 @@ func (n *NetworkClient) HTTPRequest(ctx context.Context, req *HTTPRequest) (*HTT
4043
return &HTTPResponse{
4144
ResponseCode: int(resp.ResponseCode),
4245
Data: resp.Data,
46+
Headers: resp.Headers,
4347
}, err
4448
}

ldk/go/networkServer.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ func (ns *NetworkServer) HTTPRequest(ctx context.Context, req *proto.HTTPRequest
1919
resp, err := ns.Impl.HTTPRequest(
2020
context.WithValue(ctx, Session{}, session),
2121
&HTTPRequest{
22-
URL: req.Url,
23-
Method: req.Method,
24-
Body: req.Body,
22+
URL: req.Url,
23+
Method: req.Method,
24+
Body: req.Body,
25+
Headers: req.Headers,
2526
},
2627
)
2728
if err != nil {
@@ -31,6 +32,7 @@ func (ns *NetworkServer) HTTPRequest(ctx context.Context, req *proto.HTTPRequest
3132
return &proto.HTTPResponseMsg{
3233
ResponseCode: uint32(resp.ResponseCode),
3334
Data: resp.Data,
35+
Headers: resp.Headers,
3436
}, nil
3537

3638
}

ldk/go/proto/network.pb.go

Lines changed: 61 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proto/network.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ message HTTPRequestMsg {
1313
string url = 2;
1414
string method = 3;
1515
bytes body = 4;
16+
map <string, string> headers = 5;
1617
}
1718

1819
message HTTPResponseMsg {
1920
uint32 responseCode = 1;
2021
bytes data = 2;
22+
bytes headers = 3;
2123
}

0 commit comments

Comments
 (0)