make REST api more restful

This commit is contained in:
André Roth
2024-10-03 14:33:52 +02:00
parent a3078fa93e
commit 06b2b920da
3 changed files with 17 additions and 25 deletions
+1 -1
View File
@@ -201,7 +201,7 @@ func Router(c *ctx.AptlyContext) http.Handler {
api.DELETE("/snapshots/:name", apiSnapshotsDrop)
api.GET("/snapshots/:name/diff/:withSnapshot", apiSnapshotsDiff)
api.POST("/snapshots/merge", apiSnapshotsMerge)
api.POST("/snapshots/pull", apiSnapshotsPull)
api.POST("/snapshots/:name/pull", apiSnapshotsPull)
}
{
+7 -6
View File
@@ -486,8 +486,6 @@ func apiSnapshotsMerge(c *gin.Context) {
type snapshotsPullBody struct {
// Source name where packages and dependencies will be searched
Source string `binding:"required" json:"Source" example:"source-snapshot"`
// Snapshot where packages and dependencies will be pulled to
To string `binding:"required" json:"To" example:"to-snapshot"`
// Name of the snapshot that will be created
Destination string `binding:"required" json:"Destination" example:"idestination-snapshot"`
// List of package queries, in the simplest form, name of package to be pulled from
@@ -504,13 +502,14 @@ type snapshotsPullBody struct {
// @Param no-deps query int false "no-deps: 1 to enable"
// @Param no-remove query int false "no-remove: 1 to enable"
// @Accept json
// @Param name path string true "Snapshot where packages and dependencies will be pulled to"
// @Param request body snapshotsPullBody true "See api.snapshotsPullBody"
// @Produce json
// @Success 200
// @Failure 400 {object} Error "Bad Request"
// @Failure 404 {object} Error "Not Found"
// @Failure 500 {object} Error "Internal Error"
// @Router /api/snapshots/pull [post]
// @Router /api/snapshots/{name}/pull [post]
func apiSnapshotsPull(c *gin.Context) {
var (
err error
@@ -518,6 +517,8 @@ func apiSnapshotsPull(c *gin.Context) {
body snapshotsPullBody
)
name := c.Params.ByName("name")
if err = c.BindJSON(&body); err != nil {
AbortWithJSONError(c, http.StatusBadRequest, err)
return
@@ -530,8 +531,8 @@ func apiSnapshotsPull(c *gin.Context) {
collectionFactory := context.NewCollectionFactory()
// Load <To> snapshot
toSnapshot, err := collectionFactory.SnapshotCollection().ByName(body.To)
// Load <name> snapshot
toSnapshot, err := collectionFactory.SnapshotCollection().ByName(name)
if err != nil {
AbortWithJSONError(c, http.StatusNotFound, err)
return
@@ -555,7 +556,7 @@ func apiSnapshotsPull(c *gin.Context) {
}
resources := []string{string(sourceSnapshot.ResourceKey()), string(toSnapshot.ResourceKey())}
taskName := fmt.Sprintf("Pull snapshot %s into %s and save as %s", body.Source, body.To, body.Destination)
taskName := fmt.Sprintf("Pull snapshot %s into %s and save as %s", body.Source, name, body.Destination)
maybeRunTaskInBackground(c, taskName, resources, func(_ aptly.Progress, _ *task.Detail) (*task.ProcessReturnValue, error) {
// convert snapshots to package list
toPackageList, err := deb.NewPackageListFromRefList(toSnapshot.RefList(), collectionFactory.PackageCollection(), context.Progress())
+9 -18
View File
@@ -434,9 +434,8 @@ class SnapshotsAPITestPull(APITest):
snapshot_pull_libboost = self.random_name()
# dry run first
resp = self.post("/api/snapshots/pull?dry-run=1", json={
resp = self.post(f"/api/snapshots/{snapshot_empty_repo}/pull?dry-run=1", json={
'Source': snapshot_repo_with_libboost,
'To': snapshot_empty_repo,
'Destination': snapshot_pull_libboost,
'Queries': [
'libboost-program-options-dev'
@@ -449,9 +448,8 @@ class SnapshotsAPITestPull(APITest):
self.check_equal(resp.status_code, 200)
# dry run, all-matches
resp = self.post("/api/snapshots/pull?dry-run=1&all-matches=1", json={
resp = self.post("/api/snapshots/{snapshot_empty_repo}/pull?dry-run=1&all-matches=1", json={
'Source': snapshot_repo_with_libboost,
'To': snapshot_empty_repo,
'Destination': snapshot_pull_libboost,
'Queries': [
'libboost-program-options-dev'
@@ -464,17 +462,15 @@ class SnapshotsAPITestPull(APITest):
self.check_equal(resp.status_code, 200)
# missing argument
resp = self.post("/api/snapshots/pull", json={
resp = self.post("/api/snapshots/{snapshot_empty_repo}/pull", json={
'Source': snapshot_repo_with_libboost,
'To': snapshot_empty_repo,
'Destination': snapshot_pull_libboost,
})
self.check_equal(resp.status_code, 400)
# dry run, emtpy architectures
resp = self.post("/api/snapshots/pull?dry-run=1", json={
resp = self.post("/api/snapshots/{snapshot_empty_repo}/pull?dry-run=1", json={
'Source': snapshot_repo_with_libboost,
'To': snapshot_empty_repo,
'Destination': snapshot_pull_libboost,
'Queries': [
'libboost-program-options-dev'
@@ -484,9 +480,8 @@ class SnapshotsAPITestPull(APITest):
self.check_equal(resp.status_code, 500)
# dry run, non-existing To
resp = self.post("/api/snapshots/pull", json={
resp = self.post("/api/snapshots/asd123/pull", json={
'Source': snapshot_repo_with_libboost,
'To': "asd123",
'Destination': snapshot_pull_libboost,
'Queries': [
'libboost-program-options-dev'
@@ -495,9 +490,8 @@ class SnapshotsAPITestPull(APITest):
self.check_equal(resp.status_code, 404)
# dry run, non-existing source
resp = self.post("/api/snapshots/pull?dry-run=1", json={
resp = self.post("/api/snapshots/{snapshot_empty_repo}/pull?dry-run=1", json={
'Source': "asd123",
'To': snapshot_empty_repo,
'Destination': snapshot_pull_libboost,
'Queries': [
'libboost-program-options-dev'
@@ -506,9 +500,8 @@ class SnapshotsAPITestPull(APITest):
self.check_equal(resp.status_code, 404)
# snapshot pull
resp = self.post("/api/snapshots/pull", json={
resp = self.post("/api/snapshots/{snapshot_empty_repo}/pull", json={
'Source': snapshot_repo_with_libboost,
'To': snapshot_empty_repo,
'Destination': snapshot_pull_libboost,
'Queries': [
'libboost-program-options-dev'
@@ -532,9 +525,8 @@ class SnapshotsAPITestPull(APITest):
# pull from non-existing source
non_existing_source = self.random_name()
destination = self.random_name()
resp = self.post("/api/snapshots/pull", json={
resp = self.post("/api/snapshots/{snapshot_empty_repo}/pull", json={
'Source': non_existing_source,
'To': snapshot_empty_repo,
'Destination': destination,
'Queries': [
'Name (~ *)'
@@ -549,9 +541,8 @@ class SnapshotsAPITestPull(APITest):
# pull to non-existing snapshot
non_existing_snapshot = self.random_name()
destination = self.random_name()
resp = self.post("/api/snapshots/pull", json={
resp = self.post("/api/snapshots/{snapshot_empty_repo}/pull", json={
'Source': non_existing_snapshot,
'To': snapshot_empty_repo,
'Destination': destination,
'Queries': [
'Name (~ *)'