Skip to content

Commit

Permalink
Add the client
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienvermeille committed Sep 21, 2023
1 parent 472b3d7 commit df14727
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cirrusci_client.go → generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions genqlient.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
schema: resources/graphql/schema/cirrus-ci.schema.graphql
operations:
- resources/graphql/client/queries.graphql
generated: cirrusci_client.go
generated: generated.go
## needed since it doesn't match the directory name:
package: client
package: cirrus

# We bind github's DateTime scalar type to Go's time.Time (which conveniently
# already defines MarshalJSON and UnmarshalJSON). This means genqlient will
Expand Down
67 changes: 64 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,68 @@
package client
package cirrus

import "fmt"
import (
"context"
"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() {
fmt.Println("Hi, DEV World! 😉")
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.cirrus-ci.com/graphql", &httpClient)

switch len(os.Args) {
case 1:
var viewerResp *getViewerResponse
viewerResp, err = getViewer(context.Background(), graphqlClient)
if err != nil {
return
}
fmt.Println("you are", viewerResp.Viewer.Id)

//case 2:
// username := os.Args[1]
// var userResp *getUserResponse
// userResp, err = getUser(context.Background(), graphqlClient, username)
// if err != nil {
// return
// }
// fmt.Println(username, "is", userResp.User.TheirName, "created on", userResp.User.CreatedAt.Format("2006-01-02"))

default:
err = fmt.Errorf("usage: %v [username]", os.Args[0])
}
}

//go:generate go run github.com/Khan/genqlient genqlient.yaml

0 comments on commit df14727

Please sign in to comment.