Skip to content

Commit

Permalink
Reworked wip pet API to use problem responses
Browse files Browse the repository at this point in the history
  • Loading branch information
p0t4t0sandwich committed Apr 2, 2024
1 parent 71b7943 commit 1b3c385
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
37 changes: 32 additions & 5 deletions modules/petpictures/petpictures.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"encoding/xml"
"io"
"log"
"mime/multipart"
Expand All @@ -14,6 +15,7 @@ import (
"strings"

"github.com/NeuralNexusDev/neuralnexus-api/modules/database"
"github.com/NeuralNexusDev/neuralnexus-api/problemresponses"
)

// CREATE TABLE pictures(
Expand Down Expand Up @@ -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)
Expand All @@ -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) + ")"))
}
}
20 changes: 20 additions & 0 deletions problemresponses/problemresponses.go
Original file line number Diff line number Diff line change
@@ -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,
}
}

0 comments on commit 1b3c385

Please sign in to comment.