Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement HTTP typed resource responses #489

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 61 additions & 24 deletions ballerina-tests/main.bal
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -69,26 +101,27 @@ 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;
}
}

@af:CosmosDBTrigger {connectionStringSetting: "CosmosDBConnection", databaseName: "db1", collectionName: "c2"}
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();
}
}

Expand All @@ -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;
}
}

108 changes: 108 additions & 0 deletions ballerina-tests/tests/resources/httpResTest1.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
108 changes: 108 additions & 0 deletions ballerina-tests/tests/resources/httpResTest2.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
Loading