This repository has been archived by the owner on Mar 24, 2023. It is now read-only.
/
characters.go
170 lines (125 loc) · 4.53 KB
/
characters.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package esi
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strconv"
"github.com/eveisesi/athena"
)
type characterInterface interface {
GetCharacter(ctx context.Context, characterID uint) (*athena.Character, *athena.Etag, *http.Response, error)
GetCharacterCorporationHistory(ctx context.Context, characterID uint) ([]*athena.CharacterCorporationHistory, *athena.Etag, *http.Response, error)
}
func isCharacterValid(r *athena.Character) bool {
if r.Name == "" || r.CorporationID == 0 {
return false
}
return true
}
// GetCharacter makes a HTTP GET Request to the /characters/{character_id} endpoint
// for information about the provided character
//
// Documentation: https://esi.evetech.net/ui/#/Character/get_characters_character_id
// Version: v4
// Cache: 86400 sec (24 Hour)
func (s *service) GetCharacter(ctx context.Context, characterID uint) (*athena.Character, *athena.Etag, *http.Response, error) {
endpoint := endpoints[GetCharacter]
mods := s.modifiers(ModWithCharacterID(characterID))
etag, err := s.etag.Etag(ctx, endpoint.KeyFunc(mods))
if err != nil {
return nil, nil, nil, err
}
path := endpoint.PathFunc(mods)
b, res, err := s.request(
ctx,
WithMethod(http.MethodGet),
WithPath(path),
WithEtag(etag.Etag),
)
if err != nil {
return nil, nil, nil, err
}
if res.StatusCode >= http.StatusBadRequest {
return nil, etag, res, fmt.Errorf("failed to fetch character %d, received status code of %d", characterID, res.StatusCode)
}
etag.Etag = RetrieveEtagHeader(res.Header)
etag.CachedUntil = RetrieveExpiresHeader(res.Header, 0)
_, err = s.etag.UpdateEtag(ctx, etag.EtagID, etag)
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to update etag: %w", err)
}
if res.StatusCode == http.StatusNotModified {
return nil, etag, res, nil
}
var character = new(athena.Character)
err = json.Unmarshal(b, character)
if err != nil {
err = fmt.Errorf("unable to unmarshal response body on request %s: %w", path, err)
return nil, nil, nil, err
}
character.ID = characterID
if !isCharacterValid(character) {
return nil, nil, nil, fmt.Errorf("invalid character return from esi, missing name or ticker")
}
return character, etag, res, nil
}
func characterKeyFunc(mods *modifiers) string {
requireCharacterID(mods)
return buildKey(GetCharacter.String(), strconv.Itoa(int(mods.characterID)))
}
func characterPathFunc(mods *modifiers) string {
requireCharacterID(mods)
return fmt.Sprintf(endpoints[GetCharacter].Path, mods.characterID)
}
// GetCharacterCorporationHistory makes a HTTP GET Request to the /v1/characters/{character_id}/corporationhistory/ endpoint
// for information about the provided characters corporation history
//
// Documentation: https://esi.evetech.net/ui/?version=_latest#/Character/get_characters_character_id_corporationhistory
// Version: v1
// Cache: 86400 sec (24 Hour)
func (s *service) GetCharacterCorporationHistory(ctx context.Context, characterID uint) ([]*athena.CharacterCorporationHistory, *athena.Etag, *http.Response, error) {
endpoint := endpoints[GetCharacterCorporationHistory]
mods := s.modifiers(ModWithCharacterID(characterID))
etag, err := s.etag.Etag(ctx, endpoint.KeyFunc(mods))
if err != nil {
return nil, nil, nil, err
}
path := endpoint.PathFunc(mods)
b, res, err := s.request(
ctx,
WithMethod(http.MethodGet),
WithPath(path),
WithEtag(etag.Etag),
)
if err != nil {
return nil, nil, nil, err
}
if res.StatusCode >= http.StatusBadRequest {
return nil, etag, res, fmt.Errorf("failed to fetch corporation history for character %d, received status code of %d", characterID, res.StatusCode)
}
etag.Etag = RetrieveEtagHeader(res.Header)
etag.CachedUntil = RetrieveExpiresHeader(res.Header, 0)
_, err = s.etag.UpdateEtag(ctx, etag.EtagID, etag)
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to update etag: %w", err)
}
if res.StatusCode == http.StatusNotModified {
return nil, etag, res, nil
}
var history = make([]*athena.CharacterCorporationHistory, 0, 512)
err = json.Unmarshal(b, &history)
if err != nil {
err = fmt.Errorf("unable to unmarshal response body on request %s: %w", path, err)
return nil, nil, nil, err
}
return history, etag, res, nil
}
func characterCorporationHistoryKeyFunc(mods *modifiers) string {
requireCharacterID(mods)
return buildKey(GetCharacterCorporationHistory.String(), strconv.Itoa(int(mods.characterID)))
}
func characterCorporationHistoryPathFunc(mods *modifiers) string {
requireCharacterID(mods)
return fmt.Sprintf(endpoints[GetCharacterCorporationHistory].Path, mods.characterID)
}