-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(python): support
cloud.Api
(#231)
- Loading branch information
Showing
13 changed files
with
252 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
bring cloud; | ||
bring http; | ||
bring expect; | ||
bring util; | ||
bring "./lib.w" as python; | ||
|
||
let bucket = new cloud.Bucket(); | ||
let api = new cloud.Api(); | ||
api.get("/test", new python.InflightApiEndpointHandler( | ||
path: "./test-assets", | ||
handler: "main.api_handler", | ||
).lift(bucket, id: "bucket", allow: ["put"])); | ||
|
||
test "invokes api handler" { | ||
let res = http.get("{api.url}/test"); | ||
log(Json.stringify(res)); | ||
expect.equal(res.status, 200); | ||
expect.equal(res.body, "Hello from Api Handler!"); | ||
expect.equal(res.headers["header1"], "value1"); | ||
|
||
util.waitUntil(inflight () => { | ||
return bucket.exists("/test"); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
bring cloud; | ||
bring "./inflight.w" as inflyght; | ||
bring "../types.w" as types; | ||
|
||
pub class InflightApiEndpointHandler extends inflyght.Inflight impl cloud.IApiEndpointHandler { | ||
new(props: types.InflightProps) { | ||
super(props); | ||
} | ||
|
||
pub inflight handle(req: cloud.ApiRequest): cloud.ApiResponse? { | ||
let response = this._handle(Json.stringify(req)); | ||
if let res = Json.tryParse(response) { | ||
let headers = MutMap<str>{}; | ||
for entry in Json.entries(res["headers"]) { | ||
headers.set(entry.key, entry.value.asStr()); | ||
} | ||
return { | ||
status: res["statusCode"].asNum(), | ||
headers: headers.copy(), | ||
body: res["body"].tryAsStr(), | ||
}; | ||
} else { | ||
return nil; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const { Api: TfAwsApi } = require("@winglang/sdk/lib/target-tf-aws/api.js"); | ||
const { App } = require("@winglang/sdk/lib/target-tf-aws/app.js"); | ||
const { Node } = require("@winglang/sdk/lib/std/node.js"); | ||
const awsProvider = require("@cdktf/provider-aws"); | ||
const { Function } = require("./function.js"); | ||
|
||
module.exports.Api = class Api extends TfAwsApi { | ||
addHandler(inflight, method, path) { | ||
if (inflight._inflightType !== "_inflightPython") { | ||
return super.addHandler(inflight, method, path); | ||
} | ||
|
||
let handler = this.handlers[inflight._id]; | ||
if (!handler) { | ||
handler = new Function( | ||
this, | ||
App.of(this).makeId(this, `${this.node.id}-OnMessage`), | ||
inflight, | ||
{}, | ||
); | ||
Node.of(handler).hidden = true; | ||
this.handlers[inflight._id] = handler; | ||
} | ||
|
||
return handler; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
bring cloud; | ||
bring util; | ||
bring "../types.w" as types; | ||
bring "./inflight.w" as inflyght; | ||
|
||
pub class Inflight_tfaws extends inflyght.Inflight_tfaws impl types.IApiOnRequest { | ||
new(props: types.InflightProps) { | ||
super(props); | ||
} | ||
|
||
pub inflight handle(req: cloud.ApiRequest): cloud.ApiResponse? { | ||
|
||
} | ||
|
||
pub lift(obj: std.Resource, options: types.LiftOptions): cloud.IApiEndpointHandler { | ||
this.lifts.set(options.id, { client: obj, options: options }); | ||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters