Skip to content

Commit 5b52945

Browse files
Merge pull request #7 from speakeasy-api/add_retry_after_and_paginatino_url
feat: add retry after and pagination url
2 parents 248e4ef + a89e1fe commit 5b52945

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

cmd/server/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func main() {
3333
r.HandleFunc("/pagination/limitoffset/page", pagination.HandleLimitOffsetPage).Methods(http.MethodGet, http.MethodPut)
3434
r.HandleFunc("/pagination/limitoffset/offset", pagination.HandleLimitOffsetOffset).Methods(http.MethodGet, http.MethodPut)
3535
r.HandleFunc("/pagination/cursor", pagination.HandleCursor).Methods(http.MethodGet, http.MethodPut)
36+
r.HandleFunc("/pagination/url", pagination.HandleURL).Methods(http.MethodGet)
3637
r.HandleFunc("/retries", retries.HandleRetries).Methods(http.MethodGet, http.MethodPost)
3738
r.HandleFunc("/errors/{status_code}", errors.HandleErrors).Methods(http.MethodGet)
3839
r.HandleFunc("/optional", acceptHeaders.HandleAcceptHeaderMultiplexing).Methods(http.MethodGet)

internal/pagination/service.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package pagination
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"math"
67
"net/http"
78
"strconv"
@@ -18,8 +19,9 @@ type CursorRequest struct {
1819
}
1920

2021
type PaginationResponse struct {
21-
NumPages int `json:"numPages"`
22-
ResultArray []int `json:"resultArray"`
22+
NumPages int `json:"numPages"`
23+
ResultArray []int `json:"resultArray"`
24+
Next *string `json:"next"`
2325
}
2426

2527
const total = 20
@@ -116,6 +118,41 @@ func HandleCursor(w http.ResponseWriter, r *http.Request) {
116118
}
117119
}
118120

121+
func HandleURL(w http.ResponseWriter, r *http.Request) {
122+
attemptsString := r.FormValue("attempts")
123+
var attempts int
124+
if attemptsString != "" {
125+
var err error
126+
attempts, err = strconv.Atoi(attemptsString)
127+
if err != nil {
128+
w.WriteHeader(http.StatusBadRequest)
129+
_, _ = w.Write([]byte("attempts must be an integer"))
130+
return
131+
}
132+
}
133+
134+
res := PaginationResponse{
135+
NumPages: 0,
136+
ResultArray: make([]int, 0),
137+
}
138+
139+
if attempts > 0 {
140+
baseURL := fmt.Sprintf("%s://%s%s", r.URL.Scheme, r.Host, r.URL.Path)
141+
if r.URL.Scheme == "" { // Fallback if Scheme is not available
142+
baseURL = fmt.Sprintf("http://%s%s", r.Host, r.URL.Path)
143+
}
144+
145+
nextUrl := fmt.Sprintf("%s?attempts=%d", baseURL, attempts-1)
146+
res.Next = &nextUrl
147+
}
148+
149+
w.Header().Set("Content-Type", "application/json")
150+
err := json.NewEncoder(w).Encode(res)
151+
if err != nil {
152+
w.WriteHeader(500)
153+
}
154+
}
155+
119156
func getValue(queryValue string, hasBody bool, paginationValue int) int {
120157
if hasBody {
121158
return paginationValue

internal/retries/service.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type retriesResponse struct {
1515
func HandleRetries(w http.ResponseWriter, r *http.Request) {
1616
requestID := r.URL.Query().Get("request-id")
1717
numRetriesStr := r.URL.Query().Get("num-retries")
18+
includeHeaderTimeout := r.URL.Query().Get("included-header-timeout")
1819

1920
numRetries := 3
2021
if numRetriesStr != "" {
@@ -27,6 +28,17 @@ func HandleRetries(w http.ResponseWriter, r *http.Request) {
2728
}
2829
}
2930

31+
var headerTimeout int
32+
if includeHeaderTimeout != "" {
33+
var err error
34+
headerTimeout, err = strconv.Atoi(includeHeaderTimeout)
35+
if err != nil {
36+
w.WriteHeader(http.StatusBadRequest)
37+
_, _ = w.Write([]byte("included-header-timeout must be an integer"))
38+
return
39+
}
40+
}
41+
3042
if requestID == "" {
3143
w.WriteHeader(http.StatusBadRequest)
3244
_, _ = w.Write([]byte("request-id is required"))
@@ -40,6 +52,9 @@ func HandleRetries(w http.ResponseWriter, r *http.Request) {
4052
callCounts[requestID]++
4153

4254
if callCounts[requestID] < numRetries {
55+
if headerTimeout > 0 {
56+
w.Header().Set("Retry-After", strconv.Itoa(headerTimeout))
57+
}
4358
w.WriteHeader(http.StatusServiceUnavailable)
4459
_, _ = w.Write([]byte("request failed please retry"))
4560
return

0 commit comments

Comments
 (0)