mirror of
https://codeberg.org/SafeTwitch/safetwitch-backend.git
synced 2024-12-22 13:13:00 -05:00
Proper error handling and better variable name
This commit is contained in:
parent
edb2733373
commit
8baaa9a369
1 changed files with 18 additions and 13 deletions
|
@ -4,7 +4,6 @@ import (
|
|||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
|
@ -18,7 +17,7 @@ const twitchUrl = "https://gql.twitch.tv/gql"
|
|||
func call(url, method string, body io.Reader) (*http.Response, error) {
|
||||
req, err := http.NewRequest(method, url, body)
|
||||
if err != nil {
|
||||
return req.Response, fmt.Errorf("Got error %s", err.Error())
|
||||
return req.Response, err
|
||||
}
|
||||
|
||||
req.Header.Add("Client-Id", "kimne78kx3ncx6brgo4mv6wki5h1ko")
|
||||
|
@ -92,36 +91,42 @@ func GetStreamerInfo(streamerName string) (structs.TwitchApiResponse, error) {
|
|||
},
|
||||
},
|
||||
}
|
||||
|
||||
json_data, err := json.Marshal(payload)
|
||||
|
||||
if err != nil {
|
||||
fmt.Errorf("err")
|
||||
return structs.TwitchApiResponse{}, nil
|
||||
}
|
||||
|
||||
resp, err := call(twitchUrl, "POST", bytes.NewBuffer(json_data))
|
||||
if err != nil {
|
||||
fmt.Errorf("y")
|
||||
return structs.TwitchApiResponse{}, err
|
||||
}
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
defer resp.Body.Close()
|
||||
|
||||
var result []structs.TwitchApiResponse
|
||||
err = json.Unmarshal(body, &result)
|
||||
RespStatusHandler(resp.StatusCode)
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
return structs.TwitchApiResponse{}, err
|
||||
}
|
||||
|
||||
var twitchResponse []structs.TwitchApiResponse
|
||||
err = json.Unmarshal(body, &twitchResponse)
|
||||
if err != nil {
|
||||
return structs.TwitchApiResponse{}, err
|
||||
}
|
||||
|
||||
// begin parsing response
|
||||
streamerFound := gjson.Get(string(body), "0.data.user")
|
||||
if streamerFound.String() == "" {
|
||||
return result[0], errors.New("streamer not found")
|
||||
return twitchResponse[0], errors.New("streamer not found")
|
||||
}
|
||||
|
||||
streamerData := gjson.Get(string(body), "0.data")
|
||||
parsedSocials, err := ParseSocials(streamerData.String())
|
||||
if err != nil {
|
||||
return result[0], nil
|
||||
return twitchResponse[0], nil
|
||||
}
|
||||
log.Println(parsedSocials)
|
||||
|
||||
return result[0], nil
|
||||
return twitchResponse[0], nil
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue