Skip to content

Commit 6570ba0

Browse files
feat: add retries endpoint for testing retries
1 parent 6a01f5b commit 6570ba0

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
# speakeasy-auth-test-service
1+
# speakeasy-api-test-service
2+
3+
This is a service designed to be built into a docker container and used along the test suite in the `openapi-generation` repo.
4+
5+
This will eventually replace our usage of `httpbin` as a test service, and contain tailor made endpoints for testing the generated SDKs.

cmd/server/main.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package main
22

33
import (
4-
"github.com/speakeasy-api/speakeasy-auth-test-service/internal/pagination"
5-
"github.com/speakeasy-api/speakeasy-auth-test-service/internal/responseHeaders"
64
"log"
75
"net/http"
86

7+
"github.com/speakeasy-api/speakeasy-auth-test-service/internal/pagination"
8+
"github.com/speakeasy-api/speakeasy-auth-test-service/internal/responseHeaders"
9+
"github.com/speakeasy-api/speakeasy-auth-test-service/internal/retries"
10+
911
"github.com/gorilla/mux"
1012
"github.com/speakeasy-api/speakeasy-auth-test-service/internal/auth"
1113
"github.com/speakeasy-api/speakeasy-auth-test-service/internal/requestbody"
@@ -22,6 +24,7 @@ func main() {
2224
r.HandleFunc("/pagination/limitoffset/page", pagination.HandleLimitOffsetPage).Methods(http.MethodGet, http.MethodPut)
2325
r.HandleFunc("/pagination/limitoffset/offset", pagination.HandleLimitOffsetOffset).Methods(http.MethodGet, http.MethodPut)
2426
r.HandleFunc("/pagination/cursor", pagination.HandleCursor).Methods(http.MethodGet, http.MethodPut)
27+
r.HandleFunc("/retries", retries.HandleRetries).Methods(http.MethodGet)
2528

2629
log.Println("Listening on :8080")
2730
if err := http.ListenAndServe(":8080", r); err != nil {

internal/retries/service.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package retries
2+
3+
import (
4+
"net/http"
5+
"strconv"
6+
)
7+
8+
var callCounts = map[string]int{}
9+
10+
func HandleRetries(w http.ResponseWriter, r *http.Request) {
11+
requestID := r.URL.Query().Get("request-id")
12+
numRetriesStr := r.URL.Query().Get("num-retries")
13+
14+
numRetries := 3
15+
if numRetriesStr != "" {
16+
var err error
17+
numRetries, err = strconv.Atoi(numRetriesStr)
18+
if err != nil {
19+
w.WriteHeader(http.StatusBadRequest)
20+
_, _ = w.Write([]byte("num-retries must be an integer"))
21+
return
22+
}
23+
}
24+
25+
if requestID == "" {
26+
w.WriteHeader(http.StatusBadRequest)
27+
_, _ = w.Write([]byte("request-id is required"))
28+
return
29+
}
30+
31+
_, ok := callCounts[requestID]
32+
if !ok {
33+
callCounts[requestID] = 0
34+
}
35+
callCounts[requestID]++
36+
37+
if callCounts[requestID] < numRetries {
38+
w.WriteHeader(http.StatusInternalServerError)
39+
_, _ = w.Write([]byte("request failed please retry"))
40+
return
41+
}
42+
43+
delete(callCounts, requestID)
44+
w.WriteHeader(http.StatusOK)
45+
}

0 commit comments

Comments
 (0)