From 1b3c38542bddf95b036bcbfae8eea20ce19b62b3 Mon Sep 17 00:00:00 2001 From: p0t4t0sandwich Date: Tue, 2 Apr 2024 05:39:31 -0600 Subject: [PATCH] Reworked wip pet API to use problem responses --- modules/petpictures/petpictures.go | 37 ++++++++++++++++++++++++---- problemresponses/problemresponses.go | 20 +++++++++++++++ 2 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 problemresponses/problemresponses.go diff --git a/modules/petpictures/petpictures.go b/modules/petpictures/petpictures.go index 4231dd9..3cd3409 100644 --- a/modules/petpictures/petpictures.go +++ b/modules/petpictures/petpictures.go @@ -6,6 +6,7 @@ import ( "crypto/md5" "encoding/hex" "encoding/json" + "encoding/xml" "io" "log" "mime/multipart" @@ -14,6 +15,7 @@ import ( "strings" "github.com/NeuralNexusDev/neuralnexus-api/modules/database" + "github.com/NeuralNexusDev/neuralnexus-api/problemresponses" ) // CREATE TABLE pictures( @@ -400,8 +402,23 @@ func CreatePetHandler(w http.ResponseWriter, r *http.Request) { } } if petName == "" { - http.Error(w, "Invalid pet name", http.StatusBadRequest) - return + problem := problemresponses.NewProblemResponse( + "invalid_input", + "Invalid input", + "Pet name is required", + // TODO: Add instance + "TODO: Add instance", + ) + w.WriteHeader(http.StatusBadRequest) + if r.Header.Get("Content-Type") == "application/json" { + w.Header().Set("Content-Type", "application/problem+json") + json.NewEncoder(w).Encode(problem) + } else if r.Header.Get("Content-Type") == "application/xml" { + w.Header().Set("Content-Type", "application/problem+xml") + xml.NewEncoder(w).Encode(problem) + } else { + http.Error(w, "Pet name is required", http.StatusBadRequest) + } } petResponse := createPet(petName) @@ -410,9 +427,19 @@ func CreatePetHandler(w http.ResponseWriter, r *http.Request) { return } - w.Header().Set("Content-Type", "application/json") - json.NewEncoder(w).Encode(APIResponse[Pet]{ + apiResponse := APIResponse[Pet]{ Success: true, Data: petResponse.Data, - }) + } + + w.WriteHeader(http.StatusCreated) + if r.Header.Get("Content-Type") == "application/json" { + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(apiResponse) + } else if r.Header.Get("Content-Type") == "application/xml" { + w.Header().Set("Content-Type", "application/xml") + xml.NewEncoder(w).Encode(apiResponse) + } else { + w.Write([]byte("Pet created: " + petResponse.Data.Name + " (ID: " + string(petResponse.Data.ID) + ")")) + } } diff --git a/problemresponses/problemresponses.go b/problemresponses/problemresponses.go new file mode 100644 index 0000000..16e7df5 --- /dev/null +++ b/problemresponses/problemresponses.go @@ -0,0 +1,20 @@ +package problemresponses + +// -------------- Structs -------------- +// ProblemResponse -- Defined by https://www.rfc-editor.org/rfc/rfc9457.html#section-3 +type ProblemResponse struct { + Type string `json:"type" xml:"type"` + Title string `json:"title" xml:"title"` + Detail string `json:"detail" xml:"detail"` + Instance string `json:"instance" xml:"instance"` +} + +// NewProblemResponse -- Create a new ProblemResponse +func NewProblemResponse(Type, Title, Detail, Instance string) *ProblemResponse { + return &ProblemResponse{ + Type: Type, + Title: Title, + Detail: Detail, + Instance: Instance, + } +}