Skip to content

Commit

Permalink
Merge pull request #233 from twitchdev/fix-230-232
Browse files Browse the repository at this point in the history
Fix 230 232
  • Loading branch information
Xemdo authored Apr 21, 2023
2 parents 2c9bb6d + 5023d1f commit ba7e315
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
21 changes: 16 additions & 5 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,20 +104,26 @@ 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 {
fmt.Printf("Error unmarshalling body: %v", err)
return
}

var cursor string = ""
if extensionsBody.Pagination != nil {
cursor = *extensionsBody.Pagination
}

apiResponse = models.APIResponse{
Data: extensionsBody.Data,
Status: extensionsBody.Status,
Error: extensionsBody.Error,
Message: extensionsBody.Message,
Pagination: &models.APIPagination{
Cursor: *extensionsBody.Pagination,
Cursor: cursor,
},
}
} else {
Expand Down Expand Up @@ -175,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 ba7e315

Please sign in to comment.