forked from 99designs/gqlgen
-
Notifications
You must be signed in to change notification settings - Fork 10
/
options.go
63 lines (52 loc) · 1.58 KB
/
options.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package client
import (
"encoding/base64"
"github.com/valyala/fasthttp"
)
// Var adds a variable into the outgoing request
func Var(name string, value interface{}) Option {
return func(bd *Request) {
if bd.Variables == nil {
bd.Variables = map[string]interface{}{}
}
bd.Variables[name] = value
}
}
// Operation sets the operation name for the outgoing request
func Operation(name string) Option {
return func(bd *Request) {
bd.OperationName = name
}
}
// Path sets the url that this request will be made against, useful if you are mounting your entire router
// and need to specify the url to the graphql endpoint.
func Path(url string) Option {
return func(bd *Request) {
bd.HTTP.SetRequestURI(url)
}
}
// AddHeader adds a header to the outgoing request. This is useful for setting expected Authentication headers for example.
func AddHeader(key string, value string) Option {
return func(bd *Request) {
bd.HTTP.Header.Add(key, value)
}
}
// BasicAuth authenticates the request using http basic auth.
func BasicAuth(username, password string) Option {
return func(bd *Request) {
SetBasicAuth(bd.HTTP, username, password)
}
}
// AddCookie adds a cookie to the outgoing request
func AddCookie(key, value string) Option {
return func(bd *Request) {
bd.HTTP.Header.SetCookie(key, value)
}
}
func SetBasicAuth(req *fasthttp.Request, username, password string) {
req.Header.Set("Authorization", "Basic "+basicAuth(username, password))
}
func basicAuth(username, password string) string {
auth := username + ":" + password
return base64.StdEncoding.EncodeToString([]byte(auth))
}