diff --git a/ballerina-tests/main.bal b/ballerina-tests/main.bal index a794f4b5..4a75cfcf 100644 --- a/ballerina-tests/main.bal +++ b/ballerina-tests/main.bal @@ -16,51 +16,83 @@ service "hello" on ep { resource function post .(@af:Payload string greeting) returns @af:HTTPOutput string { return "Hello from . path "; } + resource function post httpResTest1(@af:Payload string greeting) returns @af:HTTPOutput af:Unauthorized { + af:Unauthorized unauth = { + body: "Helloworld.....", + mediaType: "application/account+json", + headers: { + "Location": "/myServer/084230" + } + }; + return unauth; + } + + resource function post httpResTest2(@af:Payload string greeting) returns @af:HTTPOutput af:Ok { + af:Ok ok = {body: "Helloworld....."}; + return ok; + } + resource function post httpResTest3(@af:Payload string greeting) returns @af:HTTPOutput af:InternalServerError { + af:InternalServerError err = { + body: "Helloworld.....", + headers: { + "content-type": "application/json+id", + "Location": "/myServer/084230" + } + }; + return err; + } + resource function post httpResTest4(@af:Payload string greeting) returns @af:HTTPOutput af:InternalServerError { + af:InternalServerError err = {}; + return err; + } resource function post foo(@af:Payload string greeting) returns @af:HTTPOutput string { return "Hello from foo path " + greeting; } - resource function post foo/[string bar](@af:Payload string greeting) returns @af:HTTPOutput string { + resource function post foo/[string bar](@af:Payload string greeting) returns @af:HTTPOutput string { return "Hello from foo param " + bar; } - resource function post foo/bar(@af:Payload string greeting) returns @af:HTTPOutput string { + resource function post foo/bar(@af:Payload string greeting) returns @af:HTTPOutput string { return "Hello from foo bar res"; } resource function post query(string name, @af:Payload string greeting) returns @af:HTTPOutput string|error { - return "Hello from the query " + greeting + " " + name; + return "Hello from the query " + greeting + " " + name; } resource function post db(@af:Payload string greeting, @af:CosmosDBInput { - connectionStringSetting: "CosmosDBConnection",databaseName: "db1", - collectionName: "c2", sqlQuery: "SELECT * FROM Items"} DBEntry[] input1) returns @af:HTTPOutput string|error { - return "Hello " + greeting + input1[0].id; + connectionStringSetting: "CosmosDBConnection", + databaseName: "db1", + collectionName: "c2", + sqlQuery: "SELECT * FROM Items" + } DBEntry[] input1) returns @af:HTTPOutput string|error { + return "Hello " + greeting + input1[0].id; } - resource function post payload/jsonToRecord (@af:Payload Person greeting) returns @af:HTTPOutput string|error { + resource function post payload/jsonToRecord(@af:Payload Person greeting) returns @af:HTTPOutput string|error { return "Hello from json to record " + greeting.name; } - resource function post payload/jsonToJson (@af:Payload json greeting) returns @af:HTTPOutput string|error { + resource function post payload/jsonToJson(@af:Payload json greeting) returns @af:HTTPOutput string|error { string name = check greeting.name; - return "Hello from json to json "+ name; + return "Hello from json to json " + name; } - resource function post payload/xmlToXml (@af:Payload xml greeting) returns @af:HTTPOutput string|error { + resource function post payload/xmlToXml(@af:Payload xml greeting) returns @af:HTTPOutput string|error { return greeting.toJsonString(); } - resource function post payload/textToString (@af:Payload string greeting) returns @af:HTTPOutput string|error { + resource function post payload/textToString(@af:Payload string greeting) returns @af:HTTPOutput string|error { return greeting; } - resource function post payload/textToByte (@af:Payload byte[] greeting) returns @af:HTTPOutput string|error { + resource function post payload/textToByte(@af:Payload byte[] greeting) returns @af:HTTPOutput string|error { return string:fromBytes(greeting); } - resource function post payload/octaToByte (@af:Payload byte[] greeting) returns @af:HTTPOutput string|error { + resource function post payload/octaToByte(@af:Payload byte[] greeting) returns @af:HTTPOutput string|error { return string:fromBytes(greeting); } } @@ -69,8 +101,8 @@ service "hello" on ep { queueName: "queue2" } service "queue" on new af:QueueListener() { - remote function onMessage (@af:Payload string inMsg) returns @af:QueueOutput {queueName: "queue3"} string|error { - return "helloo "+ inMsg; + remote function onMessage(@af:Payload string inMsg) returns @af:QueueOutput {queueName: "queue3"} string|error { + return "helloo " + inMsg; } } @@ -78,17 +110,18 @@ service "queue" on new af:QueueListener() { listener af:CosmosDBListener cosmosEp = new (); service "cosmos" on cosmosEp { - remote function onUpdated (@af:Payload DBEntry[] inMsg) returns @af:QueueOutput {queueName: "queue3"} string|error { + remote function onUpdated(@af:Payload DBEntry[] inMsg) returns @af:QueueOutput {queueName: "queue3"} string|error { string id = inMsg[0].id; - return "helloo "+ id; + return "helloo " + id; } } -@af:TimerTrigger { schedule: "*/10 * * * * *" } +@af:TimerTrigger {schedule: "*/10 * * * * *"} listener af:TimerListener timerListener = new af:TimerListener(); + service "timer" on timerListener { - remote function onTrigger (@af:Payload af:TimerMetadata inMsg) returns @af:QueueOutput {queueName: "queue3"} string|error { - return "helloo "+ inMsg.IsPastDue.toString(); + remote function onTrigger(@af:Payload af:TimerMetadata inMsg) returns @af:QueueOutput {queueName: "queue3"} string|error { + return "helloo " + inMsg.IsPastDue.toString(); } } @@ -97,11 +130,15 @@ service "timer" on timerListener { } listener af:QueueListener queueListener1 = new af:QueueListener(); + service "queue-input" on queueListener1 { - remote function onMessage (@af:Payload string inMsg, @af:CosmosDBInput { - connectionStringSetting: "CosmosDBConnection",databaseName: "db1", - collectionName: "c2", sqlQuery: "SELECT * FROM Items"} DBEntry[] input1) returns @af:QueueOutput {queueName: "queue3"} string|error { - return "helloo "+ inMsg + " " +input1[0].id; + remote function onMessage(@af:Payload string inMsg, @af:CosmosDBInput { + connectionStringSetting: "CosmosDBConnection", + databaseName: "db1", + collectionName: "c2", + sqlQuery: "SELECT * FROM Items" + } DBEntry[] input1) returns @af:QueueOutput {queueName: "queue3"} string|error { + return "helloo " + inMsg + " " + input1[0].id; } } diff --git a/ballerina-tests/tests/resources/httpResTest1.json b/ballerina-tests/tests/resources/httpResTest1.json new file mode 100644 index 00000000..5134bc83 --- /dev/null +++ b/ballerina-tests/tests/resources/httpResTest1.json @@ -0,0 +1,108 @@ +{ + "Data": { + "httpPayload": { + "Url": "https://bal-dev.azurewebsites.net/api/hello/httpResTest1", + "Method": "POST", + "Query": {}, + "Headers": { + "Accept": [ + "*/*" + ], + "Content-Length": [ + "4" + ], + "Content-Type": [ + "application/x-www-form-urlencoded" + ], + "Host": [ + "bal-dev.azurewebsites.net" + ], + "Max-Forwards": [ + "10" + ], + "User-Agent": [ + "curl/7.78.0" + ], + "X-ARR-LOG-ID": [ + "28d97039-ef3e-4e6f-948b-680f7ff166f7" + ], + "CLIENT-IP": [ + "112.134.128.105:41856" + ], + "DISGUISED-HOST": [ + "bal-dev.azurewebsites.net" + ], + "X-SITE-DEPLOYMENT-ID": [ + "bal-dev" + ], + "WAS-DEFAULT-HOSTNAME": [ + "bal-dev.azurewebsites.net" + ], + "X-Forwarded-Proto": [ + "https" + ], + "X-AppService-Proto": [ + "https" + ], + "X-ARR-SSL": [ + "2048|256|CN=Microsoft Azure TLS Issuing CA 01, O=Microsoft Corporation, C=US|CN=*.azurewebsites.net, O=Microsoft Corporation, L=Redmond, S=WA, C=US" + ], + "X-Forwarded-TlsVersion": [ + "1.2" + ], + "X-Forwarded-For": [ + "112.134.128.105:41856" + ], + "X-Original-URL": [ + "/api/hello/httpResTest1" + ], + "X-WAWS-Unencoded-URL": [ + "/api/hello/httpResTest1" + ] + }, + "Params": {}, + "Identities": [ + { + "AuthenticationType": null, + "IsAuthenticated": false, + "Actor": null, + "BootstrapContext": null, + "Claims": [], + "Label": null, + "Name": null, + "NameClaimType": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name", + "RoleClaimType": "http://schemas.microsoft.com/ws/2008/06/identity/claims/role" + } + ], + "Body": "Jack" + } + }, + "Metadata": { + "Query": {}, + "Headers": { + "Accept": "*/*", + "Content-Length": "4", + "Content-Type": "application/x-www-form-urlencoded", + "Host": "bal-dev.azurewebsites.net", + "Max-Forwards": "10", + "User-Agent": "curl/7.78.0", + "X-ARR-LOG-ID": "28d97039-ef3e-4e6f-948b-680f7ff166f7", + "CLIENT-IP": "112.134.128.105:41856", + "DISGUISED-HOST": "bal-dev.azurewebsites.net", + "X-SITE-DEPLOYMENT-ID": "bal-dev", + "WAS-DEFAULT-HOSTNAME": "bal-dev.azurewebsites.net", + "X-Forwarded-Proto": "https", + "X-AppService-Proto": "https", + "X-ARR-SSL": "2048|256|CN=Microsoft Azure TLS Issuing CA 01, O=Microsoft Corporation, C=US|CN=*.azurewebsites.net, O=Microsoft Corporation, L=Redmond, S=WA, C=US", + "X-Forwarded-TlsVersion": "1.2", + "X-Forwarded-For": "112.134.128.105:41856", + "X-Original-URL": "/api/hello/httpResTest1", + "X-WAWS-Unencoded-URL": "/api/hello/httpResTest1" + }, + "sys": { + "MethodName": "post-hello-httpResTest1", + "UtcNow": "2022-06-10T07:10:30.1722785Z", + "RandGuid": "19f5a752-e046-4f7f-964f-e53207baed7b" + } + } +} diff --git a/ballerina-tests/tests/resources/httpResTest2.json b/ballerina-tests/tests/resources/httpResTest2.json new file mode 100644 index 00000000..a39a835e --- /dev/null +++ b/ballerina-tests/tests/resources/httpResTest2.json @@ -0,0 +1,108 @@ +{ + "Data": { + "httpPayload": { + "Url": "https://bal-dev.azurewebsites.net/api/hello/httpResTest2", + "Method": "POST", + "Query": {}, + "Headers": { + "Accept": [ + "*/*" + ], + "Content-Length": [ + "4" + ], + "Content-Type": [ + "application/x-www-form-urlencoded" + ], + "Host": [ + "bal-dev.azurewebsites.net" + ], + "Max-Forwards": [ + "10" + ], + "User-Agent": [ + "curl/7.78.0" + ], + "X-ARR-LOG-ID": [ + "28d97039-ef3e-4e6f-948b-680f7ff166f7" + ], + "CLIENT-IP": [ + "112.134.128.105:41856" + ], + "DISGUISED-HOST": [ + "bal-dev.azurewebsites.net" + ], + "X-SITE-DEPLOYMENT-ID": [ + "bal-dev" + ], + "WAS-DEFAULT-HOSTNAME": [ + "bal-dev.azurewebsites.net" + ], + "X-Forwarded-Proto": [ + "https" + ], + "X-AppService-Proto": [ + "https" + ], + "X-ARR-SSL": [ + "2048|256|CN=Microsoft Azure TLS Issuing CA 01, O=Microsoft Corporation, C=US|CN=*.azurewebsites.net, O=Microsoft Corporation, L=Redmond, S=WA, C=US" + ], + "X-Forwarded-TlsVersion": [ + "1.2" + ], + "X-Forwarded-For": [ + "112.134.128.105:41856" + ], + "X-Original-URL": [ + "/api/hello/httpResTest2" + ], + "X-WAWS-Unencoded-URL": [ + "/api/hello/httpResTest2" + ] + }, + "Params": {}, + "Identities": [ + { + "AuthenticationType": null, + "IsAuthenticated": false, + "Actor": null, + "BootstrapContext": null, + "Claims": [], + "Label": null, + "Name": null, + "NameClaimType": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name", + "RoleClaimType": "http://schemas.microsoft.com/ws/2008/06/identity/claims/role" + } + ], + "Body": "Jack" + } + }, + "Metadata": { + "Query": {}, + "Headers": { + "Accept": "*/*", + "Content-Length": "4", + "Content-Type": "application/x-www-form-urlencoded", + "Host": "bal-dev.azurewebsites.net", + "Max-Forwards": "10", + "User-Agent": "curl/7.78.0", + "X-ARR-LOG-ID": "28d97039-ef3e-4e6f-948b-680f7ff166f7", + "CLIENT-IP": "112.134.128.105:41856", + "DISGUISED-HOST": "bal-dev.azurewebsites.net", + "X-SITE-DEPLOYMENT-ID": "bal-dev", + "WAS-DEFAULT-HOSTNAME": "bal-dev.azurewebsites.net", + "X-Forwarded-Proto": "https", + "X-AppService-Proto": "https", + "X-ARR-SSL": "2048|256|CN=Microsoft Azure TLS Issuing CA 01, O=Microsoft Corporation, C=US|CN=*.azurewebsites.net, O=Microsoft Corporation, L=Redmond, S=WA, C=US", + "X-Forwarded-TlsVersion": "1.2", + "X-Forwarded-For": "112.134.128.105:41856", + "X-Original-URL": "/api/hello/httpResTest2", + "X-WAWS-Unencoded-URL": "/api/hello/httpResTest2" + }, + "sys": { + "MethodName": "post-hello-httpResTest2", + "UtcNow": "2022-06-10T07:10:30.1722785Z", + "RandGuid": "19f5a752-e046-4f7f-964f-e53207baed7b" + } + } +} diff --git a/ballerina-tests/tests/resources/httpResTest3.json b/ballerina-tests/tests/resources/httpResTest3.json new file mode 100644 index 00000000..5f998c76 --- /dev/null +++ b/ballerina-tests/tests/resources/httpResTest3.json @@ -0,0 +1,108 @@ +{ + "Data": { + "httpPayload": { + "Url": "https://bal-dev.azurewebsites.net/api/hello/httpResTest3", + "Method": "POST", + "Query": {}, + "Headers": { + "Accept": [ + "*/*" + ], + "Content-Length": [ + "4" + ], + "Content-Type": [ + "application/x-www-form-urlencoded" + ], + "Host": [ + "bal-dev.azurewebsites.net" + ], + "Max-Forwards": [ + "10" + ], + "User-Agent": [ + "curl/7.78.0" + ], + "X-ARR-LOG-ID": [ + "28d97039-ef3e-4e6f-948b-680f7ff166f7" + ], + "CLIENT-IP": [ + "112.134.128.105:41856" + ], + "DISGUISED-HOST": [ + "bal-dev.azurewebsites.net" + ], + "X-SITE-DEPLOYMENT-ID": [ + "bal-dev" + ], + "WAS-DEFAULT-HOSTNAME": [ + "bal-dev.azurewebsites.net" + ], + "X-Forwarded-Proto": [ + "https" + ], + "X-AppService-Proto": [ + "https" + ], + "X-ARR-SSL": [ + "2048|256|CN=Microsoft Azure TLS Issuing CA 01, O=Microsoft Corporation, C=US|CN=*.azurewebsites.net, O=Microsoft Corporation, L=Redmond, S=WA, C=US" + ], + "X-Forwarded-TlsVersion": [ + "1.2" + ], + "X-Forwarded-For": [ + "112.134.128.105:41856" + ], + "X-Original-URL": [ + "/api/hello/httpResTest3" + ], + "X-WAWS-Unencoded-URL": [ + "/api/hello/httpResTest3" + ] + }, + "Params": {}, + "Identities": [ + { + "AuthenticationType": null, + "IsAuthenticated": false, + "Actor": null, + "BootstrapContext": null, + "Claims": [], + "Label": null, + "Name": null, + "NameClaimType": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name", + "RoleClaimType": "http://schemas.microsoft.com/ws/2008/06/identity/claims/role" + } + ], + "Body": "Jack" + } + }, + "Metadata": { + "Query": {}, + "Headers": { + "Accept": "*/*", + "Content-Length": "4", + "Content-Type": "application/x-www-form-urlencoded", + "Host": "bal-dev.azurewebsites.net", + "Max-Forwards": "10", + "User-Agent": "curl/7.78.0", + "X-ARR-LOG-ID": "28d97039-ef3e-4e6f-948b-680f7ff166f7", + "CLIENT-IP": "112.134.128.105:41856", + "DISGUISED-HOST": "bal-dev.azurewebsites.net", + "X-SITE-DEPLOYMENT-ID": "bal-dev", + "WAS-DEFAULT-HOSTNAME": "bal-dev.azurewebsites.net", + "X-Forwarded-Proto": "https", + "X-AppService-Proto": "https", + "X-ARR-SSL": "2048|256|CN=Microsoft Azure TLS Issuing CA 01, O=Microsoft Corporation, C=US|CN=*.azurewebsites.net, O=Microsoft Corporation, L=Redmond, S=WA, C=US", + "X-Forwarded-TlsVersion": "1.2", + "X-Forwarded-For": "112.134.128.105:41856", + "X-Original-URL": "/api/hello/httpResTest3", + "X-WAWS-Unencoded-URL": "/api/hello/httpResTest3" + }, + "sys": { + "MethodName": "post-hello-httpResTest3", + "UtcNow": "2022-06-10T07:10:30.1722785Z", + "RandGuid": "19f5a752-e046-4f7f-964f-e53207baed7b" + } + } +} diff --git a/ballerina-tests/tests/resources/httpResTest4.json b/ballerina-tests/tests/resources/httpResTest4.json new file mode 100644 index 00000000..5c59be97 --- /dev/null +++ b/ballerina-tests/tests/resources/httpResTest4.json @@ -0,0 +1,108 @@ +{ + "Data": { + "httpPayload": { + "Url": "https://bal-dev.azurewebsites.net/api/hello/httpResTest4", + "Method": "POST", + "Query": {}, + "Headers": { + "Accept": [ + "*/*" + ], + "Content-Length": [ + "4" + ], + "Content-Type": [ + "application/x-www-form-urlencoded" + ], + "Host": [ + "bal-dev.azurewebsites.net" + ], + "Max-Forwards": [ + "10" + ], + "User-Agent": [ + "curl/7.78.0" + ], + "X-ARR-LOG-ID": [ + "28d97039-ef3e-4e6f-948b-680f7ff166f7" + ], + "CLIENT-IP": [ + "112.134.128.105:41856" + ], + "DISGUISED-HOST": [ + "bal-dev.azurewebsites.net" + ], + "X-SITE-DEPLOYMENT-ID": [ + "bal-dev" + ], + "WAS-DEFAULT-HOSTNAME": [ + "bal-dev.azurewebsites.net" + ], + "X-Forwarded-Proto": [ + "https" + ], + "X-AppService-Proto": [ + "https" + ], + "X-ARR-SSL": [ + "2048|256|CN=Microsoft Azure TLS Issuing CA 01, O=Microsoft Corporation, C=US|CN=*.azurewebsites.net, O=Microsoft Corporation, L=Redmond, S=WA, C=US" + ], + "X-Forwarded-TlsVersion": [ + "1.2" + ], + "X-Forwarded-For": [ + "112.134.128.105:41856" + ], + "X-Original-URL": [ + "/api/hello/httpResTest4" + ], + "X-WAWS-Unencoded-URL": [ + "/api/hello/httpResTest4" + ] + }, + "Params": {}, + "Identities": [ + { + "AuthenticationType": null, + "IsAuthenticated": false, + "Actor": null, + "BootstrapContext": null, + "Claims": [], + "Label": null, + "Name": null, + "NameClaimType": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name", + "RoleClaimType": "http://schemas.microsoft.com/ws/2008/06/identity/claims/role" + } + ], + "Body": "Jack" + } + }, + "Metadata": { + "Query": {}, + "Headers": { + "Accept": "*/*", + "Content-Length": "4", + "Content-Type": "application/x-www-form-urlencoded", + "Host": "bal-dev.azurewebsites.net", + "Max-Forwards": "10", + "User-Agent": "curl/7.78.0", + "X-ARR-LOG-ID": "28d97039-ef3e-4e6f-948b-680f7ff166f7", + "CLIENT-IP": "112.134.128.105:41856", + "DISGUISED-HOST": "bal-dev.azurewebsites.net", + "X-SITE-DEPLOYMENT-ID": "bal-dev", + "WAS-DEFAULT-HOSTNAME": "bal-dev.azurewebsites.net", + "X-Forwarded-Proto": "https", + "X-AppService-Proto": "https", + "X-ARR-SSL": "2048|256|CN=Microsoft Azure TLS Issuing CA 01, O=Microsoft Corporation, C=US|CN=*.azurewebsites.net, O=Microsoft Corporation, L=Redmond, S=WA, C=US", + "X-Forwarded-TlsVersion": "1.2", + "X-Forwarded-For": "112.134.128.105:41856", + "X-Original-URL": "/api/hello/httpResTest4", + "X-WAWS-Unencoded-URL": "/api/hello/httpResTest4" + }, + "sys": { + "MethodName": "post-hello-httpResTest4", + "UtcNow": "2022-06-10T07:10:30.1722785Z", + "RandGuid": "19f5a752-e046-4f7f-964f-e53207baed7b" + } + } +} diff --git a/ballerina-tests/tests/test.bal b/ballerina-tests/tests/test.bal index 256bf876..f66a3620 100644 --- a/ballerina-tests/tests/test.bal +++ b/ballerina-tests/tests/test.bal @@ -2,157 +2,232 @@ import ballerina/test; import ballerina/io; import ballerina/http; -@test:Config { } +@test:Config {} function testBaseDot() returns error? { - final http:Client clientEndpoint = check new ("http://localhost:3000"); - string jsonFilePath = "./tests/resources/base-dot.json"; - json readJson = check io:fileReadJson(jsonFilePath); - json resp = check clientEndpoint->post("/post-hello", readJson); - json expectedResp = {"Outputs":{"resp":{"body":"Hello from . path "}},"Logs":[],"ReturnValue":null}; - test:assertEquals(resp, expectedResp); + final http:Client clientEndpoint = check new ("http://localhost:3000"); + string jsonFilePath = "./tests/resources/base-dot.json"; + json readJson = check io:fileReadJson(jsonFilePath); + json resp = check clientEndpoint->post("/post-hello", readJson); + json expectedResp = {"Outputs": {"resp": {"body": "Hello from . path "}}, "Logs": [], "ReturnValue": null}; + test:assertEquals(resp, expectedResp); +} + +@test:Config {} +function httpResTest1() returns error? { + final http:Client clientEndpoint = check new ("http://localhost:3000"); + string jsonFilePath = "./tests/resources/httpResTest1.json"; + json readJson = check io:fileReadJson(jsonFilePath); + json resp = check clientEndpoint->post("/post-hello-httpResTest1", readJson); + json expectedResp = { + "Outputs": { + "resp": { + "statusCode": "401", + "body": "Helloworld.....", + "headers": {"Location": "/myServer/084230", "content-type": "application/account+json"} + } + }, + "Logs": [], + "ReturnValue": null + }; + test:assertEquals(resp, expectedResp); } +@test:Config {} +function httpResTest2() returns error? { + final http:Client clientEndpoint = check new ("http://localhost:3000"); + string jsonFilePath = "./tests/resources/httpResTest2.json"; + json readJson = check io:fileReadJson(jsonFilePath); + json resp = check clientEndpoint->post("/post-hello-httpResTest2", readJson); + json expectedResp = { + "Outputs": { + "resp": { + "statusCode": "200", + "body": "Helloworld.....", + "headers": {"content-type": "text/plain"} + } + }, + "Logs": [], + "ReturnValue": null + }; + test:assertEquals(resp, expectedResp); +} -@test:Config { } +@test:Config {} +function httpResTest3() returns error? { + final http:Client clientEndpoint = check new ("http://localhost:3000"); + string jsonFilePath = "./tests/resources/httpResTest3.json"; + json readJson = check io:fileReadJson(jsonFilePath); + json resp = check clientEndpoint->post("/post-hello-httpResTest3", readJson); + json expectedResp = { + "Outputs": { + "resp": { + "statusCode": "500", + "body": "Helloworld.....", + "headers": {"content-type": "application/json+id", "Location": "/myServer/084230"} + } + }, + "Logs": [], + "ReturnValue": null + }; + test:assertEquals(resp, expectedResp); +} + +@test:Config {} +function httpResTest4() returns error? { + final http:Client clientEndpoint = check new ("http://localhost:3000"); + string jsonFilePath = "./tests/resources/httpResTest4.json"; + json readJson = check io:fileReadJson(jsonFilePath); + json resp = check clientEndpoint->post("/post-hello-httpResTest4", readJson); + json expectedResp = { + "Outputs": { + "resp": { + "statusCode": "500", + "headers": {"content-type": "text/plain"} + } + }, + "Logs": [], + "ReturnValue": null + }; + test:assertEquals(resp, expectedResp); +} + +@test:Config {} function testSimpleResourcePath() returns error? { final http:Client clientEndpoint = check new ("http://localhost:3000"); string jsonFilePath = "./tests/resources/res-path.json"; json readJson = check io:fileReadJson(jsonFilePath); json resp = check clientEndpoint->post("/post-hello-foo", readJson); - json expectedResp = {"Outputs":{"resp":{"body":"Hello from foo path Jack"}},"Logs":[],"ReturnValue":null}; + json expectedResp = {"Outputs": {"resp": {"body": "Hello from foo path Jack"}}, "Logs": [], "ReturnValue": null}; test:assertEquals(resp, expectedResp); } -@test:Config { } +@test:Config {} function testSimpleMultiResourcePath() returns error? { final http:Client clientEndpoint = check new ("http://localhost:3000"); string jsonFilePath = "./tests/resources/res-path-param.json"; json readJson = check io:fileReadJson(jsonFilePath); json resp = check clientEndpoint->post("/post-hello-foo-bar", readJson); - json expectedResp = {"Outputs":{"resp":{"body":"Hello from foo bar res"}},"Logs":[],"ReturnValue":null}; + json expectedResp = {"Outputs": {"resp": {"body": "Hello from foo bar res"}}, "Logs": [], "ReturnValue": null}; test:assertEquals(resp, expectedResp); } - -@test:Config { } +@test:Config {} function testSimpleMultiQueryPath() returns error? { final http:Client clientEndpoint = check new ("http://localhost:3000"); string jsonFilePath = "./tests/resources/query-param.json"; json readJson = check io:fileReadJson(jsonFilePath); json resp = check clientEndpoint->post("/post-hello-query", readJson); - json expectedResp = {"Outputs":{"resp":{"body":"Hello from the query Jack test1"}},"Logs":[],"ReturnValue":null}; + json expectedResp = {"Outputs": {"resp": {"body": "Hello from the query Jack test1"}}, "Logs": [], "ReturnValue": null}; test:assertEquals(resp, expectedResp); } - -@test:Config { } +@test:Config {} function testSimpleQueue() returns error? { final http:Client clientEndpoint = check new ("http://localhost:3000"); string jsonFilePath = "./tests/resources/queue-string.json"; json readJson = check io:fileReadJson(jsonFilePath); json resp = check clientEndpoint->post("/queue", readJson); - json expectedResp = {"Outputs":{"outMsg":"helloo aaaaa"},"Logs":[],"ReturnValue":null}; + json expectedResp = {"Outputs": {"outMsg": "helloo aaaaa"}, "Logs": [], "ReturnValue": null}; test:assertEquals(resp, expectedResp); } -@test:Config { } +@test:Config {} function testCosmosInputArr() returns error? { final http:Client clientEndpoint = check new ("http://localhost:3000"); string jsonFilePath = "./tests/resources/cosmos-db-arr.json"; json readJson = check io:fileReadJson(jsonFilePath); json resp = check clientEndpoint->post("/post-hello-db", readJson); - json expectedResp = {"Outputs":{"resp":{"body":"Hello Jackhello1"}},"Logs":[],"ReturnValue":null}; + json expectedResp = {"Outputs": {"resp": {"body": "Hello Jackhello1"}}, "Logs": [], "ReturnValue": null}; test:assertEquals(resp, expectedResp); } -@test:Config { } +@test:Config {} function testJsonJsonPayload() returns error? { final http:Client clientEndpoint = check new ("http://localhost:3000"); string jsonFilePath = "./tests/resources/payload-json-json.json"; json readJson = check io:fileReadJson(jsonFilePath); json resp = check clientEndpoint->post("/post-hello-payload-jsonToJson", readJson); - json expectedResp = {"Outputs":{"resp":{"body":"Hello from json to json Anjana"}},"Logs":[],"ReturnValue":null}; + json expectedResp = {"Outputs": {"resp": {"body": "Hello from json to json Anjana"}}, "Logs": [], "ReturnValue": null}; test:assertEquals(resp, expectedResp); } -@test:Config { } + +@test:Config {} function testJsonRecordPayload() returns error? { final http:Client clientEndpoint = check new ("http://localhost:3000"); string jsonFilePath = "./tests/resources/payload-json-record.json"; json readJson = check io:fileReadJson(jsonFilePath); json resp = check clientEndpoint->post("/post-hello-payload-jsonToRecord", readJson); - json expectedResp = {"Outputs":{"resp":{"body":"Hello from json to record Anjana"}},"Logs":[],"ReturnValue":null}; + json expectedResp = {"Outputs": {"resp": {"body": "Hello from json to record Anjana"}}, "Logs": [], "ReturnValue": null}; test:assertEquals(resp, expectedResp); } -@test:Config { } +@test:Config {} function testXmlPayload() returns error? { final http:Client clientEndpoint = check new ("http://localhost:3000"); string jsonFilePath = "./tests/resources/payload-xml-xml.json"; json readJson = check io:fileReadJson(jsonFilePath); json resp = check clientEndpoint->post("/post-hello-payload-xmlToXml", readJson); string xmlPayload = "\"\\n Anjana<\\/name>\\n 12<\\/age>\\n<\\/root>\""; - json expectedResp = {"Outputs":{"resp":{"body":xmlPayload}},"Logs":[],"ReturnValue":null}; + json expectedResp = {"Outputs": {"resp": {"body": xmlPayload}}, "Logs": [], "ReturnValue": null}; test:assertEquals(resp, expectedResp); } -@test:Config { } +@test:Config {} function testTextStringPayload() returns error? { final http:Client clientEndpoint = check new ("http://localhost:3000"); string jsonFilePath = "./tests/resources/payload-text-string.json"; json readJson = check io:fileReadJson(jsonFilePath); json resp = check clientEndpoint->post("/post-hello-payload-textToString", readJson); - json expectedResp = {"Outputs":{"resp":{"body":"hello from byte\n"}},"Logs":[],"ReturnValue":null}; + json expectedResp = {"Outputs": {"resp": {"body": "hello from byte\n"}}, "Logs": [], "ReturnValue": null}; test:assertEquals(resp, expectedResp); } -@test:Config { } +@test:Config {} function testTextBytePayload() returns error? { final http:Client clientEndpoint = check new ("http://localhost:3000"); string jsonFilePath = "./tests/resources/payload-text-byte.json"; json readJson = check io:fileReadJson(jsonFilePath); json resp = check clientEndpoint->post("/post-hello-payload-textToByte", readJson); - json expectedResp = {"Outputs":{"resp":{"body":"hello from byte\n"}},"Logs":[],"ReturnValue":null}; + json expectedResp = {"Outputs": {"resp": {"body": "hello from byte\n"}}, "Logs": [], "ReturnValue": null}; test:assertEquals(resp, expectedResp); } -@test:Config { } +@test:Config {} function testOctaBytePayload() returns error? { final http:Client clientEndpoint = check new ("http://localhost:3000"); string jsonFilePath = "./tests/resources/payload-octa-byte.json"; json readJson = check io:fileReadJson(jsonFilePath); json resp = check clientEndpoint->post("/post-hello-payload-octaToByte", readJson); - json expectedResp = {"Outputs":{"resp":{"body":"hello from byte arr\n"}},"Logs":[],"ReturnValue":null}; + json expectedResp = {"Outputs": {"resp": {"body": "hello from byte arr\n"}}, "Logs": [], "ReturnValue": null}; test:assertEquals(resp, expectedResp); } - -@test:Config { } +@test:Config {} function testCosmosTrigger() returns error? { final http:Client clientEndpoint = check new ("http://localhost:3000"); string jsonFilePath = "./tests/resources/trigger-cosmos-base.json"; json readJson = check io:fileReadJson(jsonFilePath); json resp = check clientEndpoint->post("/cosmos", readJson); - json expectedResp = {"Outputs":{"outMsg":"helloo ehee"},"Logs":[],"ReturnValue":null}; + json expectedResp = {"Outputs": {"outMsg": "helloo ehee"}, "Logs": [], "ReturnValue": null}; test:assertEquals(resp, expectedResp); } - -@test:Config { } +@test:Config {} function testTimerTrigger() returns error? { final http:Client clientEndpoint = check new ("http://localhost:3000"); string jsonFilePath = "./tests/resources/timer.json"; json readJson = check io:fileReadJson(jsonFilePath); json resp = check clientEndpoint->post("/timer", readJson); - json expectedResp = {"Outputs":{"outMsg":"helloo false"},"Logs":[],"ReturnValue":null}; + json expectedResp = {"Outputs": {"outMsg": "helloo false"}, "Logs": [], "ReturnValue": null}; test:assertEquals(resp, expectedResp); } -@test:Config { } +@test:Config {} function testQueueInput() returns error? { final http:Client clientEndpoint = check new ("http://localhost:3000"); string jsonFilePath = "./tests/resources/queue-input.json"; json readJson = check io:fileReadJson(jsonFilePath); json resp = check clientEndpoint->post("/queue-input", readJson); - json expectedResp = {"Outputs":{"outMsg":"helloo qqeeewwww hello1"},"Logs":[],"ReturnValue":null}; + json expectedResp = {"Outputs": {"outMsg": "helloo qqeeewwww hello1"}, "Logs": [], "ReturnValue": null}; test:assertEquals(resp, expectedResp); } diff --git a/ballerina/Dependencies.toml b/ballerina/Dependencies.toml index d5301fe7..3f0fc2f0 100644 --- a/ballerina/Dependencies.toml +++ b/ballerina/Dependencies.toml @@ -9,7 +9,7 @@ dependencies-toml-version = "2" [[package]] org = "ballerina" name = "auth" -version = "2.3.0" +version = "2.3.1" dependencies = [ {org = "ballerina", name = "crypto"}, {org = "ballerina", name = "jballerina.java"}, @@ -104,7 +104,7 @@ modules = [ [[package]] org = "ballerina" name = "jwt" -version = "2.3.0" +version = "2.3.1" dependencies = [ {org = "ballerina", name = "cache"}, {org = "ballerina", name = "crypto"}, @@ -223,7 +223,7 @@ dependencies = [ [[package]] org = "ballerina" name = "oauth2" -version = "2.3.0" +version = "2.3.1" dependencies = [ {org = "ballerina", name = "cache"}, {org = "ballerina", name = "crypto"}, @@ -235,7 +235,7 @@ dependencies = [ [[package]] org = "ballerina" name = "observe" -version = "1.0.4" +version = "1.0.5" dependencies = [ {org = "ballerina", name = "jballerina.java"} ] diff --git a/ballerina/http_status_code_types.bal b/ballerina/http_status_code_types.bal new file mode 100644 index 00000000..821a5cbf --- /dev/null +++ b/ballerina/http_status_code_types.bal @@ -0,0 +1,932 @@ +// Copyright (c) 2022 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +// +// WSO2 Inc. licenses this file to you under the Apache License, +// Version 2.0 (the "License"); you may not use this file except +// in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +// Remove the union once https://github.com/ballerina-platform/ballerina-lang/issues/30490 is fixed. +# Defines the possible status code response record types. +public type StatusCodeResponse Continue|SwitchingProtocols|Ok|Created|Accepted|NonAuthoritativeInformation|NoContent| + ResetContent|PartialContent|MultipleChoices|MovedPermanently|Found|SeeOther|NotModified|UseProxy|TemporaryRedirect| + PermanentRedirect|BadRequest|Unauthorized|PaymentRequired|Forbidden|NotFound|MethodNotAllowed|NotAcceptable| + ProxyAuthenticationRequired|RequestTimeout|Conflict|Gone|LengthRequired|PreconditionFailed|PayloadTooLarge| + UriTooLong|UnsupportedMediaType|RangeNotSatisfiable|ExpectationFailed|UpgradeRequired|TooManyRequests| + RequestHeaderFieldsTooLarge|InternalServerError|NotImplemented|BadGateway|ServiceUnavailable|GatewayTimeout| + HttpVersionNotSupported; + +# Defines the possible success status code response record types. +type SuccessStatusCodeResponse Ok|Created|Accepted|NonAuthoritativeInformation|NoContent|ResetContent| + PartialContent; + +# The `Status` object creates the distinction for the different response status code types. +# +# + code - The response status code +public type Status distinct object { + public int code; +}; + +# The common attributed of response status code record type. +# +# + mediaType - The value of response `Content-type` header +# + headers - The response headers +# + body - The response payload +public type CommonResponse record {| + string mediaType?; + map headers?; + anydata body?; +|}; + +// Status code class declarations +# Represents the status code of `STATUS_CONTINUE`. +# +# + code - The response status code +public readonly class StatusContinue { + *Status; + public STATUS_CONTINUE code = STATUS_CONTINUE; +} + +# Represents the status code of `STATUS_SWITCHING_PROTOCOLS`. +# +# + code - The response status code +public readonly class StatusSwitchingProtocols { + *Status; + public STATUS_SWITCHING_PROTOCOLS code = STATUS_SWITCHING_PROTOCOLS; +} + +# Represents the status code of `STATUS_OK`. +# +# + code - The response status code +public readonly class StatusOK { + *Status; + public STATUS_OK code = STATUS_OK; +} + +# Represents the status code of `STATUS_CREATED`. +# +# + code - The response status code +public readonly class StatusCreated { + *Status; + public STATUS_CREATED code = STATUS_CREATED; +} + +# Represents the status code of `STATUS_ACCEPTED`. +# +# + code - The response status code +public readonly class StatusAccepted { + *Status; + public STATUS_ACCEPTED code = STATUS_ACCEPTED; +} + +# Represents the status code of `STATUS_NON_AUTHORITATIVE_INFORMATION`. +# +# + code - The response status code +public readonly class StatusNonAuthoritativeInformation { + *Status; + public STATUS_NON_AUTHORITATIVE_INFORMATION code = STATUS_NON_AUTHORITATIVE_INFORMATION; +} + +# Represents the status code of `STATUS_NO_CONTENT`. +# +# + code - The response status code +public readonly class StatusNoContent { + *Status; + public STATUS_NO_CONTENT code = STATUS_NO_CONTENT; +} + +# Represents the status code of `STATUS_RESET_CONTENT`. +# +# + code - The response status code +public readonly class StatusResetContent { + *Status; + public STATUS_RESET_CONTENT code = STATUS_RESET_CONTENT; +} + +# Represents the status code of `STATUS_PARTIAL_CONTENT`. +# +# + code - The response status code +public readonly class StatusPartialContent { + *Status; + public STATUS_PARTIAL_CONTENT code = STATUS_PARTIAL_CONTENT; +} + +# Represents the status code of `STATUS_MULTIPLE_CHOICES`. +# +# + code - The response status code +public readonly class StatusMultipleChoices { + *Status; + public STATUS_MULTIPLE_CHOICES code = STATUS_MULTIPLE_CHOICES; +} + +# Represents the status code of `STATUS_MOVED_PERMANENTLY`. +# +# + code - The response status code +public readonly class StatusMovedPermanently { + *Status; + public STATUS_MOVED_PERMANENTLY code = STATUS_MOVED_PERMANENTLY; +} + +# Represents the status code of `STATUS_FOUND`. +# +# + code - The response status code +public readonly class StatusFound { + *Status; + public STATUS_FOUND code = STATUS_FOUND; +} + +# Represents the status code of `STATUS_SEE_OTHER`. +# +# + code - The response status code +public readonly class StatusSeeOther { + *Status; + public STATUS_SEE_OTHER code = STATUS_SEE_OTHER; +} + +# Represents the status code of `STATUS_NOT_MODIFIED`. +# +# + code - The response status code +public readonly class StatusNotModified { + *Status; + public STATUS_NOT_MODIFIED code = STATUS_NOT_MODIFIED; +} + +# Represents the status code of `STATUS_USE_PROXY`. +# +# + code - The response status code +public readonly class StatusUseProxy { + *Status; + public STATUS_USE_PROXY code = STATUS_USE_PROXY; +} + +# Represents the status code of `STATUS_TEMPORARY_REDIRECT`. +# +# + code - The response status code +public readonly class StatusTemporaryRedirect { + *Status; + public STATUS_TEMPORARY_REDIRECT code = STATUS_TEMPORARY_REDIRECT; +} + +# Represents the status code of `STATUS_PERMANENT_REDIRECT`. +# +# + code - The response status code +public readonly class StatusPermanentRedirect { + *Status; + public STATUS_PERMANENT_REDIRECT code = STATUS_PERMANENT_REDIRECT; +} + +# Represents the status code of `STATUS_BAD_REQUEST`. +# +# + code - The response status code +public readonly class StatusBadRequest { + *Status; + public STATUS_BAD_REQUEST code = STATUS_BAD_REQUEST; +} + +# Represents the status code of `STATUS_UNAUTHORIZED`. +# +# + code - The response status code +public readonly class StatusUnauthorized { + *Status; + public STATUS_UNAUTHORIZED code = STATUS_UNAUTHORIZED; +} + +# Represents the status code of `STATUS_PAYMENT_REQUIRED`. +# +# + code - The response status code +public readonly class StatusPaymentRequired { + *Status; + public STATUS_PAYMENT_REQUIRED code = STATUS_PAYMENT_REQUIRED; +} + +# Represents the status code of `STATUS_FORBIDDEN`. +# +# + code - The response status code +public readonly class StatusForbidden { + *Status; + public STATUS_FORBIDDEN code = STATUS_FORBIDDEN; +} + +# Represents the status code of `STATUS_NOT_FOUND`. +# +# + code - The response status code +public readonly class StatusNotFound { + *Status; + public STATUS_NOT_FOUND code = STATUS_NOT_FOUND; +} + +# Represents the status code of `STATUS_METHOD_NOT_ALLOWED`. +# +# + code - The response status code +public readonly class StatusMethodNotAllowed { + *Status; + public STATUS_METHOD_NOT_ALLOWED code = STATUS_METHOD_NOT_ALLOWED; +} + +# Represents the status code of `STATUS_NOT_ACCEPTABLE`. +# +# + code - The response status code +public readonly class StatusNotAcceptable { + *Status; + public STATUS_NOT_ACCEPTABLE code = STATUS_NOT_ACCEPTABLE; +} + +# Represents the status code of `STATUS_PROXY_AUTHENTICATION_REQUIRED`. +# +# + code - The response status code +public readonly class StatusProxyAuthenticationRequired { + *Status; + public STATUS_PROXY_AUTHENTICATION_REQUIRED code = STATUS_PROXY_AUTHENTICATION_REQUIRED; +} + +# Represents the status code of `STATUS_REQUEST_TIMEOUT`. +# +# + code - The response status code +public readonly class StatusRequestTimeout { + *Status; + public STATUS_REQUEST_TIMEOUT code = STATUS_REQUEST_TIMEOUT; +} + +# Represents the status code of `STATUS_CONFLICT`. +# +# + code - The response status code +public readonly class StatusConflict { + *Status; + public STATUS_CONFLICT code = STATUS_CONFLICT; +} + +# Represents the status code of `STATUS_GONE`. +# +# + code - The response status code +public readonly class StatusGone { + *Status; + public STATUS_GONE code = STATUS_GONE; +} + +# Represents the status code of `STATUS_LENGTH_REQUIRED`. +# +# + code - The response status code +public readonly class StatusLengthRequired { + *Status; + public STATUS_LENGTH_REQUIRED code = STATUS_LENGTH_REQUIRED; +} + +# Represents the status code of `STATUS_PRECONDITION_FAILED`. +# +# + code - The response status code +public readonly class StatusPreconditionFailed { + *Status; + public STATUS_PRECONDITION_FAILED code = STATUS_PRECONDITION_FAILED; +} + +# Represents the status code of `STATUS_PAYLOAD_TOO_LARGE`. +# +# + code - The response status code +public readonly class StatusPayloadTooLarge { + *Status; + public STATUS_PAYLOAD_TOO_LARGE code = STATUS_PAYLOAD_TOO_LARGE; +} + +# Represents the status code of `STATUS_URI_TOO_LONG`. +# +# + code - The response status code +public readonly class StatusUriTooLong { + *Status; + public STATUS_URI_TOO_LONG code = STATUS_URI_TOO_LONG; +} + +# Represents the status code of `STATUS_UNSUPPORTED_MEDIA_TYPE`. +# +# + code - The response status code +public readonly class StatusUnsupportedMediaType { + *Status; + public STATUS_UNSUPPORTED_MEDIA_TYPE code = STATUS_UNSUPPORTED_MEDIA_TYPE; +} + +# Represents the status code of `STATUS_RANGE_NOT_SATISFIABLE`. +# +# + code - The response status code +public readonly class StatusRangeNotSatisfiable { + *Status; + public STATUS_RANGE_NOT_SATISFIABLE code = STATUS_RANGE_NOT_SATISFIABLE; +} + +# Represents the status code of `STATUS_EXPECTATION_FAILED`. +# +# + code - The response status code +public readonly class StatusExpectationFailed { + *Status; + public STATUS_EXPECTATION_FAILED code = STATUS_EXPECTATION_FAILED; +} + +# Represents the status code of `STATUS_UPGRADE_REQUIRED`. +# +# + code - The response status code +public readonly class StatusUpgradeRequired { + *Status; + public STATUS_UPGRADE_REQUIRED code = STATUS_UPGRADE_REQUIRED; +} + +# Represents the status code of `STATUS_TOO_MANY_REQUESTS`. +# +# + code - The response status code +public readonly class StatusTooManyRequests { + *Status; + public STATUS_TOO_MANY_REQUESTS code = STATUS_TOO_MANY_REQUESTS; +} + +# Represents the status code of `STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE`. +# +# + code - The response status code +public readonly class StatusRequestHeaderFieldsTooLarge { + *Status; + public STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE code = STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE; +} + +# Represents the status code of `STATUS_INTERNAL_SERVER_ERROR`. +# +# + code - The response status code +public readonly class StatusInternalServerError { + *Status; + public STATUS_INTERNAL_SERVER_ERROR code = STATUS_INTERNAL_SERVER_ERROR; +} + +# Represents the status code of `STATUS_NOT_IMPLEMENTED`. +# +# + code - The response status code +public readonly class StatusNotImplemented { + *Status; + public STATUS_NOT_IMPLEMENTED code = STATUS_NOT_IMPLEMENTED; +} + +# Represents the status code of `STATUS_BAD_GATEWAY`. +# +# + code - The response status code +public readonly class StatusBadGateway { + *Status; + public STATUS_BAD_GATEWAY code = STATUS_BAD_GATEWAY; +} + +# Represents the status code of `STATUS_SERVICE_UNAVAILABLE`. +# +# + code - The response status code +public readonly class StatusServiceUnavailable { + *Status; + public STATUS_SERVICE_UNAVAILABLE code = STATUS_SERVICE_UNAVAILABLE; +} + +# Represents the status code of `STATUS_GATEWAY_TIMEOUT`. +# +# + code - The response status code +public readonly class StatusGatewayTimeout { + *Status; + public STATUS_GATEWAY_TIMEOUT code = STATUS_GATEWAY_TIMEOUT; +} + +# Represents the status code of `STATUS_HTTP_VERSION_NOT_SUPPORTED`. +# +# + code - The response status code +public readonly class StatusHttpVersionNotSupported { + *Status; + public STATUS_HTTP_VERSION_NOT_SUPPORTED code = STATUS_HTTP_VERSION_NOT_SUPPORTED; +} + +// Status code object initialization +final StatusContinue STATUS_CONTINUE_OBJ = new; +final StatusSwitchingProtocols STATUS_SWITCHING_PROTOCOLS_OBJ = new; +final StatusOK STATUS_OK_OBJ = new; +final StatusCreated STATUS_CREATED_OBJ = new; +final StatusAccepted STATUS_ACCEPTED_OBJ = new; +final StatusNonAuthoritativeInformation STATUS_NON_AUTHORITATIVE_INFORMATION_OBJ = new; +final StatusNoContent STATUS_NO_CONTENT_OBJ = new; +final StatusResetContent STATUS_RESET_CONTENT_OBJ = new; +final StatusPartialContent STATUS_PARTIAL_CONTENT_OBJ = new; +final StatusMultipleChoices STATUS_MULTIPLE_CHOICES_OBJ = new; +final StatusMovedPermanently STATUS_MOVED_PERMANENTLY_OBJ = new; +final StatusFound STATUS_FOUND_OBJ = new; +final StatusSeeOther STATUS_SEE_OTHER_OBJ = new; +final StatusNotModified STATUS_NOT_MODIFIED_OBJ = new; +final StatusUseProxy STATUS_USE_PROXY_OBJ = new; +final StatusTemporaryRedirect STATUS_TEMPORARY_REDIRECT_OBJ = new; +final StatusPermanentRedirect STATUS_PERMANENT_REDIRECT_OBJ = new; +final StatusBadRequest STATUS_BAD_REQUEST_OBJ = new; +final StatusUnauthorized STATUS_UNAUTHORIZED_OBJ = new; +final StatusPaymentRequired STATUS_PAYMENT_REQUIRED_OBJ = new; +final StatusForbidden STATUS_FORBIDDEN_OBJ = new; +final StatusNotFound STATUS_NOT_FOUND_OBJ = new; +final StatusMethodNotAllowed STATUS_METHOD_NOT_ALLOWED_OBJ = new; +final StatusNotAcceptable STATUS_NOT_ACCEPTABLE_OBJ = new; +final StatusProxyAuthenticationRequired STATUS_PROXY_AUTHENTICATION_REQUIRED_OBJ = new; +final StatusRequestTimeout STATUS_REQUEST_TIMEOUT_OBJ = new; +final StatusConflict STATUS_CONFLICT_OBJ = new; +final StatusGone STATUS_GONE_OBJ = new; +final StatusLengthRequired STATUS_LENGTH_REQUIRED_OBJ = new; +final StatusPreconditionFailed STATUS_PRECONDITION_FAILED_OBJ = new; +final StatusPayloadTooLarge STATUS_PAYLOAD_TOO_LARGE_OBJ = new; +final StatusUriTooLong STATUS_URI_TOO_LONG_OBJ = new; +final StatusUnsupportedMediaType STATUS_UNSUPPORTED_MEDIA_TYPE_OBJ = new; +final StatusRangeNotSatisfiable STATUS_RANGE_NOT_SATISFIABLE_OBJ = new; +final StatusExpectationFailed STATUS_EXPECTATION_FAILED_OBJ = new; +final StatusUpgradeRequired STATUS_UPGRADE_REQUIRED_OBJ = new; +final StatusTooManyRequests STATUS_TOO_MANY_REQUESTS_OBJ = new; +final StatusRequestHeaderFieldsTooLarge STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE_OBJ = new; +final StatusInternalServerError STATUS_INTERNAL_SERVER_ERROR_OBJ = new; +final StatusNotImplemented STATUS_NOT_IMPLEMENTED_OBJ = new; +final StatusBadGateway STATUS_BAD_GATEWAY_OBJ = new; +final StatusServiceUnavailable STATUS_SERVICE_UNAVAILABLE_OBJ = new; +final StatusGatewayTimeout STATUS_GATEWAY_TIMEOUT_OBJ = new; +final StatusHttpVersionNotSupported STATUS_HTTP_VERSION_NOT_SUPPORTED_OBJ = new; + +// Status code record types +# The status code response record of `Continue`. +# +# + status - The response status code obj +public type Continue record {| + *CommonResponse; + readonly StatusContinue status = STATUS_CONTINUE_OBJ; +|}; + +# The status code response record of `SwitchingProtocols`. +# +# + status - The response status code obj +public type SwitchingProtocols record {| + *CommonResponse; + readonly StatusSwitchingProtocols status = STATUS_SWITCHING_PROTOCOLS_OBJ; +|}; + +# The status code response record of `Ok`. +# +# + status - The response status code obj +public type Ok record {| + *CommonResponse; + readonly StatusOK status = STATUS_OK_OBJ; +|}; + +# The status code response record of `Created`. +# +# + status - The response status code obj +public type Created record {| + *CommonResponse; + readonly StatusCreated status = STATUS_CREATED_OBJ; +|}; + +# The status code response record of `Accepted`. +# +# + status - The response status code obj +public type Accepted record {| + *CommonResponse; + readonly StatusAccepted status = STATUS_ACCEPTED_OBJ; +|}; + +# The status code response record of `NonAuthoritativeInformation`. +# +# + status - The response status code obj +public type NonAuthoritativeInformation record {| + *CommonResponse; + readonly StatusNonAuthoritativeInformation status = STATUS_NON_AUTHORITATIVE_INFORMATION_OBJ; +|}; + +# The status code response record of `NoContent`. +# +# + headers - The response headers +# + status - The response status code obj +public type NoContent record {| + map headers?; + readonly StatusNoContent status = STATUS_NO_CONTENT_OBJ; +|}; + +# The status code response record of `ResetContent`. +# +# + status - The response status code obj +public type ResetContent record {| + *CommonResponse; + readonly StatusResetContent status = STATUS_RESET_CONTENT_OBJ; +|}; + +# The status code response record of `PartialContent`. +# +# + status - The response status code obj +public type PartialContent record {| + *CommonResponse; + readonly StatusPartialContent status = STATUS_PARTIAL_CONTENT_OBJ; +|}; + +# The status code response record of `MultipleChoices`. +# +# + status - The response status code obj +public type MultipleChoices record {| + *CommonResponse; + readonly StatusMultipleChoices status = STATUS_MULTIPLE_CHOICES_OBJ; +|}; + +# The status code response record of `MovedPermanently`. +# +# + status - The response status code obj +public type MovedPermanently record {| + *CommonResponse; + readonly StatusMovedPermanently status = STATUS_MOVED_PERMANENTLY_OBJ; +|}; + +# The status code response record of `Found`. +# +# + status - The response status code obj +public type Found record {| + *CommonResponse; + readonly StatusFound status = STATUS_FOUND_OBJ; +|}; + +# The status code response record of `SeeOther`. +# +# + status - The response status code obj +public type SeeOther record {| + *CommonResponse; + readonly StatusSeeOther status = STATUS_SEE_OTHER_OBJ; +|}; + +# The status code response record of `NotModified`. +# +# + status - The response status code obj +public type NotModified record {| + *CommonResponse; + readonly StatusNotModified status = STATUS_NOT_MODIFIED_OBJ; +|}; + +# The status code response record of `UseProxy`. +# +# + status - The response status code obj +public type UseProxy record {| + *CommonResponse; + readonly StatusUseProxy status = STATUS_USE_PROXY_OBJ; +|}; + +# The status code response record of `TemporaryRedirect`. +# +# + status - The response status code obj +public type TemporaryRedirect record {| + *CommonResponse; + readonly StatusTemporaryRedirect status = STATUS_TEMPORARY_REDIRECT_OBJ; +|}; + +# The status code response record of `PermanentRedirect`. +# +# + status - The response status code obj +public type PermanentRedirect record {| + *CommonResponse; + readonly StatusPermanentRedirect status = STATUS_PERMANENT_REDIRECT_OBJ; +|}; + +# The status code response record of `BadRequest`. +# +# + status - The response status code obj +public type BadRequest record {| + *CommonResponse; + readonly StatusBadRequest status = STATUS_BAD_REQUEST_OBJ; +|}; + +# The status code response record of `Unauthorized`. +# +# + status - The response status code obj +public type Unauthorized record {| + *CommonResponse; + readonly StatusUnauthorized status = STATUS_UNAUTHORIZED_OBJ; +|}; + +# The status code response record of `PaymentRequired`. +# +# + status - The response status code obj +public type PaymentRequired record {| + *CommonResponse; + readonly StatusPaymentRequired status = STATUS_PAYMENT_REQUIRED_OBJ; +|}; + +# The status code response record of `Forbidden`. +# +# + status - The response status code obj +public type Forbidden record {| + *CommonResponse; + readonly StatusForbidden status = STATUS_FORBIDDEN_OBJ; +|}; + +# The status code response record of `NotFound`. +# +# + status - The response status code obj +public type NotFound record {| + *CommonResponse; + readonly StatusNotFound status = STATUS_NOT_FOUND_OBJ; +|}; + +# The status code response record of `MethodNotAllowed`. +# +# + status - The response status code obj +public type MethodNotAllowed record {| + *CommonResponse; + readonly StatusMethodNotAllowed status = STATUS_METHOD_NOT_ALLOWED_OBJ; +|}; + +# The status code response record of `NotAcceptable`. +# +# + status - The response status code obj +public type NotAcceptable record {| + *CommonResponse; + readonly StatusNotAcceptable status = STATUS_NOT_ACCEPTABLE_OBJ; +|}; + +# The status code response record of `ProxyAuthenticationRequired`. +# +# + status - The response status code obj +public type ProxyAuthenticationRequired record {| + *CommonResponse; + readonly StatusProxyAuthenticationRequired status = STATUS_PROXY_AUTHENTICATION_REQUIRED_OBJ; +|}; + +# The status code response record of `RequestTimeout`. +# +# + status - The response status code obj +public type RequestTimeout record {| + *CommonResponse; + readonly StatusRequestTimeout status = STATUS_REQUEST_TIMEOUT_OBJ; +|}; + +# The status code response record of `Conflict`. +# +# + status - The response status code obj +public type Conflict record {| + *CommonResponse; + readonly StatusConflict status = STATUS_CONFLICT_OBJ; +|}; + +# The status code response record of `Gone`. +# +# + status - The response status code obj +public type Gone record {| + *CommonResponse; + readonly StatusGone status = STATUS_GONE_OBJ; +|}; + +# The status code response record of `LengthRequired`. +# +# + status - The response status code obj +public type LengthRequired record {| + *CommonResponse; + readonly StatusLengthRequired status = STATUS_LENGTH_REQUIRED_OBJ; +|}; + +# The status code response record of `PreconditionFailed`. +# +# + status - The response status code obj +public type PreconditionFailed record {| + *CommonResponse; + readonly StatusPreconditionFailed status = STATUS_PRECONDITION_FAILED_OBJ; +|}; + +# The status code response record of `PayloadTooLarge`. +# +# + status - The response status code obj +public type PayloadTooLarge record {| + *CommonResponse; + readonly StatusPayloadTooLarge status = STATUS_PAYLOAD_TOO_LARGE_OBJ; +|}; + +# The status code response record of `UriTooLong`. +# +# + status - The response status code obj +public type UriTooLong record {| + *CommonResponse; + readonly StatusUriTooLong status = STATUS_URI_TOO_LONG_OBJ; +|}; + +# The status code response record of `UnsupportedMediaType`. +# +# + status - The response status code obj +public type UnsupportedMediaType record {| + *CommonResponse; + readonly StatusUnsupportedMediaType status = STATUS_UNSUPPORTED_MEDIA_TYPE_OBJ; +|}; + +# The status code response record of `RangeNotSatisfiable`. +# +# + status - The response status code obj +public type RangeNotSatisfiable record {| + *CommonResponse; + readonly StatusRangeNotSatisfiable status = STATUS_RANGE_NOT_SATISFIABLE_OBJ; +|}; + +# The status code response record of `ExpectationFailed`. +# +# + status - The response status code obj +public type ExpectationFailed record {| + *CommonResponse; + readonly StatusExpectationFailed status = STATUS_EXPECTATION_FAILED_OBJ; +|}; + +# The status code response record of `UpgradeRequired`. +# +# + status - The response status code obj +public type UpgradeRequired record {| + *CommonResponse; + readonly StatusUpgradeRequired status = STATUS_UPGRADE_REQUIRED_OBJ; +|}; + +# The status code response record of `TooManyRequests`. +# +# + status - The response status code obj +public type TooManyRequests record {| + *CommonResponse; + readonly StatusTooManyRequests status = STATUS_TOO_MANY_REQUESTS_OBJ; +|}; + +# The status code response record of `RequestHeaderFieldsTooLarge`. +# +# + status - The response status code obj +public type RequestHeaderFieldsTooLarge record {| + *CommonResponse; + readonly StatusRequestHeaderFieldsTooLarge status = STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE_OBJ; +|}; + +# The status code response record of `InternalServerError`. +# +# + status - The response status code obj +public type InternalServerError record {| + *CommonResponse; + readonly StatusInternalServerError status = STATUS_INTERNAL_SERVER_ERROR_OBJ; +|}; + +# The status code response record of `NotImplemented`. +# +# + status - The response status code obj +public type NotImplemented record {| + *CommonResponse; + readonly StatusNotImplemented status = STATUS_NOT_IMPLEMENTED_OBJ; +|}; + +# The status code response record of `BadGateway`. +# +# + status - The response status code obj +public type BadGateway record {| + *CommonResponse; + readonly StatusBadGateway status = STATUS_BAD_GATEWAY_OBJ; +|}; + +# The status code response record of `ServiceUnavailable`. +# +# + status - The response status code obj +public type ServiceUnavailable record {| + *CommonResponse; + readonly StatusServiceUnavailable status = STATUS_SERVICE_UNAVAILABLE_OBJ; +|}; + +# The status code response record of `GatewayTimeout`. +# +# + status - The response status code obj +public type GatewayTimeout record {| + *CommonResponse; + readonly StatusGatewayTimeout status = STATUS_GATEWAY_TIMEOUT_OBJ; +|}; + +# The status code response record of `HttpVersionNotSupported`. +# +# + status - The response status code obj +public type HttpVersionNotSupported record {| + *CommonResponse; + readonly StatusHttpVersionNotSupported status = STATUS_HTTP_VERSION_NOT_SUPPORTED_OBJ; +|}; + +# The common status code response constant of `Continue`. +public final readonly & Continue CONTINUE = {}; + +# The common status code response constant of `SwitchingProtocols`. +public final readonly & SwitchingProtocols SWITCHING_PROTOCOLS = {}; + +# The common status code response constant of `Ok`. +public final readonly & Ok OK = {}; + +# The common status code response constant of `Created`. +public final readonly & Created CREATED = {}; + +# The common status code response constant of `Accepted`. +public final readonly & Accepted ACCEPTED = {}; + +# The common status code response constant of `NonAuthoritativeInformation`. +public final readonly & NonAuthoritativeInformation NON_AUTHORITATIVE_INFORMATION = {}; + +# The common status code response constant of `NoContent`. +public final readonly & NoContent NO_CONTENT = {}; + +# The common status code response constant of `ResetContent`. +public final readonly & ResetContent RESET_CONTENT = {}; + +# The common status code response constant of `PartialContent`. +public final readonly & PartialContent PARTIAL_CONTENT = {}; + +# The common status code response constant of `MultipleChoices`. +public final readonly & MultipleChoices MULTIPLE_CHOICES = {}; + +# The common status code response constant of `MovedPermanently`. +public final readonly & MovedPermanently MOVED_PERMANENTLY = {}; + +# The common status code response constant of `Found`. +public final readonly & Found FOUND = {}; + +# The common status code response constant of `SeeOther`. +public final readonly & SeeOther SEE_OTHER = {}; + +# The common status code response constant of `NotModified`. +public final readonly & NotModified NOT_MODIFIED = {}; + +# The common status code response constant of `UseProxy`. +public final readonly & UseProxy USE_PROXY = {}; + +# The common status code response constant of `TemporaryRedirect`. +public final readonly & TemporaryRedirect TEMPORARY_REDIRECT = {}; + +# The common status code response constant of `PermanentRedirect`. +public final readonly & PermanentRedirect PERMANENT_REDIRECT = {}; + +# The common status code response constant of `BadRequest`. +public final readonly & BadRequest BAD_REQUEST = {}; + +# The common status code response constant of `Unauthorized`. +public final readonly & Unauthorized UNAUTHORIZED = {}; + +# The common status code response constant of `PaymentRequired`. +public final readonly & PaymentRequired PAYMENT_REQUIRED = {}; + +# The common status code response constant of `Forbidden`. +public final readonly & Forbidden FORBIDDEN = {}; + +# The common status code response constant of `NotFound`. +public final readonly & NotFound NOT_FOUND = {}; + +# The common status code response constant of `MethodNotAllowed`. +public final readonly & MethodNotAllowed METHOD_NOT_ALLOWED = {}; + +# The common status code response constant of `NotAcceptable`. +public final readonly & NotAcceptable NOT_ACCEPTABLE = {}; + +# The common status code response constant of `ProxyAuthenticationRequired`. +public final readonly & ProxyAuthenticationRequired PROXY_AUTHENTICATION_REQUIRED = {}; + +# The common status code response constant of `RequestTimeout`. +public final readonly & RequestTimeout REQUEST_TIMEOUT = {}; + +# The common status code response constant of `Conflict`. +public final readonly & Conflict CONFLICT = {}; + +# The common status code response constant of `Gone`. +public final readonly & Gone GONE = {}; + +# The common status code response constant of `LengthRequired`. +public final readonly & LengthRequired LENGTH_REQUIRED = {}; + +# The common status code response constant of `PreconditionFailed`. +public final readonly & PreconditionFailed PRECONDITION_FAILED = {}; + +# The common status code response constant of `PayloadTooLarge`. +public final readonly & PayloadTooLarge PAYLOAD_TOO_LARGE = {}; + +# The common status code response constant of `UriTooLong`. +public final readonly & UriTooLong URI_TOO_LONG = {}; + +# The common status code response constant of `UnsupportedMediaType`. +public final readonly & UnsupportedMediaType UNSUPPORTED_MEDIA_TYPE = {}; + +# The common status code response constant of `RangeNotSatisfiable`. +public final readonly & RangeNotSatisfiable RANGE_NOT_SATISFIABLE = {}; + +# The common status code response constant of `ExpectationFailed`. +public final readonly & ExpectationFailed EXPECTATION_FAILED = {}; + +# The common status code response constant of `UpgradeRequired`. +public final readonly & UpgradeRequired UPGRADE_REQUIRED = {}; + +# The common status code response constant of `TooManyRequests`. +public final readonly & TooManyRequests TOO_MANY_REQUESTS = {}; + +# The common status code response constant of `RequestHeaderFieldsTooLarge`. +public final readonly & RequestHeaderFieldsTooLarge REQUEST_HEADER_FIELDS_TOO_LARGE = {}; + +# The common status code response constant of `InternalServerError`. +public final readonly & InternalServerError INTERNAL_SERVER_ERROR = {}; + +# The common status code response constant of `NotImplemented`. +public final readonly & NotImplemented NOT_IMPLEMENTED = {}; + +# The common status code response constant of `BadGateway`. +public final readonly & BadGateway BAD_GATEWAY = {}; + +# The common status code response constant of `ServiceUnavailable`. +public final readonly & ServiceUnavailable SERVICE_UNAVAILABLE = {}; + +# The common status code response constant of `GatewayTimeout`. +public final readonly & GatewayTimeout GATEWAY_TIMEOUT = {}; + +# The common status code response constant of `HttpVersionNotSupported`. +public final readonly & HttpVersionNotSupported HTTP_VERSION_NOT_SUPPORTED = {}; diff --git a/ballerina/http_status_codes.bal b/ballerina/http_status_codes.bal new file mode 100644 index 00000000..1c999d60 --- /dev/null +++ b/ballerina/http_status_codes.bal @@ -0,0 +1,108 @@ +// Copyright (c) 2022 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +// +// WSO2 Inc. licenses this file to you under the Apache License, +// Version 2.0 (the "License"); you may not use this file except +// in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +# The HTTP response status code: 100 Continue +public const int STATUS_CONTINUE = 100; +# The HTTP response status code: 101 Switching Protocols +public const int STATUS_SWITCHING_PROTOCOLS = 101; + +# The HTTP response status code: 200 OK +public const int STATUS_OK = 200; +# The HTTP response status code: 201 Created +public const int STATUS_CREATED = 201; +# The HTTP response status code: 202 Accepted +public const int STATUS_ACCEPTED = 202; +# The HTTP response status code: 203 Non Authoritative Information +public const int STATUS_NON_AUTHORITATIVE_INFORMATION = 203; +# The HTTP response status code: 204 No Content +public const int STATUS_NO_CONTENT = 204; +# The HTTP response status code: 205 Reset Content +public const int STATUS_RESET_CONTENT = 205; +# The HTTP response status code: 206 Partial Content +public const int STATUS_PARTIAL_CONTENT = 206; + +# The HTTP response status code: 300 Multiple Choices +public const int STATUS_MULTIPLE_CHOICES = 300; +# The HTTP response status code: 301 Moved Permanently +public const int STATUS_MOVED_PERMANENTLY = 301; +# The HTTP response status code: 302 Found +public const int STATUS_FOUND = 302; +# The HTTP response status code: 303 See Other +public const int STATUS_SEE_OTHER = 303; +# The HTTP response status code: 304 Not Modified +public const int STATUS_NOT_MODIFIED = 304; +# The HTTP response status code: 305 Use Proxy +public const int STATUS_USE_PROXY = 305; +# The HTTP response status code: 307 Temporary Redirect +public const int STATUS_TEMPORARY_REDIRECT = 307; +# The HTTP response status code: 308 Permanent Redirect +public const int STATUS_PERMANENT_REDIRECT = 308; + +# The HTTP response status code: 400 Bad Request +public const int STATUS_BAD_REQUEST = 400; +# The HTTP response status code: 401 Unauthorized +public const int STATUS_UNAUTHORIZED = 401; +# The HTTP response status code: 402 Payment Required +public const int STATUS_PAYMENT_REQUIRED = 402; +# The HTTP response status code: 403 Forbidden +public const int STATUS_FORBIDDEN = 403; +# The HTTP response status code: 404 Not Found +public const int STATUS_NOT_FOUND = 404; +# The HTTP response status code: 405 Method Not Allowed +public const int STATUS_METHOD_NOT_ALLOWED = 405; +# The HTTP response status code: 406 Not Acceptable +public const int STATUS_NOT_ACCEPTABLE = 406; +# The HTTP response status code: 407 Proxy Authentication Required +public const int STATUS_PROXY_AUTHENTICATION_REQUIRED = 407; +# The HTTP response status code: 408 Request Timeout +public const int STATUS_REQUEST_TIMEOUT = 408; +# The HTTP response status code: 409 Conflict +public const int STATUS_CONFLICT = 409; +# The HTTP response status code: 410 Gone +public const int STATUS_GONE = 410; +# The HTTP response status code: 411 Length Required +public const int STATUS_LENGTH_REQUIRED = 411; +# The HTTP response status code: 412 Precondition Failed +public const int STATUS_PRECONDITION_FAILED = 412; +# The HTTP response status code: 413 Payload Too Large +public const int STATUS_PAYLOAD_TOO_LARGE = 413; +# The HTTP response status code: 414 URI Too Long +public const int STATUS_URI_TOO_LONG = 414; +# The HTTP response status code: 415 Unsupported Media Type +public const int STATUS_UNSUPPORTED_MEDIA_TYPE = 415; +# The HTTP response status code: 416 Range Not Satisfiable +public const int STATUS_RANGE_NOT_SATISFIABLE = 416; +# The HTTP response status code: 417 Expectation Failed +public const int STATUS_EXPECTATION_FAILED = 417; +# The HTTP response status code: 426 Upgrade Required +public const int STATUS_UPGRADE_REQUIRED = 426; +# The HTTP response status code: 429 Too Many Requests +public const int STATUS_TOO_MANY_REQUESTS = 429; +# The HTTP response status code: 431 Request Header Fields Too Large +public const int STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE = 431; + +# The HTTP response status code: 500 Internal Server Error +public const int STATUS_INTERNAL_SERVER_ERROR = 500; +# The HTTP response status code: 501 Not Implemented +public const int STATUS_NOT_IMPLEMENTED = 501; +# The HTTP response status code: 502 Bad Gateway +public const int STATUS_BAD_GATEWAY = 502; +# The HTTP response status code: 503 Service Unavailable +public const int STATUS_SERVICE_UNAVAILABLE = 503; +# The HTTP response status code: 504 Gateway Timeout +public const int STATUS_GATEWAY_TIMEOUT = 504; +# The HTTP response status code: 505 HTTP Version Not Supported +public const int STATUS_HTTP_VERSION_NOT_SUPPORTED = 505; diff --git a/native/src/main/java/io/ballerina/stdlib/azure/functions/Constants.java b/native/src/main/java/io/ballerina/stdlib/azure/functions/Constants.java index d8cf80cc..b47c6e2d 100644 --- a/native/src/main/java/io/ballerina/stdlib/azure/functions/Constants.java +++ b/native/src/main/java/io/ballerina/stdlib/azure/functions/Constants.java @@ -26,4 +26,17 @@ public interface Constants { String PACKAGE_NAME = "azure_functions"; String SERVICE_OBJECT = "AZURE_FUNCTION_SERVICE_OBJECT"; + + String QUEUE_OUTPUT = "QueueOutput"; + String COSMOS_DBOUTPUT = "CosmosDBOutput"; + String OUT_MSG = "outMsg"; + String HTTP_OUTPUT = "HTTPOutput"; + String STATUS = "status"; + String CODE = "code"; + String STATUS_CODE = "statusCode"; + String BODY = "body"; + String HEADERS = "headers"; + String CONTENT_TYPE = "content-type"; + String MEDIA_TYPE = "mediaType"; + String RESP = "resp"; } diff --git a/native/src/main/java/io/ballerina/stdlib/azure/functions/FunctionCallback.java b/native/src/main/java/io/ballerina/stdlib/azure/functions/FunctionCallback.java index a51c31e1..db768689 100644 --- a/native/src/main/java/io/ballerina/stdlib/azure/functions/FunctionCallback.java +++ b/native/src/main/java/io/ballerina/stdlib/azure/functions/FunctionCallback.java @@ -27,12 +27,15 @@ import io.ballerina.runtime.api.creators.ValueCreator; import io.ballerina.runtime.api.types.Type; import io.ballerina.runtime.api.utils.StringUtils; +import io.ballerina.runtime.api.utils.TypeUtils; import io.ballerina.runtime.api.values.BError; import io.ballerina.runtime.api.values.BMap; +import io.ballerina.runtime.api.values.BObject; import io.ballerina.runtime.api.values.BString; import java.util.ArrayList; import java.util.List; +import java.util.Locale; import static io.ballerina.runtime.api.utils.StringUtils.fromString; @@ -41,10 +44,12 @@ */ public class FunctionCallback implements Callback { + private final Future future; private final Module module; private final List annotations; + public FunctionCallback(Future future, Module module, Object[] annotations) { this.future = future; this.module = module; @@ -68,24 +73,73 @@ public void notifySuccess(Object result) { } BMap mapValue = ValueCreator.createMapValue(TypeCreator.createMapType(PredefinedTypes.TYPE_ANYDATA)); -// if (result instanceof BArray) { -// BArray result1 = (BArray) result; -// Object[] values = result1.getValues(); -// for (int i = 0; i < values.length; i++) { -// Object obj = values[i]; -// String identifier = generateUniqueIdentifier(i); -// mapValue.put(StringUtils.fromString(identifier), obj); -// } -// future.complete(mapValue); -// return; -// } - if (this.annotations.get(0).equals("QueueOutput") || this.annotations.get(0).equals("CosmosDBOutput")) { - mapValue.put(StringUtils.fromString("outMsg"), result); + if (Constants.QUEUE_OUTPUT.equals(this.annotations.get(0)) || + Constants.COSMOS_DBOUTPUT.equals(this.annotations.get(0))) { + + mapValue.put(StringUtils.fromString(Constants.OUT_MSG), result); + // Check HTTPOutput with annotations + } else if (Constants.HTTP_OUTPUT.equals(this.annotations.get(0))) { + //Check HTTPResponse + if (isHTTPResponse(result)) { + BMap resultMap = (BMap) result; + + // Extract status code + BObject status = (BObject) (resultMap.get(StringUtils.fromString(Constants.STATUS))); + Object statusCode = Long.toString(status.getIntValue(StringUtils.fromString(Constants.CODE))); + statusCode = StringUtils.fromString((String) statusCode); + + // Create a BMap for response field + BMap respMap = + ValueCreator.createMapValue(TypeCreator.createMapType(PredefinedTypes.TYPE_ANYDATA)); + respMap.put(StringUtils.fromString(Constants.STATUS_CODE), statusCode); + + // Create body field in the response Map + if (resultMap.containsKey(StringUtils.fromString(Constants.BODY))) { + Object body = resultMap.get(StringUtils.fromString(Constants.BODY)); + respMap.put(StringUtils.fromString(Constants.BODY), body); + } + + // Create header field in the response Map + if (resultMap.containsKey(StringUtils.fromString(Constants.HEADERS))) { + Object headers = resultMap.get(StringUtils.fromString(Constants.HEADERS)); + BMap headersMap = (BMap) headers; + // Add Content-type field in headers if there is not + if (!isContentTypeExist(headersMap)) { + headersMap.put(StringUtils.fromString(Constants.CONTENT_TYPE), + StringUtils.fromString("text/plain")); + } + respMap.put(StringUtils.fromString(Constants.HEADERS), headers); + } else { + // If there is no headers add one with default content-type + Object headers = + ValueCreator.createMapValue(TypeCreator.createMapType(PredefinedTypes.TYPE_ANYDATA)); + ((BMap) headers).put(StringUtils.fromString(Constants.CONTENT_TYPE), + StringUtils.fromString("text/plain")); + respMap.put(StringUtils.fromString(Constants.HEADERS), headers); + + } + + // If there is mediaType replace content-type in headers + if (resultMap.containsKey(StringUtils.fromString(Constants.MEDIA_TYPE))) { + Object headers = resultMap.get(StringUtils.fromString(Constants.HEADERS)); + Object mediaType = resultMap.get(StringUtils.fromString(Constants.MEDIA_TYPE)); + ((BMap) headers).put(StringUtils.fromString(Constants.CONTENT_TYPE), mediaType); + } + mapValue.put(StringUtils.fromString(Constants.RESP), respMap); + + } else { + //Handle result except HTTPResponse cases + BMap respMap = + ValueCreator.createMapValue(TypeCreator.createMapType(PredefinedTypes.TYPE_ANYDATA)); + respMap.put(StringUtils.fromString(Constants.BODY), result); + mapValue.put(StringUtils.fromString(Constants.RESP), respMap); + } } else { + // Handle other output bindings BMap respMap = ValueCreator.createMapValue(TypeCreator.createMapType(PredefinedTypes.TYPE_ANYDATA)); - respMap.put(StringUtils.fromString("body"), result); - mapValue.put(StringUtils.fromString("resp"), respMap); + respMap.put(StringUtils.fromString(Constants.BODY), result); + mapValue.put(StringUtils.fromString(Constants.RESP), respMap); } future.complete(mapValue); } @@ -106,4 +160,22 @@ private boolean isModuleDefinedError(BError error) { String packageName = packageDetails.getName(); return Constants.PACKAGE_ORG.equals(orgName) && Constants.PACKAGE_NAME.equals(packageName); } + + private boolean isHTTPResponse(Object result) { + Module resultPkg = TypeUtils.getType(result).getPackage(); + return (result instanceof BMap) && (((BMap) result).containsKey(fromString(Constants.STATUS))) && + Constants.PACKAGE_ORG.equals(resultPkg.getOrg()) && + Constants.PACKAGE_NAME.equals(resultPkg.getName()); + //TODO : Check inheritance + //(https://github.com/ballerina-platform/module-ballerinax-azure.functions/issues/490) + } + + private boolean isContentTypeExist(BMap headersMap) { + for (BString headerKey : headersMap.getKeys()) { + if (headerKey.getValue().toLowerCase(Locale.ROOT).equals(Constants.CONTENT_TYPE)) { + return true; + } + } + return false; + } }