-
Notifications
You must be signed in to change notification settings - Fork 30
/
vaultbin.go
64 lines (51 loc) · 1.2 KB
/
vaultbin.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
package util
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
type VaultBinPaste struct {
ID string
}
func (v *VaultBinPaste) URL() string {
return fmt.Sprintf("https://vaultb.in/%s", v.ID)
}
type vaultBinRequest struct {
Content string `json:"content"`
Language string `json:"language"`
}
type vaultBinResponse struct {
Data vaultbinResponseData `json:"data"`
}
type vaultbinResponseData struct {
ID string `json:"id"`
}
func CreateVaultBinPaste(content string, language string) (*VaultBinPaste, error) {
reqBody, err := json.Marshal(vaultBinRequest{
Content: content,
Language: language,
})
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", "https://vaultb.in/api/pastes", bytes.NewReader(reqBody))
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if resp.StatusCode != 200 {
return nil, fmt.Errorf("vaultb.in returned status code %d", resp.StatusCode)
}
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var vaultBinResp vaultBinResponse
err = json.Unmarshal(respBody, &vaultBinResp)
if err != nil {
return nil, err
}
return &VaultBinPaste{
ID: vaultBinResp.Data.ID,
}, nil
}