Skip to content

Commit

Permalink
Merge pull request #22 from Icelain/dev
Browse files Browse the repository at this point in the history
Switched to using the standard library url builder
  • Loading branch information
Icelain authored Dec 3, 2022
2 parents f71f872 + e78a847 commit 7f9bde4
Showing 1 changed file with 19 additions and 25 deletions.
44 changes: 19 additions & 25 deletions jokeapi.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"io/ioutil"
"net/http"
"net/url"
"strings"
)

Expand Down Expand Up @@ -48,13 +49,6 @@ type JokeAPI struct {
ExportedParams Params
}

func setSign(sign *string) {

if *sign == "?" {
*sign = "&"
}
}

func contextifyError(context string, err error) error {

return errors.New(context + ": " + err.Error())
Expand All @@ -68,37 +62,37 @@ func (j *JokeAPI) Fetch() (JokesResp, error) {
//response = map[string]interface{}{}
jokeConsumer jokeConsumer
mainURL string
sign string = "?"
reqUrl *url.URL
err error
)


//param handling begins here
if len(j.ExportedParams.Categories) > 0 {
mainURL = baseURL + strings.Join(j.ExportedParams.Categories, ",")
} else {
mainURL = baseURL + "Any"
}

reqUrl, err = url.Parse(mainURL)
if err != nil {

if err != nil {
return JokesResp{}, contextifyError("Request failed as url could not be generated", err)
}

if len(j.ExportedParams.Blacklist) > 0 {

mainURL += sign + "blacklistFlags=" + strings.Join(j.ExportedParams.Blacklist, ",")

setSign(&sign)
}

if j.ExportedParams.JokeType != "" {

mainURL += sign + "type=" + j.ExportedParams.JokeType
setSign(&sign)
}

query := reqUrl.Query()

if j.ExportedParams.Lang != "" {

mainURL += sign + "lang=" + j.ExportedParams.Lang

}
query.Set("blacklistFlags", strings.Join(j.ExportedParams.Blacklist, ","))
query.Set("type", j.ExportedParams.JokeType)
query.Set("lang", j.ExportedParams.Lang)
reqUrl.RawQuery = query.Encode()

//param handling ends here
resp, err := http.Get(mainURL)
resp, err := http.Get(reqUrl.String())
if err != nil {
return JokesResp{}, contextifyError("Request failed", err)
}
Expand Down

0 comments on commit 7f9bde4

Please sign in to comment.