This repository has been archived by the owner on Apr 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapi_cardconduit.go
82 lines (69 loc) · 1.73 KB
/
api_cardconduit.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
package main
import (
"bytes"
"encoding/json"
"errors"
"io"
"net/http"
"time"
cleanhttp "github.com/hashicorp/go-cleanhttp"
)
const (
CCEstimateURL = "https://cardconduit.com/api/v1.0/estimate"
)
type CCItem struct {
ScryfallID string `json:"scryfall_id"`
Condition string `json:"condition,omitempty"`
Quantity int `json:"quantity,omitempty"`
Language string `json:"language,omitempty"`
IsFoil bool `json:"is_foil,omitempty"`
IsEtched bool `json:"is_etched,omitempty"`
}
type CCPayload struct {
Items []CCItem `json:"items"`
}
type CCResponse struct {
Success bool `json:"success"`
Message string `json:"message"`
HTTPCode int `json:"http_code"`
Data struct {
Estimate struct {
ID string `json:"id"`
URL string `json:"url"`
CreatedAt time.Time `json:"created_at"`
} `json:"estimate"`
} `json:"data"`
}
func sendCardConduitEstimate(items []CCItem) (string, error) {
var payload CCPayload
payload.Items = items
reqBytes, err := json.Marshal(&payload)
if err != nil {
return "", err
}
req, err := http.NewRequest(http.MethodPost, CCEstimateURL, bytes.NewReader(reqBytes))
if err != nil {
return "", err
}
req.Header.Set("Authorization", "Bearer "+Config.Api["cardconduit"])
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
resp, err := cleanhttp.DefaultClient().Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
var response CCResponse
err = json.Unmarshal(data, &response)
if err != nil {
return "", err
}
if !response.Success {
return "", errors.New(response.Message)
}
return response.Data.Estimate.URL, nil
}