Skip to content

Commit 0f10b98

Browse files
committed
Fix: Links list response
The link response format can change when containing one or multiple projects. The SDK will now cover both scenarios.
1 parent 176f41b commit 0f10b98

2 files changed

Lines changed: 22 additions & 4 deletions

File tree

projects/link.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ type Link struct {
7676
CreatedAt time.Time `json:"created-date"`
7777

7878
// UpdatedByUserID is the user who last updated this link.
79-
UpdatedByUserID *int64 `json:"updated-by-userId"`
79+
UpdatedByUserID *LegacyNumber `json:"updated-by-userId"`
8080

8181
// UpdatedAt is the date and time when the link was last updated.
8282
UpdatedAt *time.Time `json:"updated-date"`
@@ -559,7 +559,11 @@ type LinkListResponse struct {
559559
request LinkListRequest
560560
hasMore bool
561561

562-
Links []Link `json:"links"`
562+
// Links is the list of links matching the request filters. This field is not
563+
// directly decoded from the API response, but is populated in the
564+
// HandleHTTPResponse method, which decodes the response and extracts the
565+
// links from it.
566+
Links []Link `json:"-"`
563567
}
564568

565569
// HandleHTTPResponse handles the HTTP response for the LinkListResponse. If
@@ -574,9 +578,23 @@ func (l *LinkListResponse) HandleHTTPResponse(resp *http.Response) error {
574578
pages, _ := strconv.ParseInt(resp.Header.Get("X-Pages"), 10, 64)
575579
l.hasMore = pages > page
576580

577-
if err := json.NewDecoder(resp.Body).Decode(l); err != nil {
581+
var links struct {
582+
Project struct {
583+
Links []Link `json:"links"`
584+
} `json:"project"`
585+
Projects []struct {
586+
Links []Link `json:"links"`
587+
} `json:"projects"`
588+
}
589+
590+
if err := json.NewDecoder(resp.Body).Decode(&links); err != nil {
578591
return fmt.Errorf("failed to decode list links response: %w", err)
579592
}
593+
594+
l.Links = links.Project.Links
595+
for _, project := range links.Projects {
596+
l.Links = append(l.Links, project.Links...)
597+
}
580598
return nil
581599
}
582600

projects/link_example_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func startLinkServer() (string, func(), error) {
182182
mux.HandleFunc("GET /links", func(w http.ResponseWriter, _ *http.Request) {
183183
w.WriteHeader(http.StatusOK)
184184
w.Header().Set("Content-Type", "application/json")
185-
_, _ = fmt.Fprintln(w, `{"links":[{"id":"12345"},{"id":"12346"}]}`)
185+
_, _ = fmt.Fprintln(w, `{"project":{"links":[{"id":"12345"},{"id":"12346"}]}}`)
186186
})
187187

188188
server := &http.Server{

0 commit comments

Comments
 (0)