Skip to content

Commit

Permalink
feat: add retries endpoint for testing retries
Browse files Browse the repository at this point in the history
  • Loading branch information
TristanSpeakEasy committed Jun 12, 2023
1 parent 6a01f5b commit 6570ba0
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# speakeasy-auth-test-service
# speakeasy-api-test-service

This is a service designed to be built into a docker container and used along the test suite in the `openapi-generation` repo.

This will eventually replace our usage of `httpbin` as a test service, and contain tailor made endpoints for testing the generated SDKs.
7 changes: 5 additions & 2 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package main

import (
"github.com/speakeasy-api/speakeasy-auth-test-service/internal/pagination"
"github.com/speakeasy-api/speakeasy-auth-test-service/internal/responseHeaders"
"log"
"net/http"

"github.com/speakeasy-api/speakeasy-auth-test-service/internal/pagination"
"github.com/speakeasy-api/speakeasy-auth-test-service/internal/responseHeaders"
"github.com/speakeasy-api/speakeasy-auth-test-service/internal/retries"

"github.com/gorilla/mux"
"github.com/speakeasy-api/speakeasy-auth-test-service/internal/auth"
"github.com/speakeasy-api/speakeasy-auth-test-service/internal/requestbody"
Expand All @@ -22,6 +24,7 @@ func main() {
r.HandleFunc("/pagination/limitoffset/page", pagination.HandleLimitOffsetPage).Methods(http.MethodGet, http.MethodPut)
r.HandleFunc("/pagination/limitoffset/offset", pagination.HandleLimitOffsetOffset).Methods(http.MethodGet, http.MethodPut)
r.HandleFunc("/pagination/cursor", pagination.HandleCursor).Methods(http.MethodGet, http.MethodPut)
r.HandleFunc("/retries", retries.HandleRetries).Methods(http.MethodGet)

log.Println("Listening on :8080")
if err := http.ListenAndServe(":8080", r); err != nil {
Expand Down
45 changes: 45 additions & 0 deletions internal/retries/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package retries

import (
"net/http"
"strconv"
)

var callCounts = map[string]int{}

func HandleRetries(w http.ResponseWriter, r *http.Request) {
requestID := r.URL.Query().Get("request-id")
numRetriesStr := r.URL.Query().Get("num-retries")

numRetries := 3
if numRetriesStr != "" {
var err error
numRetries, err = strconv.Atoi(numRetriesStr)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte("num-retries must be an integer"))
return
}
}

if requestID == "" {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte("request-id is required"))
return
}

_, ok := callCounts[requestID]
if !ok {
callCounts[requestID] = 0
}
callCounts[requestID]++

if callCounts[requestID] < numRetries {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte("request failed please retry"))
return
}

delete(callCounts, requestID)
w.WriteHeader(http.StatusOK)
}

0 comments on commit 6570ba0

Please sign in to comment.