-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
69 lines (57 loc) · 1.57 KB
/
client.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
package golamap
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strings"
)
type OLAMap struct {
Token string // Ola map token
RequestId string // Unique UUID for a request
HttpService HttpServ // HTTP service interface
}
type HttpServ interface {
SendOlaMapRequest(method, url, requestID, oauthToken string, responseObj interface{}) error
}
type TokenResponse struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
}
// Initialize the Olamap with X-RequestID
func Initialize(requestID string) *OLAMap {
httpReq := &OlaRequest{}
return &OLAMap{
RequestId: requestID,
HttpService: httpReq,
}
}
// Configure OLA access token
func (o *OLAMap) ConfigureAccessToken(clientID, clientSecret string) error {
form := url.Values{}
form.Set("grant_type", "client_credentials")
form.Set("scope", "openid")
form.Set("client_id", clientID)
form.Set("client_secret", clientSecret)
req, err := http.NewRequest("POST", TokenURL, strings.NewReader(form.Encode()))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return errors.New(fmt.Sprintf("Failed to get token - statuscode %v", resp.StatusCode))
}
var tokenResponse TokenResponse
if err := json.NewDecoder(resp.Body).Decode(&tokenResponse); err != nil {
return err
}
o.Token = "Bearer " + tokenResponse.AccessToken
return nil
}