Fixed body and header issues with app creator

This commit is contained in:
frikky
2020-06-01 10:05:20 +02:00
parent 8cfbb39386
commit 4e10981db5
4 changed files with 109 additions and 89 deletions
+16 -49
View File
@@ -16,7 +16,7 @@ import (
"cloud.google.com/go/storage"
"github.com/getkin/kin-openapi/openapi3"
"github.com/satori/go.uuid"
//"github.com/satori/go.uuid"
"gopkg.in/yaml.v2"
)
@@ -347,7 +347,11 @@ func generateYaml(swagger *openapi3.Swagger, newmd5 string) (WorkflowApp, []stri
api.Name = swagger.Info.Title
api.Description = swagger.Info.Description
api.ID = uuid.NewV4().String()
// FIXME: Versioning issue?
api.ID = newmd5
//uuid.NewV4().String()
api.IsValid = true
api.Link = swagger.Servers[0].URL // host doesnt exist lol
if strings.HasSuffix(api.Link, "/") {
@@ -722,7 +726,7 @@ func handleConnect(swagger *openapi3.Swagger, api WorkflowApp, extraParameters [
optionalParameters := []WorkflowAppActionParameter{}
if len(path.Connect.Parameters) > 0 {
for _, param := range path.Connect.Parameters {
if param.Value.Schema == nil {
if param.Value.Schema == nil || param.Value.In == "header" {
continue
}
curParam := WorkflowAppActionParameter{
@@ -828,8 +832,7 @@ func handleGet(swagger *openapi3.Swagger, api WorkflowApp, extraParameters []Wor
optionalParameters := []WorkflowAppActionParameter{}
if len(path.Get.Parameters) > 0 {
for _, param := range path.Get.Parameters {
//log.Printf("TYPE: %#v", param.Value.Schema)
if param.Value.Schema == nil {
if param.Value.Schema == nil || param.Value.In == "header" {
continue
}
@@ -933,7 +936,7 @@ func handleHead(swagger *openapi3.Swagger, api WorkflowApp, extraParameters []Wo
optionalParameters := []WorkflowAppActionParameter{}
if len(path.Head.Parameters) > 0 {
for _, param := range path.Head.Parameters {
if param.Value.Schema == nil {
if param.Value.Schema == nil || param.Value.In == "header" {
continue
}
curParam := WorkflowAppActionParameter{
@@ -1036,7 +1039,7 @@ func handleDelete(swagger *openapi3.Swagger, api WorkflowApp, extraParameters []
optionalParameters := []WorkflowAppActionParameter{}
if len(path.Delete.Parameters) > 0 {
for _, param := range path.Delete.Parameters {
if param.Value.Schema == nil {
if param.Value.Schema == nil || param.Value.In == "header" {
continue
}
curParam := WorkflowAppActionParameter{
@@ -1140,26 +1143,13 @@ func handlePost(swagger *openapi3.Swagger, api WorkflowApp, extraParameters []Wo
optionalQueries := []string{}
parameters := []string{}
optionalParameters := []WorkflowAppActionParameter{}
/*
WorkflowAppActionParameter{
Name: "body",
Description: "The body to use",
Multiline: true,
Required: false,
Example: `{"username": "test"}`,
Schema: SchemaDefinition{
Type: "string",
},
},
}
*/
if len(path.Post.Parameters) > 0 {
for _, param := range path.Post.Parameters {
if param.Value.Schema == nil {
if param.Value.Schema == nil || param.Value.In == "header" {
continue
}
log.Printf("PARAM: %#v", param.Value)
curParam := WorkflowAppActionParameter{
Name: param.Value.Name,
Description: param.Value.Description,
@@ -1257,21 +1247,10 @@ func handlePatch(swagger *openapi3.Swagger, api WorkflowApp, extraParameters []W
firstQuery = true
optionalQueries := []string{}
parameters := []string{}
optionalParameters := []WorkflowAppActionParameter{
WorkflowAppActionParameter{
Name: "body",
Description: "The body to use",
Multiline: true,
Required: false,
Example: `{"username": "test"}`,
Schema: SchemaDefinition{
Type: "string",
},
},
}
optionalParameters := []WorkflowAppActionParameter{}
if len(path.Patch.Parameters) > 0 {
for _, param := range path.Patch.Parameters {
if param.Value.Schema == nil {
if param.Value.Schema == nil || param.Value.In == "header" {
continue
}
curParam := WorkflowAppActionParameter{
@@ -1372,22 +1351,10 @@ func handlePut(swagger *openapi3.Swagger, api WorkflowApp, extraParameters []Wor
optionalQueries := []string{}
parameters := []string{}
optionalParameters := []WorkflowAppActionParameter{}
/*
WorkflowAppActionParameter{
Name: "body",
Description: "The body to use",
Multiline: true,
Required: false,
Example: `{"username": "test"}`,
Schema: SchemaDefinition{
Type: "string",
},
},
}
*/
if len(path.Put.Parameters) > 0 {
for _, param := range path.Put.Parameters {
if param.Value.Schema == nil {
if param.Value.Schema == nil || param.Value.In == "header" {
continue
}
curParam := WorkflowAppActionParameter{
+21 -13
View File
@@ -2112,7 +2112,6 @@ func SetSession(ctx context.Context, Userdata User, value string) error {
func setOpenApiDatastore(ctx context.Context, id string, data ParsedOpenApi) error {
k := datastore.NameKey("openapi3", id, nil)
if _, err := dbclient.Put(ctx, k, &data); err != nil {
log.Println(err)
return err
}
@@ -5075,6 +5074,8 @@ func getOpenapi(resp http.ResponseWriter, request *http.Request) {
return
}
log.Printf("API LENGTH GET: %d, ID: %s", len(parsedApi.Body), id)
parsedApi.Success = true
data, err := json.Marshal(parsedApi)
if err != nil {
@@ -5505,13 +5506,6 @@ func verifySwagger(resp http.ResponseWriter, request *http.Request) {
// Test = client side with fetch?
ctx := context.Background()
//client, err := storage.NewClient(ctx)
//if err != nil {
// log.Printf("Failed to create client (storage): %v", err)
// resp.WriteHeader(401)
// resp.Write([]byte(`{"success": false, "reason": "Failed creating client"}`))
// return
//}
swagger, err := openapi3.NewSwaggerLoader().LoadSwaggerFromData(body)
if err != nil {
@@ -5542,6 +5536,9 @@ func verifySwagger(resp http.ResponseWriter, request *http.Request) {
return
}
log.Printf("PATHS: %#v", swagger.Paths["/comments/put"])
log.Printf("Functions: %d", len(swagger.Paths))
log.Printf("Actions: %d", len(api.Actions))
api.Owner = user.Id
err = dumpApi(basePath, api)
@@ -5680,24 +5677,35 @@ func verifySwagger(resp http.ResponseWriter, request *http.Request) {
return
}
log.Printf("DO I REACH HERE WHEN SAVING?")
parsed := ParsedOpenApi{
ID: api.ID,
ID: newmd5,
Body: string(body),
}
setOpenApiDatastore(ctx, api.ID, parsed)
err = increaseStatisticsField(ctx, "total_apps_created", api.ID, 1)
log.Printf("API LENGTH: %d, ID: %s", len(parsed.Body), newmd5)
// FIXME: Might cause versioning issues if we re-use the same!!
// FIXME: Need a way to track different versions of the same app properly.
// Hint: Save API.id somewhere, and use newmd5 to save latest version
err = setOpenApiDatastore(ctx, newmd5, parsed)
if err != nil {
log.Printf("Failed saving to datastore: %s", err)
resp.WriteHeader(500)
resp.Write([]byte(fmt.Sprintf(`{"success": true, "reason": "%"}`, err)))
}
err = increaseStatisticsField(ctx, "total_apps_created", newmd5, 1)
if err != nil {
log.Printf("Failed to increase success execution stats: %s", err)
}
err = increaseStatisticsField(ctx, "openapi_apps_created", api.ID, 1)
err = increaseStatisticsField(ctx, "openapi_apps_created", newmd5, 1)
if err != nil {
log.Printf("Failed to increase success execution stats: %s", err)
}
resp.WriteHeader(200)
resp.Write([]byte(`{"success": true}`))
resp.Write([]byte(fmt.Sprintf(`{"success": true, "id": "%s"}`, api.ID)))
}
func healthCheckHandler(resp http.ResponseWriter, request *http.Request) {
+3
View File
@@ -2745,6 +2745,9 @@ func getWorkflowAppConfig(resp http.ResponseWriter, request *http.Request) {
return
}
//log.Printf("%#v", parsedApi)
log.Printf("API LEN: %d, ID: %s", len(parsedApi.Body), fileId)
//log.Printf("Parsed API: %#v", parsedApi)
if len(parsedApi.ID) > 0 {
parsedApi.Success = true