-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ciphersprint.go
65 lines (52 loc) · 1.05 KB
/
ciphersprint.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
package ciphersprint
import (
"encoding/json"
"io"
"net/http"
"net/url"
"github.com/syntaqx/ciphersprint/pkg/challenge"
)
const (
defaultBaseURL = "https://ciphersprint.pulley.com/"
)
type HTTPError struct {
Code int
Error string
}
type Client struct {
BaseURL *url.URL
httpClient *http.Client
}
func NewClient(httpClient *http.Client) *Client {
if httpClient == nil {
httpClient = &http.Client{}
}
c := &Client{httpClient: httpClient}
c.initialize()
return c
}
func (c *Client) initialize() {
if c.BaseURL == nil {
c.BaseURL, _ = url.Parse(defaultBaseURL)
}
}
func (c *Client) GetChallenge(url string) (*challenge.ChallengeResponse, error) {
u, err := c.BaseURL.Parse(url)
if err != nil {
return nil, err
}
resp, err := c.httpClient.Get(u.String())
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var challenge challenge.ChallengeResponse
if err := json.Unmarshal(body, &challenge); err != nil {
return nil, err
}
return &challenge, nil
}