Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

Commit

Permalink
v1.4.5-DEV: Refactored the http client
Browse files Browse the repository at this point in the history
- Do not send referrers if not wanted
- Do not err on HTTP status below 400
  • Loading branch information
wneessen committed Jun 9, 2021
1 parent 2cb71e5 commit 5343a4b
Show file tree
Hide file tree
Showing 11 changed files with 19 additions and 18 deletions.
2 changes: 1 addition & 1 deletion api/rt_traderoutes.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func GetTraderoutes(hc *http.Client) (Traderoutes, error) {
apiUrl := "https://maps.seaofthieves.rarethief.com/js/trade_routes.js"

l.Debugf("Fetching traderoutes from rarethief...")
httpResp, err := httpclient.HttpReqGet(apiUrl, hc, "", "")
httpResp, err := httpclient.HttpReqGet(apiUrl, hc, nil, nil, true)
if err != nil {
return Traderoutes{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion api/sot_achievement.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func GetLatestAchievement(hc *http.Client, rc string) (Achievement, error) {
apiUrl := "https://www.seaofthieves.com/api/profilev2/achievements"

l.Debugf("Fetching user achievements from API...")
httpResp, err := httpclient.HttpReqGet(apiUrl, hc, rc, "")
httpResp, err := httpclient.HttpReqGet(apiUrl, hc, &rc, nil, false)
if err != nil {
return Achievement{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion api/sot_balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func GetBalance(hc *http.Client, rc string) (UserBalance, error) {
apiUrl := "https://www.seaofthieves.com/api/profilev2/balance"

l.Debugf("Fetching balance from API...")
httpResp, err := httpclient.HttpReqGet(apiUrl, hc, rc, "")
httpResp, err := httpclient.HttpReqGet(apiUrl, hc, &rc, nil, false)
if err != nil {
return userBalance, err
}
Expand Down
3 changes: 2 additions & 1 deletion api/sot_dailydeed.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ func GetDailyDeed(hc *http.Client, rc string) (BountyList, error) {
apiUrl := "https://www.seaofthieves.com/event-hub"

l.Debugf("Fetching event-hub data from www.seaofthieves.com...")
httpResp, err := httpclient.HttpReqGet(apiUrl, hc, rc, "https://www.seaofthieves.com/season-two")
ref := "https://www.seaofthieves.com/season-two"
httpResp, err := httpclient.HttpReqGet(apiUrl, hc, &rc, &ref, false)
if err != nil {
return BountyList{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion api/sot_ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func GetFactionLedger(hc *http.Client, rc string, f string) (FactionLedger, erro

l.Debugf("Fetching user ledger position in %v faction from API...", f)
var userApiLedger ApiLedger
httpResp, err := httpclient.HttpReqGet(apiUrl, hc, rc, "")
httpResp, err := httpclient.HttpReqGet(apiUrl, hc, &rc, nil, false)
if err != nil {
return FactionLedger{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion api/sot_reputation.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func GetFactionReputation(hc *http.Client, rc string, f string) (FactionReputati

l.Debugf("Fetching user reputation in %v faction from API...", f)
var userReps Factions
httpResp, err := httpclient.HttpReqGet(apiUrl, hc, rc, "")
httpResp, err := httpclient.HttpReqGet(apiUrl, hc, &rc, nil, false)
if err != nil {
return FactionReputation{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion api/sot_season.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func GetSeasonProgress(hc *http.Client, rc string) (SeasonProgress, error) {

l.Debugf("Fetching user season progress from API...")
var userProgress SeasonProgress
httpResp, err := httpclient.HttpReqGet(apiUrl, hc, rc, "")
httpResp, err := httpclient.HttpReqGet(apiUrl, hc, &rc, nil, false)
if err != nil {
return userProgress, err
}
Expand Down
2 changes: 1 addition & 1 deletion api/sot_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func GetStats(hc *http.Client, rc string) (UserStats, error) {

l.Debugf("Fetching user season progress from API...")
var userOverview ApiOverview
httpResp, err := httpclient.HttpReqGet(apiUrl, hc, rc, "")
httpResp, err := httpclient.HttpReqGet(apiUrl, hc, &rc, nil, false)
if err != nil {
return UserStats{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion api/urbandict_term.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func GetUrbanDict(hc *http.Client, w string) (UrbanDictEntry, error) {
}

l.Debugf("Fetching UD definition fact from UD API...")
httpResp, err := httpclient.HttpReqGet(apiUrl, hc, "", "")
httpResp, err := httpclient.HttpReqGet(apiUrl, hc, nil, nil, true)
if err != nil {
return UrbanDictEntry{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion api/uselessfact_random.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func GetRandFact(hc *http.Client) (RandomFact, error) {
apiUrl := "https://uselessfacts.jsph.pl/random.json?language=en"

l.Debugf("Fetching random fact from API...")
httpResp, err := httpclient.HttpReqGet(apiUrl, hc, "", "")
httpResp, err := httpclient.HttpReqGet(apiUrl, hc, nil, nil, true)
if err != nil {
return randomFact, err
}
Expand Down
16 changes: 8 additions & 8 deletions httpclient/httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func NewHttpClient() (*http.Client, error) {
}

// Perform a GET request
func HttpReqGet(u string, hc *http.Client, rc string, ref string) ([]byte, error) {
func HttpReqGet(u string, hc *http.Client, rc *string, ref *string, noref bool) ([]byte, error) {
l := log.WithFields(log.Fields{
"action": "httpclient.HttpReqGet",
})
Expand All @@ -45,20 +45,20 @@ func HttpReqGet(u string, hc *http.Client, rc string, ref string) ([]byte, error
}

// Set cookie
if rc != "" {
if rc != nil {
ratCookie := &http.Cookie{
Name: "rat",
Value: rc,
Value: *rc,
}
httpReq.AddCookie(ratCookie)
}

// Set referer
if ref != "" {
httpReq.Header.Set("referer", ref)
}
if ref == "" {
if !noref {
httpReq.Header.Set("referer", "https://www.seaofthieves.com/profile/achievements")
if ref != nil {
httpReq.Header.Set("referer", *ref)
}
}

// Set HTTP header
Expand All @@ -74,7 +74,7 @@ func HttpReqGet(u string, hc *http.Client, rc string, ref string) ([]byte, error
}
}()

if serverResp.StatusCode != 200 {
if serverResp.StatusCode >= 400 {
l.Errorf("HTTP request failed: %v", serverResp.Status)
return []byte{}, fmt.Errorf("%v", serverResp.StatusCode)
}
Expand Down

0 comments on commit 5343a4b

Please sign in to comment.