-
Notifications
You must be signed in to change notification settings - Fork 0
/
idtapi.go
100 lines (77 loc) · 2.1 KB
/
idtapi.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
90
91
92
93
94
95
96
97
98
99
100
package idtapi
import (
"bytes"
"encoding/json"
"io"
"log"
"net/http"
"net/url"
"strings"
)
type Authentication struct {
Token string `json:"access_token"`
Expires int `json:"expires_in"`
Type string `json:"token_type"`
}
type Sequence struct {
Name string `json:"Name"`
Sequence string `json:"Sequence"`
}
type Problem struct {
ComplexityScore float64 `json:"Score"`
Name string `json:"Name"`
StartIndex int `json:"StartIndex"`
}
func GetComplexityScore(sequences []Sequence, username string, password string, clientId string, clientSecret string, urlPath string, urlToken string) [][]Problem {
auth := GetToken(username, password, clientId, clientSecret, urlToken)
requestByte, _ := json.Marshal(sequences)
req, _ := http.NewRequest("POST", urlPath, bytes.NewReader(requestByte))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", auth.Type+" "+auth.Token)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
var gBlockAnalyzed [][]Problem
if resp.Status == "200 OK" {
err := json.NewDecoder(resp.Body).Decode(&gBlockAnalyzed)
if err != nil {
panic(err)
}
return gBlockAnalyzed
}
return gBlockAnalyzed
}
func GetToken(username string, password string, clientId string, clientSecret string, urlToken string) Authentication {
data := url.Values{}
data.Set("grant_type", "password")
data.Set("username", username)
data.Set("password", password)
data.Set("scope", "test")
req, _ := http.NewRequest("POST", urlToken, strings.NewReader(data.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.SetBasicAuth(clientId, clientSecret)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
auth := Authentication{}
if resp.Status == "200 OK" {
err := json.NewDecoder(resp.Body).Decode(&auth)
if err != nil {
panic(err)
}
return auth
} else {
b, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
log.Fatalln(string(b))
}
return auth
}