-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathListInstance.bal
74 lines (59 loc) · 2.35 KB
/
ListInstance.bal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import ballerina/http;
import ballerina/io;
import ballerina/time;
import ballerina/system;
import ballerina/crypto;
import ballerina/file;
import ballerina/log;
import ballerina/mime;
http:Client clientEndpoint = new("https://iaas.us-ashburn-1.oraclecloud.com");
function checkEncode(string str) returns string|error {
string resultStr = check http:encode(str, "UTF-8");
return resultStr;
}
public function main() {
string compartmentId = <YOUR COMPARTMENT ID>;
http:Request request = new;
string date;
time:Time time = time:currentTime().toTimezone("GMT");
date = time.format("E, dd MMM yyyy HH:mm:ss") + " GMT";
request.setHeader("date", date);
string partReqTarget = "get /20160918/instances/?compartmentId=";
var value = http:encode(compartmentId, "UTF-8");
string encodedString;
if (value is string) {
encodedString = value;
} else {
error err = error("100", { message: "Error occurred when converting to int"});
panic err;
}
string requestURI = "/20160918/instances/?compartmentId=" + encodedString;
string reqTarget = partReqTarget + encodedString;
request.setHeader("request-target", reqTarget);
string host = "iaas.us-ashburn-1.oraclecloud.com";
request.setHeader("host", host);
// api key id
string tenancyId= <YOUR TENANCY ID>;
string authUserId= <YOUR USER ID>;
string keyFingerprint = <YOUR FINGERPRINT ID>;
string apiKeyId = tenancyId + "/" + authUserId + "/" + keyFingerprint;
string pathToKey = <YOUR PATH TO PRIVATE KEY>;
string uriString = "https://iaas.us-ashburn-1.oraclecloud.com" + requestURI;
string authHeader = crypto:rsa(uriString, pathToKey, apiKeyId, "GET", "SHA256");
io:println("authHeader from RSA: ", authHeader);
authHeader = authHeader.replace("Authorization: ", "");
request.setHeader("Authorization", authHeader);
// Send a GET request to the specified endpoint.
var response = clientEndpoint->get(requestURI, message = request);
if (response is http:Response) {
io:println("GET request:");
var msg = response.getJsonPayload();
if (msg is json) {
io:println(msg);
} else {
log:printError("Invalid payload received", err = msg);
}
} else {
log:printError("Error when calling the backend", err = response);
}
}