Skip to content

Commit

Permalink
Changed APIResonse struct to generics to not make assumptions about f…
Browse files Browse the repository at this point in the history
…ormat used in the Data response field
  • Loading branch information
Xemdo committed Apr 21, 2023
1 parent 224a9f7 commit 29ede46
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
13 changes: 9 additions & 4 deletions internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func NewRequest(method string, path string, queryParameters []string, body []byt
var err error
var cursor string

isExtensionsEndpoint := false // https://github.com/twitchdev/twitch-cli/issues/157
isExtensionsLiveEndpoint := false // https://github.com/twitchdev/twitch-cli/issues/157

data.Data = make([]interface{}, 0)
client, err := GetClientInformation()
Expand Down Expand Up @@ -104,7 +104,7 @@ func NewRequest(method string, path string, queryParameters []string, body []byt
}
if strings.Contains(u.String(), "extensions/live") {
// https://github.com/twitchdev/twitch-cli/issues/157
isExtensionsEndpoint = true
isExtensionsLiveEndpoint = true
var extensionsBody models.ExtensionAPIResponse
err = json.Unmarshal(resp.Body, &extensionsBody)
if err != nil {
Expand Down Expand Up @@ -181,12 +181,17 @@ func NewRequest(method string, path string, queryParameters []string, body []byt
data.Data = make([]interface{}, 0)
}
// handle json marshalling better; returns empty slice vs. null
if !strings.Contains(path, "schedule") && len(data.Data.([]interface{})) == 0 && data.Error == "" {
_, isInterface := data.Data.([]interface{})
if isInterface && !strings.Contains(path, "schedule") && len(data.Data.([]interface{})) == 0 && data.Error == "" {
data.Data = make([]interface{}, 0)
}
_, isStringMap := data.Data.(map[string]any)
if isStringMap && !strings.Contains(path, "schedule") && len(data.Data.(map[string]any)) == 0 && data.Error == "" {
data.Data = make(map[string]any, 0)
}

var d []byte
if isExtensionsEndpoint {
if isExtensionsLiveEndpoint {
extensionBody := models.ExtensionAPIResponse{
Data: data.Data,
Pagination: &data.Pagination.Cursor,
Expand Down
4 changes: 2 additions & 2 deletions internal/models/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package models

type APIResponse struct {
Data interface{} `json:"data,omitempty"`
Data any `json:"data,omitempty"`
Pagination *APIPagination `json:"pagination,omitempty"`
Error string `json:"error,omitempty"`
Status int `json:"status,omitempty"`
Expand All @@ -22,7 +22,7 @@ type BitsLeaderboardDateRange struct {
EndedAt string `json:"ended_at"`
}

type ExtensionAPIResponse struct {
type ExtensionAPIResponse struct { // extensions/live
Data interface{} `json:"data,omitempty"`
Pagination *string `json:"pagination,omitempty"`
Error string `json:"error,omitempty"`
Expand Down

0 comments on commit 29ede46

Please sign in to comment.