This repository has been archived by the owner on Mar 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello.go
89 lines (75 loc) · 2.34 KB
/
hello.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
84
85
86
87
88
89
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strconv"
"github.com/sphereio/jsonq"
)
type Credentials struct {
ClientId string `json:"client_id"`
ClientSecret string `json:"client_secret"`
ProjectKey string `json:"project_key"`
}
type Auth struct {
AccessToken string `json:"access_token"`
}
func parseJson(path string) *Credentials {
file, e := ioutil.ReadFile(path)
if e != nil {
log.Fatal("Error while reading config file: ", e)
}
var c Credentials
json.Unmarshal(file, &c)
return &c
}
func getAccessToken(c *Credentials) *Auth {
authUrl := fmt.Sprintf("https://%v:%[email protected]/oauth/token", c.ClientId, c.ClientSecret)
data := url.Values{}
data.Set("grant_type", "client_credentials")
data.Set("scope", fmt.Sprintf("manage_project:%v", c.ProjectKey))
client := &http.Client{}
req, _ := http.NewRequest("POST", authUrl, bytes.NewBufferString(data.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
resp, err := client.Do(req)
if err != nil {
log.Fatal("Error when retrieving access_token: ", err)
}
defer resp.Body.Close()
var a Auth
json.NewDecoder(resp.Body).Decode(&a)
return &a
}
func getProductProjections(access_token string, project string) *jsonq.JsonQuery {
apiUrl := fmt.Sprintf("https://api.sphere.io/%v/product-projections", project)
client := &http.Client{}
req, _ := http.NewRequest("GET", apiUrl, nil)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", fmt.Sprintf("Bearer %v", access_token))
resp, err := client.Do(req)
if err != nil {
log.Fatal("Error when fetching products: ", err)
}
defer resp.Body.Close()
// we now use a json parsing library to decode arbitrary data
// in order to avoid declaring structs for the entire products response
data := map[string]interface{}{}
json.NewDecoder(resp.Body).Decode(&data)
jq := jsonq.NewQuery(data)
return jq
}
func main() {
// read credentials
c := parseJson("./config.json")
// get an access_token
auth := getAccessToken(c)
// get product projections
products := getProductProjections(auth.AccessToken, c.ProjectKey)
count, _ := products.Int("count")
fmt.Println(fmt.Sprintf("Found %v products", count))
}