This repository has been archived by the owner on Apr 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from kaviththiranga/master
Add playground service for handling gists
- Loading branch information
Showing
59 changed files
with
598 additions
and
59 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[project] | ||
org-name= "ballerina.io" | ||
org-name= "ballerina.playground" | ||
version= "1.0.0" | ||
|
||
[dependencies] |
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 |
---|---|---|
@@ -1,19 +1,29 @@ | ||
org_name = "ballerina.io" | ||
org_name = "ballerina.playground" | ||
version = "1.0.0" | ||
lockfile_version = "1.0.0" | ||
ballerina_version = "1.0.3" | ||
|
||
[[imports."ballerina.io/playground_controller:1.0.0"]] | ||
[[imports."ballerina.playground/playground_controller:1.0.0"]] | ||
org_name = "ballerina.playground" | ||
name = "playground_commons" | ||
version = "1.0.0" | ||
|
||
[[imports."ballerina.playground/playground_gists:1.0.0"]] | ||
org_name = "ballerina.playground" | ||
name = "playground_commons" | ||
version = "1.0.0" | ||
|
||
[[imports."ballerinax/java.arrays:0.0.0"]] | ||
org_name = "ballerinax" | ||
name = "java" | ||
version = "0.0.0" | ||
|
||
[[imports."ballerina.io/playground_controller:1.0.0"]] | ||
[[imports."ballerina.playground/playground_commons:1.0.0"]] | ||
org_name = "ballerinax" | ||
name = "java.arrays" | ||
name = "java" | ||
version = "0.0.0" | ||
|
||
[[imports."ballerinax/java.arrays:0.0.0"]] | ||
[[imports."ballerina.playground/playground_commons:1.0.0"]] | ||
org_name = "ballerinax" | ||
name = "java" | ||
name = "java.arrays" | ||
version = "0.0.0" |
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,6 @@ | ||
import ballerina/crypto; | ||
|
||
public function getCacheId(string sourceCode, string balVersion) returns string { | ||
string cacheSource = sourceCode + balVersion; | ||
return crypto:hashMd5(cacheSource.toBytes()).toBase16(); | ||
} |
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,55 @@ | ||
import ballerina/http; | ||
|
||
const API_HOST = "https://api.github.com"; | ||
const GISTS_RESOURCE = "gists"; | ||
|
||
public type GistClient client object { | ||
|
||
string? accessToken; | ||
http:Client restClient; | ||
|
||
public function __init(GithubClientConfig config = {}) { | ||
self.accessToken = config?.token; | ||
self.restClient = new (API_HOST, config?.httpClientConfig); | ||
} | ||
|
||
public remote function createGist(string fileName, | ||
string content, string description) returns @tainted Gist|error { | ||
json reqBody = { | ||
"description": description, | ||
"public": true, | ||
"files": { | ||
[fileName]: { | ||
"content": content | ||
} | ||
} | ||
}; | ||
http:Request req = self.createRequest(reqBody); | ||
var response = check self.restClient->post("/" + GISTS_RESOURCE, req); | ||
return parseResponse(response); | ||
} | ||
|
||
public remote function getGist(string gistId) returns @tainted Gist|error { | ||
http:Request req = self.createRequest(); | ||
var response = check self.restClient->get("/" +GISTS_RESOURCE + "/" + gistId); | ||
return parseResponse(response); | ||
} | ||
|
||
private function createRequest(json payload = ()) returns http:Request { | ||
http:Request req = new(); | ||
if (self.accessToken is string) { | ||
req.setHeader("Authorization", "token " + <string> self.accessToken); | ||
} | ||
req.setHeader("Accept", "application/vnd.github.v3+json"); | ||
if (!(payload is ())) { | ||
req.setJsonPayload(payload); | ||
} | ||
return req; | ||
} | ||
}; | ||
|
||
public type GithubClientConfig record { | ||
string token?; | ||
http:ClientConfiguration httpClientConfig?; | ||
}; | ||
|
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,21 @@ | ||
public type Gist record { | ||
string url; | ||
string id; | ||
string html_url; | ||
boolean truncated; | ||
map<GistFile> files; | ||
string description; | ||
boolean 'public; | ||
string created_at; | ||
string updated_at; | ||
}; | ||
|
||
public type GistFile record { | ||
string filename; | ||
string 'type; | ||
string language; | ||
string raw_url; | ||
int size; | ||
boolean truncated; | ||
string content; | ||
}; |
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,13 @@ | ||
import ballerina/http; | ||
|
||
function parseResponse(http:Response response) returns @tainted Gist|error { | ||
if (response.statusCode == http:STATUS_CREATED | ||
|| response.statusCode == http:STATUS_OK ) { | ||
return Gist.constructFrom(check response.getJsonPayload()); | ||
} else if (response.statusCode == http:STATUS_NOT_FOUND) { | ||
return error("404 Not Found. Check gist id."); | ||
} else { | ||
return error("Unhandled response code: " + response.statusCode.toString() + "\n" | ||
+ response.getTextPayload().toString()); | ||
} | ||
} |
File renamed without changes.
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
Empty file.
Empty file.
21 changes: 0 additions & 21 deletions
21
api/controller/src/playground_controller/utils/cache_utils.bal
This file was deleted.
Oops, something went wrong.
Empty file.
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 @@ | ||
import playground_commons as commons; | ||
import ballerina/system; | ||
|
||
const GITHUB_ACCESS_TOKEN = "GITHUB_ACCESS_TOKEN"; | ||
string token = system:getEnv(GITHUB_ACCESS_TOKEN); | ||
|
||
function createGist(CreateGistRequest createReq) returns @untainted commons:Gist|error { | ||
string fileName = <@untainted> createReq.fileName; | ||
string content = <@untainted> createReq.content; | ||
string description = <@untainted> createReq.description; | ||
commons:GistClient gistClient = new({ token: token }); | ||
return gistClient->createGist(fileName, content, description); | ||
} | ||
|
||
function getGistFile(string gistId, string fileName) returns @untainted commons:GistFile|error { | ||
commons:GistClient gistClient = new; | ||
commons:Gist gist = check gistClient->getGist(gistId); | ||
commons:GistFile gistFile; | ||
map<commons:GistFile> files = gist.files; | ||
if (files.hasKey(fileName)) { | ||
return files.get(fileName); | ||
} else { | ||
return error("Cannot find the file named " | ||
+ fileName + " in gist " + gistId); | ||
} | ||
} |
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,66 @@ | ||
import ballerina/http; | ||
import playground_commons as commons; | ||
import ballerina/log; | ||
|
||
@http:ServiceConfig { | ||
basePath: "/gists" | ||
} | ||
service gistsService on new http:Listener(9090) { | ||
@http:ResourceConfig { | ||
methods: ["POST"], | ||
path: "/", | ||
body: "createReq", | ||
consumes: ["application/json"] | ||
} | ||
resource function create(http:Caller caller, http:Request request, | ||
CreateGistRequest createReq) { | ||
commons:Gist|error gist = createGist(createReq); | ||
if (gist is error) { | ||
respondAndHandleErrors(caller, createErrorResponse(gist)); | ||
} else { | ||
respondAndHandleErrors(caller, gist); | ||
} | ||
} | ||
@http:ResourceConfig { | ||
methods: ["GET"], | ||
path: "/{gistId}/{fileName}" | ||
} | ||
resource function get(http:Caller caller, http:Request request, | ||
string gistId, string fileName) { | ||
commons:GistFile|error gistFile = getGistFile(gistId, fileName); | ||
if (gistFile is error) { | ||
respondAndHandleErrors(caller, createErrorResponse(gistFile)); | ||
} else { | ||
respondAndHandleErrors(caller, gistFile); | ||
} | ||
} | ||
} | ||
|
||
function createErrorResponse(error err) returns http:Response { | ||
http:Response errorResp = new; | ||
errorResp.statusCode = 500; | ||
errorResp.setTextPayload(err.reason() + "\n" + err.detail().toString()); | ||
log:printError("Error: ", err); | ||
return errorResp; | ||
} | ||
|
||
function respondAndHandleErrors(http:Caller caller, | ||
http:Response|commons:Gist|commons:GistFile|json|string response) { | ||
http:Response|json|string resp; | ||
if (response is commons:Gist || response is commons:GistFile) { | ||
json|error createdJson = json.constructFrom(response); | ||
if (createdJson is error) { | ||
string err = "Error while creating json: " + createdJson.reason(); | ||
log:printError(err); | ||
resp = createErrorResponse(error(err)); | ||
} else { | ||
resp = createdJson; | ||
} | ||
} else { | ||
resp = response; | ||
} | ||
error? status = caller->respond(resp); | ||
if (status is error) { | ||
log:printError("Error while responding: " + status.reason()); | ||
} | ||
} |
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,5 @@ | ||
type CreateGistRequest record { | ||
string fileName; | ||
string content; | ||
string description; | ||
}; |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[project] | ||
org-name= "ballerina.io" | ||
org-name= "ballerina.playground" | ||
version= "1.0.0" | ||
|
||
[dependencies] |
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 @@ | ||
!api |
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,2 @@ | ||
api | ||
containerId.txt |
Oops, something went wrong.