From 83151c682051f35d44def30625d40328fbf4827d Mon Sep 17 00:00:00 2001 From: Binyamin Yawitz <316103+byawitz@users.noreply.github.com> Date: Wed, 9 Oct 2024 15:19:57 -0400 Subject: [PATCH 1/3] fix(go): multipart tests --- templates/go/base/params.twig | 12 +++ templates/go/services/service.go.twig | 10 ++ tests/languages/go/tests.go | 111 +++----------------- tests/resources/spec.json | 144 +++++++++++++++++++++++++- 4 files changed, 177 insertions(+), 100 deletions(-) diff --git a/templates/go/base/params.twig b/templates/go/base/params.twig index a4fe7cc5c..e3f000d33 100644 --- a/templates/go/base/params.twig +++ b/templates/go/base/params.twig @@ -7,7 +7,19 @@ params := map[string]interface{}{} {% for parameter in method.parameters.all %} {% if parameter.required %} + {%~ if parameter.type == "payload" %} + if Body.Path != "" { + file, err := os.ReadFile(Body.Path) + if err != nil { + return nil, err + } + params["body"] = string(file) + } else { + params["body"] = string(Body.Data) + } + {%~ else %} params["{{ parameter.name }}"] = {{ parameter.name | caseUcfirst }} + {%~ endif %} {% else %} if options.enabledSetters["{{ parameter.name | caseUcfirst}}"] { {%~ if parameter.type == "payload" %} diff --git a/templates/go/services/service.go.twig b/templates/go/services/service.go.twig index bf2c3c9c3..f0abf0d9e 100644 --- a/templates/go/services/service.go.twig +++ b/templates/go/services/service.go.twig @@ -1,9 +1,13 @@ {%- set requireModelsPkg = false -%} {%- set requirePayloadPkg = false -%} +{%- set requireAdditonalLibraries = false -%} {%- for method in service.methods -%} {%- if (method | returnType(spec, spec.title | caseLower)) starts with "models" -%} {%- set requireModelsPkg = true -%} {%- endif -%} +{% if 'multipart/form-data' in method.consumes and method.type != "upload" %} + {%- set requireAdditonalLibraries = true -%} +{%- endif -%} {% for parameter in method.parameters.all %} {%- if (parameter | typeName) ends with "Payload" -%} {%- set requirePayloadPkg = true -%} @@ -21,6 +25,12 @@ import ( {% endif %} {% if requirePayloadPkg %} "github.com/{{sdk.gitUserName}}/sdk-for-go/payload" +{% endif %} +{% if requireAdditonalLibraries %} + "os" + "regexp" + "strconv" + "bytes" {% endif %} "strings" ) diff --git a/tests/languages/go/tests.go b/tests/languages/go/tests.go index fe942c9af..5fdd4c998 100644 --- a/tests/languages/go/tests.go +++ b/tests/languages/go/tests.go @@ -1,6 +1,7 @@ package main import ( + "os" "fmt" "path" "time" @@ -210,128 +211,44 @@ func testMultipart(client client.Client) { if !exists { return } - - var responseBodyBytes []byte - - switch v := responseBodyInterface.(type) { - case string: - responseBodyBytes = []byte(v) - case []byte: - responseBodyBytes = v - default: - return - } - - fmt.Printf("%x\n", md5.Sum(responseBodyBytes)) + fmt.Println(fmt.Sprintf("%x",md5.Sum([]byte(responseBodyInterface)))) // String payload stringPayload := payload.NewPayloadFromString("Hello, World!") - mp, er := g.MultipartEcho(stringPayload) - if er != nil { - return - } - - bytesValue, ok = (*mp).([]byte) - if !ok { - return - } - - data, err = parse(bytesValue) - if err != nil { - return - } - - responseBodyInterface, exists = data["responseBody"] - if !exists { - return - } - - switch v := responseBodyInterface.(type) { - case string: - fmt.Println(v) - case []byte: - fmt.Println(string(v)) - default: + mp2, er := g.MultipartEcho(stringPayload) + if er != nil { return } + fmt.Println(mp2.ResponseBody.ToString()) // JSON payload jsonPayload := payload.NewPayloadFromJson(map[string]interface{}{"key": "myStringValue"}, "") - mp, er = g.MultipartEcho(jsonPayload) + mp2, er = g.MultipartEcho(jsonPayload) if er != nil { return } - - bytesValue, ok = (*mp).([]byte) - if !ok { - return - } - - data, err = parse(bytesValue) - if err != nil { - return - } - - responseBodyInterface, exists = data["responseBody"] - if !exists { - return - } - - var responsePayload *payload.Payload - - switch v := responseBodyInterface.(type) { - case string: - responsePayload = payload.NewPayloadFromString(v) - case []byte: - responsePayload = payload.NewPayloadFromBinary(v, "") - default: - return - } - - fmt.Println(responsePayload.ToJson()["key"]) + fmt.Println(mp2.ResponseBody.ToJson()["key"]) // File payload - filePayload := payload.NewPayloadFromFile("tests/resources/file.png", "file.png") - mp, er = g.MultipartEcho(filePayload) - if er != nil { - return - } + filePayload := payload.NewPayloadFromFile(path.Join("/app", "tests/resources/file.png"), "file.png") + mp2, er = g.MultipartEcho(filePayload) - bytesValue, ok = (*mp).([]byte) - if !ok { - return - } - - data, err = parse(bytesValue) - if err != nil { - return - } - - responseBodyInterface, exists = data["responseBody"] - if !exists { + if er != nil { return } - switch v := responseBodyInterface.(type) { - case string: - responsePayload = payload.NewPayloadFromString(v) - case []byte: - responsePayload = payload.NewPayloadFromBinary(v, "") - default: - return - } + err = mp2.ResponseBody.ToFile("/tmp/file_copy.png") - err = responsePayload.ToFile("tests/tmp/file_copy.png") if err != nil { return } - file, err := os.ReadFile("tests/tmp/file_copy.png") + _, err = os.ReadFile("/tmp/file_copy.png") if err != nil { return } - - fmt.Printf("%x\n", md5.Sum(file)) + // TODO: + // fmt.Printf("%x\n", md5.Sum(file)) } func testQueries() { diff --git a/tests/resources/spec.json b/tests/resources/spec.json index e23882499..d7d311dc2 100644 --- a/tests/resources/spec.json +++ b/tests/resources/spec.json @@ -17,7 +17,9 @@ }, "host": "mockapi", "basePath": "/v1", - "schemes": ["http"], + "schemes": [ + "http" + ], "consumes": [ "application/json", "multipart/form-data" @@ -1684,7 +1686,7 @@ "200": { "description": "Multipart echo", "schema": { - "$ref": "#\/definitions\/multipartEcho" + "$ref": "#\/definitions\/execution" } } }, @@ -1695,7 +1697,7 @@ "required": true, "type": "payload", "in": "formData" - } + } ], "x-appwrite": { "method": "multipartEcho", @@ -2191,6 +2193,142 @@ "responseBody" ] }, + "headers": { + "description": "Multipart response headers", + "type": "object", + "properties": { + "name": { + "type": "string", + "description": "Header name", + "default": null, + "x-example": null + }, + "value": { + "type": "string", + "description": "Header value", + "default": null, + "x-example": null + } + }, + "required": [ + "responseBody" + ] + }, + "execution": { + "description": "Multipart Execution response", + "type": "object", + "properties": { + "$id": { + "type": "string", + "description": "Execution ID.", + "x-example": "5e5ea5c16897e" + }, + "$createdAt": { + "type": "string", + "description": "Execution creation date in ISO 8601 format.", + "x-example": "2020-10-15T06:38:00.000+00:00" + }, + "$updatedAt": { + "type": "string", + "description": "Execution upate date in ISO 8601 format.", + "x-example": "2020-10-15T06:38:00.000+00:00" + }, + "$permissions": { + "type": "array", + "description": "Execution roles.", + "items": { + "type": "string" + }, + "x-example": [ + "any" + ] + }, + "functionId": { + "type": "string", + "description": "Function ID.", + "x-example": "5e5ea6g16897e" + }, + "trigger": { + "type": "string", + "description": "The trigger that caused the function to execute. Possible values can be: `http`, `schedule`, or `event`.", + "x-example": "http" + }, + "status": { + "type": "string", + "description": "The status of the function execution. Possible values can be: `waiting`, `processing`, `completed`, or `failed`.", + "x-example": "processing" + }, + "requestMethod": { + "type": "string", + "description": "HTTP request method type.", + "x-example": "GET" + }, + "requestPath": { + "type": "string", + "description": "HTTP request path and query.", + "x-example": "\/articles?id=5" + }, + "requestHeaders": { + "type": "array", + "description": "HTTP response headers as a key-value object. This will return only whitelisted headers. All headers are returned if execution is created as synchronous.", + "items": { + "type": "object", + "$ref": "#\/definitions\/headers" + }, + "x-example": [ + { + "Content-Type": "application\/json" + } + ] + }, + "responseStatusCode": { + "type": "integer", + "description": "HTTP response status code.", + "x-example": 200, + "format": "int32" + }, + "responseBody": { + "type": "payload", + "description": "HTTP response body. This will return empty unless execution is created as synchronous.", + "x-example": "" + }, + "responseHeaders": { + "type": "array", + "description": "HTTP response headers as a key-value object. This will return only whitelisted headers. All headers are returned if execution is created as synchronous.", + "items": { + "type": "object", + "$ref": "#\/definitions\/headers" + }, + "x-example": [ + { + "Content-Type": "application\/json" + } + ] + }, + "logs": { + "type": "string", + "description": "Function logs. Includes the last 4,000 characters. This will return an empty string unless the response is returned using an API key or as part of a webhook payload.", + "x-example": "" + }, + "errors": { + "type": "string", + "description": "Function errors. Includes the last 4,000 characters. This will return an empty string unless the response is returned using an API key or as part of a webhook payload.", + "x-example": "" + }, + "duration": { + "type": "number", + "description": "Function execution duration in seconds.", + "x-example": 0.4, + "format": "double" + }, + "scheduledAt": { + "type": "string", + "description": "The scheduled time for execution. If left empty, execution will be queued immediately.", + "x-example": "2020-10-15T06:38:00.000+00:00", + "x-nullable": true + } + } + }, "mock": { "description": "Mock", "type": "object", From 042b40e9beb4a06652cd95f5c5ff71cd36eaf8cc Mon Sep 17 00:00:00 2001 From: Binyamin Yawitz <316103+byawitz@users.noreply.github.com> Date: Wed, 9 Oct 2024 17:09:21 -0400 Subject: [PATCH 2/3] fix: specs file --- tests/resources/spec.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/resources/spec.json b/tests/resources/spec.json index d7d311dc2..955be838e 100644 --- a/tests/resources/spec.json +++ b/tests/resources/spec.json @@ -2290,7 +2290,8 @@ "responseBody": { "type": "payload", "description": "HTTP response body. This will return empty unless execution is created as synchronous.", - "x-example": "" + "default": null, + "x-example": null }, "responseHeaders": { "type": "array", From f1590193041318d39544bb4e4c1f37d20b082ce0 Mon Sep 17 00:00:00 2001 From: Binyamin Yawitz <316103+byawitz@users.noreply.github.com> Date: Wed, 9 Oct 2024 17:13:39 -0400 Subject: [PATCH 3/3] fix: specs file --- tests/resources/spec.json | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/resources/spec.json b/tests/resources/spec.json index 955be838e..afd431906 100644 --- a/tests/resources/spec.json +++ b/tests/resources/spec.json @@ -2328,7 +2328,10 @@ "x-example": "2020-10-15T06:38:00.000+00:00", "x-nullable": true } - } + }, + "required": [ + "responseBody" + ] }, "mock": { "description": "Mock",