-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathep.go
68 lines (58 loc) · 1.4 KB
/
ep.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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
)
const EP_URL = "https://evepraisal.com/appraisal.json"
type EvePraisal struct {
Appraisal struct {
Totals struct {
Buy float64
Sell float64
Volume float64
}
}
}
func (esi *ESI) getEvePraisal(items []ContractItems) (EvePraisal, string, error) {
ep := EvePraisal{}
url := paramEncode(items)
client := http.Client{}
request := http.Request{
Method: "POST",
URL: url,
Header: make(http.Header),
}
request.Header.Set("User-Agent", "Outfit 418 contracts appraiser, [email protected]")
resp, err := client.Do(&request)
if err != nil {
panic(err)
}
defer resp.Body.Close()
epId := resp.Header.Get("x-appraisal-id")
if epId == "" {
return ep, "", fmt.Errorf("no ep id found in header")
}
epLink := fmt.Sprintf("https://evepraisal.com/a/%s", epId)
json.NewDecoder(resp.Body).Decode(&ep)
return ep, epLink, nil
}
func paramEncode(items []ContractItems) *url.URL {
var stringItems string
for _, item := range items {
stringItems += fmt.Sprintf("%s %v\n", item.Name, item.Quantity)
}
baseUrl, err := url.Parse(EP_URL)
if err != nil {
log.Println("Malformed URL: ", err.Error())
return nil
}
queryParams := url.Values{}
queryParams.Add("raw_textarea", stringItems)
queryParams.Add("market", "jita")
queryParams.Add("persist", "yes")
baseUrl.RawQuery = queryParams.Encode()
return baseUrl
}