-
Notifications
You must be signed in to change notification settings - Fork 14
/
request.go
49 lines (39 loc) · 885 Bytes
/
request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package restclient
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
)
type Request struct {
Method string
Path string
Body io.Reader
}
func NewRequest(method string, path string, body io.Reader) *Request {
req := new(Request)
req.Method = method
req.Path = path
req.Body = body
return req
}
func (req *Request) ToHTTPRequest(ctx context.Context, c *MassdriverClient) (*http.Request, error) {
url, err := url.Parse(c.baseURL)
if err != nil {
return nil, err
}
url.Path = req.Path
httpReq, err := http.NewRequestWithContext(ctx, req.Method, url.String(), req.Body)
if err != nil {
return nil, err
}
if c.apiKey != "" {
httpReq.Header.Set("X-Md-Api-Key", c.apiKey)
} else {
fmt.Println("Warning: API Key not specified")
}
// for now assuming everything is json
httpReq.Header.Set("Content-Type", "application/json")
return httpReq, nil
}