-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoa_access_token.go
67 lines (54 loc) · 1.6 KB
/
moa_access_token.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
//********************************************************************************************************************//
//
// Copyright (C) 2018 - 2021 J&J Ideenschmiede GmbH <[email protected]>
//
// This file is part of goidealo.
// All code may be used. Feel free and maybe code something better.
//
// Author: Jonas Kwiedor (aka gowizzard)
//
//********************************************************************************************************************//
package goidealo
import "encoding/json"
// MoaAccessTokenReturn is to decode the json return
type MoaAccessTokenReturn struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
Scope string `json:"scope"`
ShopId int `json:"shop_id"`
}
// MoaAccessToken is to generate a bearer token for the moa api
func MoaAccessToken(r Request) (MoaAccessTokenReturn, error) {
// Config new request
c := Config{
MoaAccessToken: true,
Path: "/api/v2/oauth/token",
Method: "POST",
}
// Check sandbox
if r.Sandbox {
c.MoaAccessToken = false
c.MoaSandboxAccessToken = true
}
// Send new request
response, err := c.Send(r)
if err != nil {
return MoaAccessTokenReturn{}, err
}
// Close request body
defer response.Body.Close()
// Check response status
err = statusCodes(response.Status)
if err != nil {
return MoaAccessTokenReturn{}, err
}
// Decode data
var decode MoaAccessTokenReturn
err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return MoaAccessTokenReturn{}, err
}
// Return data
return decode, nil
}