Skip to content

Instantly share code, notes, and snippets.

@harsh-98
Created May 13, 2022 18:39
Show Gist options
  • Save harsh-98/6c7476f3928e76058b5726440279f84c to your computer and use it in GitHub Desktop.
Save harsh-98/6c7476f3928e76058b5726440279f84c to your computer and use it in GitHub Desktop.
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"github.com/Khan/genqlient/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() {
var err error
defer func() {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}()
key := os.Getenv("GITHUB_TOKEN")
if key == "" {
err = fmt.Errorf("must set GITHUB_TOKEN=<github token>")
return
}
httpClient := http.Client{
Transport: &authedTransport{
key: key,
wrapped: http.DefaultTransport,
},
}
graphqlClient := graphql.NewClient("https://api.github.com/graphql", &httpClient)
switch len(os.Args) {
case 1:
var viewerResp *mutResponse
id := "IALE_kwHOBkiUV84ACmlp"
ip := "104.161.246.80"
isActive := true
viewerResp, err = mut(context.Background(), graphqlClient, id, ip, isActive)
if err != nil {
return
}
bytess, err := json.Marshal(viewerResp)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(json.Marshal(bytess))
default:
err = fmt.Errorf("usage: %v [username]", os.Args[0])
}
}
//go:generate go run github.com/Khan/genqlient genqlient.yaml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment