/
graphql.go
83 lines (70 loc) · 1.82 KB
/
graphql.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"time"
)
const endpoint = "https://api.github.com/graphql"
var limit = wf.Config.Get("limit")
type SearchResult struct {
Data struct {
Search struct {
Nodes []struct {
Name string `json:"name"`
URL string `json:"url"`
} `json:"nodes"`
} `json:"search"`
} `json:"data"`
}
func search(q string) SearchResult {
var searchRepos = `{"query":"query{search(query:\"%s\",type:REPOSITORY,last:%s){nodes{...on Repository{name url}}}}"`
return query(fmt.Sprintf(searchRepos, q, limit))
}
// query makes a post request to GitHub's GraphQL endpoint
// for a given query, using a cached or new oauth token.
func query(q string) SearchResult {
// get cached token
tok, err := cachedToken()
if err != nil {
log.Printf("Error retrieving cached token; it might not exist: %v", err)
// get new token
tok, err = newToken()
if err != nil {
log.Fatalf("Error aquiring token: %v", err)
}
// store token
err = cacheToken(tok)
if err != nil {
log.Fatalf("Error storing token: %v", err)
}
}
// build request
req, err := http.NewRequest("POST", endpoint, bytes.NewBuffer([]byte(q)))
if err != nil {
log.Fatalf("Error building request: %v", err)
}
req.Header.Set("Authorization", "bearer "+tok.AccessToken)
// make request
var client = http.Client{Timeout: time.Second * 10}
resp, err := client.Do(req)
if err != nil {
log.Fatalf("Error reading response: %v", err)
}
if resp.StatusCode != 200 {
log.Fatalf("Response unsuccessful: %v", resp.Status)
}
defer resp.Body.Close()
// read response as bytes
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatalf("Error reading response body: %v", err)
}
// deserialize response to struct
var results SearchResult
json.Unmarshal(bodyBytes, &results)
return results
}