-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
71 lines (63 loc) · 1.75 KB
/
handlers.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
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/antihax/goesi"
)
// /raw
func (esi *ESI) handleContractsRequest(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
tokenSource, err := esi.tokenSource()
if err != nil {
log.Println("No token found, redirecting to login page")
http.Redirect(w, r, "http://localhost:4180/login", http.StatusFound)
return
}
ctx := context.WithValue(context.Background(), goesi.ContextOAuth2, tokenSource)
contracts, err := esi.GetRawContracts(ctx)
if err != nil {
http.Error(w, "Token Failure", http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(contracts)
}
// /parsed
func (esi *ESI) handleParsedContractsRequest(w http.ResponseWriter, r *http.Request) {
// Get token
tokenSource, err := esi.tokenSource()
if err != nil {
log.Println("No token found, redirecting to login page")
http.Redirect(w, r, "http://localhost:4180/login", http.StatusFound)
return
}
// Get raw contracts
ctx := context.WithValue(context.Background(), goesi.ContextOAuth2, tokenSource)
contracts, err := esi.GetRawContracts(ctx)
if err != nil {
http.Error(w, "Token Failure", http.StatusInternalServerError)
return
}
// Parse additional data
parsed := make([]Contract, 0)
for _, contract := range contracts {
if contract.Status != "outstanding" {
continue
}
parsed_contract, err := esi.ParseContract(contract, ctx)
if err != nil {
continue
}
parsed = append(parsed, parsed_contract)
}
// Return json
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(parsed)
}
// /sendit
func (esi *ESI) handleSendContracts(w http.ResponseWriter, r *http.Request) {
esi.CheckContracts()
fmt.Fprintf(w, "Sent")
}