PUT /admin/api/v1/pets/{petname}/script
(w http.ResponseWriter, r *http.Request)
| 124 | |
| 125 | // PUT /admin/api/v1/pets/{petname}/script |
| 126 | func apiV1PutPetScript(w http.ResponseWriter, r *http.Request) { |
| 127 | petName := strings.ToLower(strings.TrimSpace(r.PathValue("petname"))) |
| 128 | if petName == "" { |
| 129 | writeAPIError(w, http.StatusBadRequest, "petname is required") |
| 130 | return |
| 131 | } |
| 132 | |
| 133 | var body struct { |
| 134 | Script string `json:"script"` |
| 135 | } |
| 136 | if err := json.NewDecoder(r.Body).Decode(&body); err != nil { |
| 137 | writeAPIError(w, http.StatusBadRequest, "malformed request body: "+err.Error()) |
| 138 | return |
| 139 | } |
| 140 | |
| 141 | if err := pets.SavePetScript(petName, body.Script); err != nil { |
| 142 | writeAPIError(w, http.StatusInternalServerError, err.Error()) |
| 143 | return |
| 144 | } |
| 145 | |
| 146 | // Invalidate the cached VM so the next invocation picks up the new script |
| 147 | scripting.InvalidatePetVM(petName) |
| 148 | |
| 149 | writeJSON(w, http.StatusOK, APIResponse[struct{}]{Success: true}) |
| 150 | } |
nothing calls this directly
no test coverage detected