wire auth to example, fix wire format, it's aliiiiiive!
This commit is contained in:
+36
-5
@@ -3,17 +3,48 @@ package example
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/Khan/genql/graphql"
|
"github.com/Khan/genql/graphql"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type authedTransport struct {
|
||||||
|
key string
|
||||||
|
wrapped http.RoundTripper
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *authedTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||||
|
req.Header.Set("Authorization", "bearer "+t.key)
|
||||||
|
return t.wrapped.RoundTrip(req)
|
||||||
|
}
|
||||||
|
|
||||||
func Main() {
|
func Main() {
|
||||||
client := graphql.NewClient("https://api.github.com/graphql", nil)
|
var err error
|
||||||
resp, err := getViewer(context.Background(), client)
|
defer func() {
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
key := os.Getenv("KEY")
|
||||||
|
if key == "" {
|
||||||
|
err = fmt.Errorf("must set KEY=<github token>")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
httpClient := http.Client{
|
||||||
|
Transport: &authedTransport{
|
||||||
|
key: key,
|
||||||
|
wrapped: http.DefaultTransport,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
graphqlClient := graphql.NewClient("https://api.github.com/graphql", &httpClient)
|
||||||
|
resp, err := getViewer(context.Background(), graphqlClient)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
return
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
fmt.Println("you are:", resp.Viewer.Name)
|
|
||||||
|
fmt.Println("you are:", *resp.Viewer.Name)
|
||||||
}
|
}
|
||||||
|
|||||||
+16
-3
@@ -1,12 +1,12 @@
|
|||||||
package graphql
|
package graphql
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/vektah/gqlparser/gqlerror"
|
"github.com/vektah/gqlparser/gqlerror"
|
||||||
)
|
)
|
||||||
@@ -24,11 +24,24 @@ func NewClient(endpoint string, httpClient *http.Client) *Client {
|
|||||||
return &Client{endpoint, http.MethodPost, httpClient}
|
return &Client{endpoint, http.MethodPost, httpClient}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type payload struct {
|
||||||
|
Query string `json:"query"`
|
||||||
|
Variables map[string]string `json:"variables"`
|
||||||
|
}
|
||||||
|
|
||||||
func (client *Client) MakeRequest(ctx context.Context, query string, retval interface{}) error {
|
func (client *Client) MakeRequest(ctx context.Context, query string, retval interface{}) error {
|
||||||
|
body, err := json.Marshal(payload{
|
||||||
|
Query: query,
|
||||||
|
Variables: nil, // TODO
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
req, err := http.NewRequest(
|
req, err := http.NewRequest(
|
||||||
client.method,
|
client.method,
|
||||||
client.endpoint,
|
client.endpoint,
|
||||||
strings.NewReader(query))
|
bytes.NewReader(body))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -40,7 +53,7 @@ func (client *Client) MakeRequest(ctx context.Context, query string, retval inte
|
|||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
body, err = ioutil.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user