-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
121 lines (100 loc) · 2.97 KB
/
request.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
package howlongtobeat
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"github.com/corpix/uarand"
)
type SearchRequest struct {
SearchType SearchType `json:"searchType,required"`
SearchTerms []string `json:"searchTerms,required"`
SearchPage int `json:"searchPage,required"`
PageSize int `json:"size,required"`
SearchOptions searchOptions `json:"searchOptions,required"`
}
func NewSearchRequest(searchTerms string) (SearchRequest, error) {
request := SearchRequest{}
request.setDefaults()
request.SetSearchTerms(searchTerms)
return request, nil
}
func (s *SearchRequest) SetSearchTerms(terms string) {
result := strings.Split(terms, " ")
s.SearchTerms = result
}
func (s *SearchRequest) SetPlatform(newP Platform) {
s.SearchOptions.Games.Platform = newP
}
func (s *SearchRequest) SetPagination(pageIndex, pageSize int) {
s.SearchPage = pageIndex
s.PageSize = pageSize
}
func (s *SearchRequest) SetModifier(modifier Modifier) {
s.SearchOptions.Games.Modifier = modifier
}
func (s *SearchRequest) SetSorting(sort SortBy) {
s.SearchOptions.Games.SortCategory = sort
}
func (s *SearchRequest) SetGameplay(pers Perspective, flow Flow, genre Genre) {
s.SearchOptions.Games.Gameplay.Perspective = pers
s.SearchOptions.Games.Gameplay.Flow = flow
s.SearchOptions.Games.Gameplay.Genre = genre
}
func (s *SearchRequest) setDefaults() {
s.SearchType = SearchTypeGames
s.SearchPage = 1
s.PageSize = 20
s.SearchOptions.Games = searchOptionsGames{
UserId: 0,
Platform: PlatformAll,
SortCategory: SortByMostPopular,
RangeCategory: "main",
RangeTime: searchOptionsGamesRangeTime{},
Gameplay: searchOptionsGamesGameplay{
Perspective: PerspectiveAll,
Flow: FlowAll,
Genre: GenreAll,
},
Modifier: ModifierAll,
}
s.SearchOptions.Users = searchOptionsUsers{
SortCategory: "postcount",
}
s.SearchOptions.Filter = ""
s.SearchOptions.Sort = 0
s.SearchOptions.Randomizer = 0
}
func (s SearchRequest) send(searchId string) (SearchResult, error) {
url := fmt.Sprintf("https://howlongtobeat.com/api/search/%s", searchId)
result := SearchResult{}
httpClient := &http.Client{}
requestBody, err := json.Marshal(s)
if err != nil {
return result, fmt.Errorf("invalid body: %s", err)
}
req, _ := http.NewRequest("POST", url, strings.NewReader(string(requestBody)))
req.Header = map[string][]string{
"Content-Type": {"application/json"},
"User-Agent": {uarand.GetRandom()},
"Referer": {"https://howlongtobeat.com/"},
}
resp, err := httpClient.Do(req)
if err != nil {
return result, fmt.Errorf("error: %s", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return result, fmt.Errorf("can't read body: %s", err)
}
if resp.StatusCode != 200 {
return result, fmt.Errorf("request failed: %s", resp.Status)
}
err = json.Unmarshal(body, &result)
if err != nil {
return result, fmt.Errorf("invalid response body: %s", err)
}
return result, nil
}