Update swagger doc. Indicate to user if updated or created

This commit is contained in:
James Jeffrey
2017-07-05 10:10:16 -07:00
parent 2832c5adec
commit c66a0d0cc4
2 changed files with 70 additions and 14 deletions

View File

@@ -24,9 +24,9 @@ import (
func (s *Server) handleRouteCreateOrUpdate(c *gin.Context) {
ctx := c.MustGet("ctx").(context.Context)
log := common.Logger(ctx)
method := strings.ToLower(c.Request.Method)
method := strings.ToUpper(c.Request.Method)
switch method {
case "post", "put", "patch":
case http.MethodPost, http.MethodPut, http.MethodPatch:
default:
c.JSON(http.StatusMethodNotAllowed, simpleError(fmt.Errorf(http.StatusText(http.StatusMethodNotAllowed))))
return
@@ -48,7 +48,7 @@ func (s *Server) handleRouteCreateOrUpdate(c *gin.Context) {
wroute.Route.AppName = c.MustGet(api.AppName).(string)
if method == "put" || method == "patch" {
if method == http.MethodPut || method == http.MethodPatch {
p := path.Clean(c.MustGet(api.Path).(string))
if wroute.Route.Path != "" && wroute.Route.Path != p {
@@ -61,14 +61,16 @@ func (s *Server) handleRouteCreateOrUpdate(c *gin.Context) {
wroute.Route.SetDefaults()
if err = wroute.Validate(method == "post"); err != nil {
if err = wroute.Validate(method == http.MethodPost); err != nil {
log.WithError(err).Debug(models.ErrRoutesCreate)
c.JSON(http.StatusBadRequest, simpleError(err))
return
}
if method == "post" || method == "put" {
app, err := s.Datastore.GetApp(ctx, wroute.Route.AppName)
//Create the app if it does not exist.
if method == http.MethodPost || method == http.MethodPut {
var app *models.App
app, err = s.Datastore.GetApp(ctx, wroute.Route.AppName)
if err != nil && err != models.ErrAppsNotFound {
log.WithError(err).Error(models.ErrAppsGet)
c.JSON(http.StatusInternalServerError, simpleError(models.ErrAppsGet))
@@ -108,17 +110,22 @@ func (s *Server) handleRouteCreateOrUpdate(c *gin.Context) {
var route *models.Route
var createdOrUpdated int
switch method {
case "post":
case http.MethodPost:
route, err = s.Datastore.InsertRoute(ctx, wroute.Route)
case "put":
createdOrUpdated = 1
case http.MethodPut:
route, err = s.Datastore.UpdateRoute(ctx, wroute.Route)
createdOrUpdated = 2
if err == models.ErrRoutesNotFound {
// try insert then
route, err = s.Datastore.InsertRoute(ctx, wroute.Route)
createdOrUpdated = 1
}
case "patch":
case http.MethodPatch:
route, err = s.Datastore.UpdateRoute(ctx, wroute.Route)
createdOrUpdated = 2
}
if err != nil {
@@ -128,5 +135,15 @@ func (s *Server) handleRouteCreateOrUpdate(c *gin.Context) {
s.cacheRefresh(route)
c.JSON(http.StatusOK, routeResponse{"Route successfully created", route})
var msg string
var code int
switch createdOrUpdated {
case 1:
msg = "Route successfully created"
code = http.StatusCreated
case 2:
msg = "Route successfully updated"
code = http.StatusOK
}
c.JSON(code, routeResponse{msg, route})
}

View File

@@ -6,7 +6,7 @@ swagger: '2.0'
info:
title: Oracle Functions
description: The open source serverless platform.
version: "0.1.33"
version: "0.1.34"
# the domain of the service
host: "127.0.0.1:8080"
# array of all schemes that your API supports
@@ -177,7 +177,7 @@ paths:
schema:
$ref: '#/definitions/RouteWrapper'
responses:
200:
201:
description: Route created
schema:
$ref: '#/definitions/RouteWrapper'
@@ -220,8 +220,47 @@ paths:
$ref: '#/definitions/Error'
/apps/{app}/routes/{route}:
put:
summary: Create a Route if it does not exist. Update if it does. Will also create app if it does not exist.
description: Update or Create a route
tags:
- Routes
parameters:
- name: app
in: path
description: name of the app.
required: true
type: string
- name: route
in: path
description: route path.
required: true
type: string
- name: body
in: body
description: One route to post.
required: true
schema:
$ref: '#/definitions/RouteWrapper'
responses:
200:
description: Route updated
schema:
$ref: '#/definitions/RouteWrapper'
201:
description: Route created
schema:
$ref: '#/definitions/RouteWrapper'
400:
description: Invalid route due to parameters being missing or invalid.
schema:
$ref: '#/definitions/Error'
default:
description: Unexpected error
schema:
$ref: '#/definitions/Error'
patch:
summary: Update a Route
summary: Update a Route, Fails if the route or app does not exist.
description: Update a route
tags:
- Routes
@@ -252,7 +291,7 @@ paths:
schema:
$ref: '#/definitions/Error'
404:
description: App does not exist.
description: App/ Route does not exist.
schema:
$ref: '#/definitions/Error'
default: