-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
141 lines (114 loc) · 3.3 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package spn2
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"time"
)
const baseURL = "https://web.archive.org/save"
// Client represents a client to interact with the SPN2 API.
type Client struct {
httpClient *http.Client
AccessKey string
SecretKey string
}
// NewClient creates a new SPN2 API client.
func NewClient(accessKey, secretKey string) *Client {
return &Client{
httpClient: &http.Client{Timeout: 30 * time.Second},
AccessKey: accessKey,
SecretKey: secretKey,
}
}
// SubmitURL submits a new URL to the SPN2 API for capturing.
func (c *Client) SubmitURL(url string) (*CaptureResponse, error) {
reqBody := bytes.NewBufferString("url=" + url)
req, err := http.NewRequest("POST", baseURL, reqBody)
if err != nil {
return nil, err
}
c.addAuthHeaders(req)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Accept", "application/json")
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var captureResp CaptureResponse
if err := json.NewDecoder(resp.Body).Decode(&captureResp); err != nil {
return nil, err
}
return &captureResp, nil
}
// GetStatus retrieves the status of a capture using the provided Job ID.
func (c *Client) GetStatus(jobID string) (*StatusResponse, error) {
reqURL := fmt.Sprintf("%s/status/%s", baseURL, jobID)
req, err := http.NewRequest("GET", reqURL, nil)
if err != nil {
return nil, err
}
c.addAuthHeaders(req)
req.Header.Set("Accept", "application/json")
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var statusResp StatusResponse
if err := json.NewDecoder(resp.Body).Decode(&statusResp); err != nil {
return nil, err
}
return &statusResp, nil
}
// GetSystemStatus checks the system status of the SPN2 API.
func (c *Client) GetSystemStatus() (*SystemStatusResponse, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("%s/status/system", baseURL), nil)
if err != nil {
return nil, err
}
c.addAuthHeaders(req)
req.Header.Set("Accept", "application/json")
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, errors.New("failed to get system status")
}
var systemStatus SystemStatusResponse
if err := json.NewDecoder(resp.Body).Decode(&systemStatus); err != nil {
return nil, err
}
return &systemStatus, nil
}
// GetUserStatus checks the user's account status in the SPN2 API.
func (c *Client) GetUserStatus() (*UserStatusResponse, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("%s/status/user", baseURL), nil)
if err != nil {
return nil, err
}
c.addAuthHeaders(req)
req.Header.Set("Accept", "application/json")
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, errors.New("failed to get user status")
}
var userStatus UserStatusResponse
if err := json.NewDecoder(resp.Body).Decode(&userStatus); err != nil {
return nil, err
}
return &userStatus, nil
}
// addAuthHeaders adds the necessary authentication headers to the request.
func (c *Client) addAuthHeaders(req *http.Request) {
authValue := fmt.Sprintf("LOW %s:%s", c.AccessKey, c.SecretKey)
req.Header.Set("Authorization", authValue)
}