-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fetch and create registered models (#129)
In this PR: - GET /v1/model-registry/{model_registry_id}/registered_models - POST /v1/model-registry/{model_registry_id}/registered_models - HTTP client Signed-off-by: Eder Ignatowicz <[email protected]>
- Loading branch information
Showing
21 changed files
with
771 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"github.com/julienschmidt/httprouter" | ||
"github.com/kubeflow/model-registry/pkg/openapi" | ||
"github.com/kubeflow/model-registry/ui/bff/data" | ||
"github.com/kubeflow/model-registry/ui/bff/integrations" | ||
"github.com/kubeflow/model-registry/ui/bff/validation" | ||
"net/http" | ||
) | ||
|
||
func (app *App) GetRegisteredModelsHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
//TODO (ederign) implement pagination | ||
client, ok := r.Context().Value(httpClientKey).(integrations.HTTPClientInterface) | ||
if !ok { | ||
app.serverErrorResponse(w, r, errors.New("REST client not found")) | ||
return | ||
} | ||
|
||
modelList, err := data.FetchAllRegisteredModels(client) | ||
if err != nil { | ||
app.serverErrorResponse(w, r, err) | ||
return | ||
} | ||
|
||
modelRegistryRes := Envelope{ | ||
"registered_models": modelList, | ||
} | ||
|
||
err = app.WriteJSON(w, http.StatusOK, modelRegistryRes, nil) | ||
if err != nil { | ||
app.serverErrorResponse(w, r, err) | ||
} | ||
} | ||
|
||
func (app *App) CreateRegisteredModelHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
client, ok := r.Context().Value(httpClientKey).(integrations.HTTPClientInterface) | ||
if !ok { | ||
app.serverErrorResponse(w, r, errors.New("REST client not found")) | ||
return | ||
} | ||
|
||
var model openapi.RegisteredModel | ||
if err := json.NewDecoder(r.Body).Decode(&model); err != nil { | ||
app.serverErrorResponse(w, r, fmt.Errorf("error decoding JSON:: %v", err.Error())) | ||
return | ||
} | ||
|
||
if err := validation.ValidateRegisteredModel(model); err != nil { | ||
app.badRequestResponse(w, r, fmt.Errorf("validation error:: %v", err.Error())) | ||
return | ||
} | ||
|
||
jsonData, err := json.Marshal(model) | ||
if err != nil { | ||
app.serverErrorResponse(w, r, fmt.Errorf("error marshaling model to JSON: %w", err)) | ||
return | ||
} | ||
|
||
createdModel, err := data.CreateRegisteredModel(client, jsonData) | ||
if err != nil { | ||
var httpErr *integrations.HTTPError | ||
if errors.As(err, &httpErr) { | ||
app.errorResponse(w, r, httpErr) | ||
} else { | ||
app.serverErrorResponse(w, r, err) | ||
} | ||
return | ||
} | ||
|
||
if createdModel == nil { | ||
app.serverErrorResponse(w, r, fmt.Errorf("created model is nil")) | ||
return | ||
} | ||
|
||
w.Header().Set("Location", fmt.Sprintf("%s/%s", RegisteredModelsPath, *createdModel.Id)) | ||
err = app.WriteJSON(w, http.StatusCreated, createdModel, nil) | ||
if err != nil { | ||
app.serverErrorResponse(w, r, fmt.Errorf("error writing JSON")) | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.