Skip to content

Commit 22d430e

Browse files
authored
Merge pull request #10 from jkao1/feature/custom-request-headers
added header customizability to graphql.Request
2 parents c0950a2 + 0b1841d commit 22d430e

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ Low-level GraphQL client for Go.
99
* Use variables and upload files
1010
* Simple error handling
1111

12+
## Installation
13+
Make sure you have a working Go environment. To install graphql, simply run:
14+
15+
```
16+
$ go get github.com/machinebox/graphql
17+
```
18+
19+
## Usage
20+
1221
```go
1322
import "context"
1423

@@ -29,6 +38,9 @@ req := graphql.NewRequest(`
2938
// set any variables
3039
req.Var("key", "value")
3140

41+
// set header fields
42+
req.Header.Set("Cache-Control", "no-cache")
43+
3244
// define a Context for the request
3345
ctx := context.Background()
3446

graphql.go

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"io"
3939
"mime/multipart"
4040
"net/http"
41+
"net/textproto"
4142

4243
"github.com/pkg/errors"
4344
)
@@ -122,6 +123,12 @@ func (c *Client) Run(ctx context.Context, req *Request, resp interface{}) error
122123
}
123124
r.Header.Set("Content-Type", writer.FormDataContentType())
124125
r.Header.Set("Accept", "application/json")
126+
for key, values := range req.Header {
127+
for _, value := range values {
128+
r.Header.Add(key, value)
129+
}
130+
}
131+
c.logf(">> headers: %v", r.Header)
125132
r = r.WithContext(ctx)
126133
res, err := c.httpClient.Do(r)
127134
if err != nil {
@@ -174,12 +181,34 @@ type Request struct {
174181
q string
175182
vars map[string]interface{}
176183
files []file
184+
185+
// Header mirrors the Header of a http.Request. It contains
186+
// the request header fields either received
187+
// by the server or to be sent by the client.
188+
//
189+
// If a server received a request with header lines,
190+
//
191+
// Host: example.com
192+
// accept-encoding: gzip, deflate
193+
// Accept-Language: en-us
194+
// fOO: Bar
195+
// foo: two
196+
//
197+
// then
198+
//
199+
// Header = map[string][]string{
200+
// "Accept-Encoding": {"gzip, deflate"},
201+
// "Accept-Language": {"en-us"},
202+
// "Foo": {"Bar", "two"},
203+
// }
204+
Header Header
177205
}
178206

179207
// NewRequest makes a new Request with the specified string.
180208
func NewRequest(q string) *Request {
181209
req := &Request{
182-
q: q,
210+
q: q,
211+
Header: make(map[string][]string),
183212
}
184213
return req
185214
}
@@ -201,6 +230,37 @@ func (req *Request) File(fieldname, filename string, r io.Reader) {
201230
})
202231
}
203232

233+
// A Header represents the key-value pairs in an HTTP header.
234+
type Header map[string][]string
235+
236+
// Add adds the key, value pair to the header.
237+
// It appends to any existing values associated with key.
238+
func (h Header) Add(key, value string) {
239+
textproto.MIMEHeader(h).Add(key, value)
240+
}
241+
242+
// Set sets the header entries associated with key to
243+
// the single element value. It replaces any existing
244+
// values associated with key.
245+
func (h Header) Set(key, value string) {
246+
textproto.MIMEHeader(h).Set(key, value)
247+
}
248+
249+
// Get gets the first value associated with the given key.
250+
// It is case insensitive; textproto.CanonicalMIMEHeaderKey is used
251+
// to canonicalize the provided key.
252+
// If there are no values associated with the key, Get returns "".
253+
// To access multiple values of a key, or to use non-canonical keys,
254+
// access the map directly.
255+
func (h Header) Get(key string) string {
256+
return textproto.MIMEHeader(h).Get(key)
257+
}
258+
259+
// Del deletes the values associated with key.
260+
func (h Header) Del(key string) {
261+
textproto.MIMEHeader(h).Del(key)
262+
}
263+
204264
// file represents a file to upload.
205265
type file struct {
206266
Field string

0 commit comments

Comments
 (0)