From 71e4d50eb3e8a076e3914374a385ba032282cded Mon Sep 17 00:00:00 2001 From: Darren Shepherd Date: Fri, 15 Nov 2024 17:14:35 -0700 Subject: [PATCH] chore: add missing file --- apiclient/create.go | 75 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 apiclient/create.go diff --git a/apiclient/create.go b/apiclient/create.go new file mode 100644 index 000000000..653add0cb --- /dev/null +++ b/apiclient/create.go @@ -0,0 +1,75 @@ +package apiclient + +import ( + "context" + "encoding/json" + "io" + "net/http" + + "github.com/otto8-ai/otto8/apiclient/types" +) + +func (c *Client) getGeneric(ctx context.Context, typeName, ref string) (string, error) { + _, resp, err := c.doRequest(ctx, http.MethodGet, "/"+typeName+"s/"+ref, nil) + if err != nil { + return "", err + } + defer resp.Body.Close() + + var outputMap = map[string]any{} + if err := json.NewDecoder(resp.Body).Decode(&outputMap); err != nil { + return "", err + } + ref, _ = outputMap["id"].(string) + return ref, nil +} + +func (c *Client) createGeneric(ctx context.Context, typeName string, inputMap any) (string, error) { + _, resp, err := c.postJSON(ctx, "/"+typeName+"s", inputMap) + if err != nil { + return "", err + } + defer resp.Body.Close() + + var outputMap = map[string]any{} + if err := json.NewDecoder(resp.Body).Decode(&outputMap); err != nil { + return "", err + } + id, _ := outputMap["id"].(string) + return id, nil +} + +func (c *Client) Create(ctx context.Context, typeName string, body []byte) (string, error) { + var ( + inputMap = map[string]any{} + ) + + if err := json.Unmarshal(body, &inputMap); err != nil { + return "", err + } + + alias, _ := inputMap["alias"].(string) + if alias == "" { + return c.createGeneric(ctx, typeName, inputMap) + } + + id, err := c.getGeneric(ctx, typeName, alias) + if types.IsNotFound(err) { + return c.createGeneric(ctx, typeName, inputMap) + } else if err != nil { + return "", err + } + + _, resp, err := c.putJSON(ctx, "/"+typeName+"s/"+id, inputMap) + if err != nil { + return "", err + } + defer resp.Body.Close() + + _, err = io.ReadAll(resp.Body) + if err != nil { + return "", err + } + + return id, nil +}