-
Notifications
You must be signed in to change notification settings - Fork 0
/
archive.go
75 lines (62 loc) · 1.71 KB
/
archive.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
package archive
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"github.com/pkg/errors"
)
// SnapshotURL is the URL that the snapshot request is sent to.
const SnapshotURL = "https://pragma.archivelab.org"
// JSONError is an error returned by a JSON endpoint.
type JSONError struct {
Error string `json:"error"`
}
// SnapshotRequest is the format the snapshot API accepts.
type SnapshotRequest struct {
URL string `json:"url"`
}
// SnapshotResponse is the response from the snapshot API.
type SnapshotResponse struct {
AnnotationID string `json:"annotation_id"`
Domain string `json:"domain"`
ID int `json:"id"`
Path string `json:"path"`
Protocol string `json:"protocol"`
WaybackID string `json:"wayback_id"`
}
// Snapshot requests the internet archive to record the URL.
func Snapshot(url string) (SnapshotResponse, error) {
req := SnapshotRequest{
URL: url,
}
var resp SnapshotResponse
err := postJSON(SnapshotURL, req, &resp)
return resp, err
}
func postJSON(url string, req, resp interface{}) error {
reqJSON, err := json.Marshal(req)
if err != nil {
return err
}
post, err := http.NewRequest("POST", url, bytes.NewBuffer(reqJSON))
post.Header.Set("Content-Type", "application/json")
client := &http.Client{}
postResp, err := client.Do(post)
if err != nil {
return err
}
defer postResp.Body.Close()
if postResp.StatusCode != http.StatusOK {
return errors.Errorf("expected status OK: got %d", postResp.StatusCode)
}
body, _ := ioutil.ReadAll(postResp.Body)
var jsonErr JSONError
if err := json.Unmarshal(body, &jsonErr); err != nil {
return err
}
if len(jsonErr.Error) > 0 {
return errors.New(jsonErr.Error)
}
return json.Unmarshal(body, resp)
}