Skip to content

Commit 2586e76

Browse files
Merge pull request #1051 from dfinity/fix/remove-http-type-defintions
🔥 remove redundant http type defintions
2 parents a4dc2ae + 23a8cb4 commit 2586e76

File tree

4 files changed

+135
-341
lines changed

4 files changed

+135
-341
lines changed

motoko/send_http_get/src/send_http_get_backend/Types.mo

-71
This file was deleted.

motoko/send_http_get/src/send_http_get_backend/main.mo

+72-100
Original file line numberDiff line numberDiff line change
@@ -2,142 +2,114 @@ import Blob "mo:base/Blob";
22
import Cycles "mo:base/ExperimentalCycles";
33
import Nat64 "mo:base/Nat64";
44
import Text "mo:base/Text";
5-
6-
//import the custom types we have in Types.mo
7-
import Types "Types";
8-
5+
import IC "ic:aaaaa-aa";
96

107
//Actor
118
actor {
129

13-
//This method sends a GET request to a URL with a free API we can test.
14-
//This method returns Coinbase data on the exchange rate between USD and ICP
15-
//for a certain day.
16-
//The API response looks like this:
17-
// [
18-
// [
19-
// 1682978460, <-- start timestamp
20-
// 5.714, <-- lowest price during time range
21-
// 5.718, <-- highest price during range
22-
// 5.714, <-- price at open
23-
// 5.714, <-- price at close
24-
// 243.5678 <-- volume of ICP traded
25-
// ],
26-
// ]
10+
//This method sends a GET request to a URL with a free API we can test.
11+
//This method returns Coinbase data on the exchange rate between USD and ICP
12+
//for a certain day.
13+
//The API response looks like this:
14+
// [
15+
// [
16+
// 1682978460, <-- start timestamp
17+
// 5.714, <-- lowest price during time range
18+
// 5.718, <-- highest price during range
19+
// 5.714, <-- price at open
20+
// 5.714, <-- price at close
21+
// 243.5678 <-- volume of ICP traded
22+
// ],
23+
// ]
2724

2825
//function to transform the response
29-
public query func transform(raw : Types.TransformArgs) : async Types.CanisterHttpResponsePayload {
30-
let transformed : Types.CanisterHttpResponsePayload = {
31-
status = raw.response.status;
32-
body = raw.response.body;
33-
headers = [
34-
{
35-
name = "Content-Security-Policy";
36-
value = "default-src 'self'";
37-
},
38-
{ name = "Referrer-Policy"; value = "strict-origin" },
39-
{ name = "Permissions-Policy"; value = "geolocation=(self)" },
40-
{
41-
name = "Strict-Transport-Security";
42-
value = "max-age=63072000";
43-
},
44-
{ name = "X-Frame-Options"; value = "DENY" },
45-
{ name = "X-Content-Type-Options"; value = "nosniff" },
46-
];
47-
};
48-
transformed;
26+
public query func transform({
27+
context : Blob;
28+
response : IC.http_request_result;
29+
}) : async IC.http_request_result {
30+
{
31+
response with headers = []; // not intersted in the headers
32+
};
4933
};
50-
51-
public func get_icp_usd_exchange() : async Text {
5234

53-
//1. DECLARE IC MANAGEMENT CANISTER
54-
//We need this so we can use it to make the HTTP request
55-
let ic : Types.IC = actor ("aaaaa-aa");
56-
57-
//2. SETUP ARGUMENTS FOR HTTP GET request
35+
public func get_icp_usd_exchange() : async Text {
5836

59-
// 2.1 Setup the URL and its query parameters
37+
//1. SETUP ARGUMENTS FOR HTTP GET request
6038
let ONE_MINUTE : Nat64 = 60;
61-
let start_timestamp : Types.Timestamp = 1682978460; //May 1, 2023 22:01:00 GMT
39+
let start_timestamp : Nat64 = 1682978460; //May 1, 2023 22:01:00 GMT
6240
let host : Text = "api.exchange.coinbase.com";
6341
let url = "https://" # host # "/products/ICP-USD/candles?start=" # Nat64.toText(start_timestamp) # "&end=" # Nat64.toText(start_timestamp) # "&granularity=" # Nat64.toText(ONE_MINUTE);
6442

65-
// 2.2 prepare headers for the system http_request call
43+
// 1.2 prepare headers for the system http_request call
6644
let request_headers = [
67-
{ name = "Host"; value = host # ":443" },
68-
{ name = "User-Agent"; value = "exchange_rate_canister" },
45+
{ name = "User-Agent"; value = "price-feed" },
6946
];
7047

71-
// 2.2.1 Transform context
72-
let transform_context : Types.TransformContext = {
73-
function = transform;
74-
context = Blob.fromArray([]);
75-
};
76-
77-
// 2.3 The HTTP request
78-
let http_request : Types.HttpRequestArgs = {
79-
url = url;
80-
max_response_bytes = null; //optional for request
81-
headers = request_headers;
82-
body = null; //optional for request
83-
method = #get;
84-
transform = ?transform_context;
48+
// 1.3 The HTTP request
49+
let http_request : IC.http_request_args = {
50+
url = url;
51+
max_response_bytes = null; //optional for request
52+
headers = request_headers;
53+
body = null; //optional for request
54+
method = #get;
55+
transform = ?{
56+
function = transform;
57+
context = Blob.fromArray([]);
58+
};
8559
};
8660

87-
//3. ADD CYCLES TO PAY FOR HTTP REQUEST
61+
//2. ADD CYCLES TO PAY FOR HTTP REQUEST
8862

89-
//The IC specification spec says, "Cycles to pay for the call must be explicitly transferred with the call"
9063
//IC management canister will make the HTTP request so it needs cycles
9164
//See: https://internetcomputer.org/docs/current/motoko/main/cycles
92-
65+
9366
//The way Cycles.add() works is that it adds those cycles to the next asynchronous call
94-
//"Function add(amount) indicates the additional amount of cycles to be transferred in the next remote call"
95-
//See: https://internetcomputer.org/docs/current/references/ic-interface-spec/#ic-http_request
67+
//See:
68+
// - https://internetcomputer.org/docs/current/references/ic-interface-spec/#ic-http_request
69+
// - https://internetcomputer.org/docs/current/references/https-outcalls-how-it-works#pricing
70+
// - https://internetcomputer.org/docs/current/developer-docs/gas-cost
9671
Cycles.add<system>(230_949_972_000);
97-
98-
//4. MAKE HTTPS REQUEST AND WAIT FOR RESPONSE
99-
//Since the cycles were added above, we can just call the IC management canister with HTTPS outcalls below
100-
let http_response : Types.HttpResponsePayload = await ic.http_request(http_request);
101-
102-
//5. DECODE THE RESPONSE
103-
104-
//As per the type declarations in `src/Types.mo`, the BODY in the HTTP response
105-
//comes back as [Nat8s] (e.g. [2, 5, 12, 11, 23]). Type signature:
106-
107-
//public type HttpResponsePayload = {
72+
73+
//3. MAKE HTTPS REQUEST AND WAIT FOR RESPONSE
74+
let http_response : IC.http_request_result = await IC.http_request(http_request);
75+
76+
//4. DECODE THE RESPONSE
77+
78+
//As per the type declarations, the BODY in the HTTP response
79+
//comes back as Blob. Type signature:
80+
81+
//public type http_request_result = {
10882
// status : Nat;
10983
// headers : [HttpHeader];
110-
// body : [Nat8];
84+
// body : Blob;
11185
// };
11286

113-
//We need to decode that [Nat8] array that is the body into readable text.
87+
//We need to decode that Blob that is the body into readable text.
11488
//To do this, we:
115-
// 1. Convert the [Nat8] into a Blob
116-
// 2. Use Blob.decodeUtf8() method to convert the Blob to a ?Text optional
117-
// 3. We use a switch to explicitly call out both cases of decoding the Blob into ?Text
118-
let response_body: Blob = Blob.fromArray(http_response.body);
119-
let decoded_text: Text = switch (Text.decodeUtf8(response_body)) {
120-
case (null) { "No value returned" };
121-
case (?y) { y };
89+
// 1. Use Text.decodeUtf8() method to convert the Blob to a ?Text optional
90+
// 2. We use a switch to explicitly call out both cases of decoding the Blob into ?Text
91+
let decoded_text : Text = switch (Text.decodeUtf8(http_response.body)) {
92+
case (null) { "No value returned" };
93+
case (?y) { y };
12294
};
12395

124-
//6. RETURN RESPONSE OF THE BODY
96+
//5. RETURN RESPONSE OF THE BODY
12597
//The API response will looks like this:
126-
98+
//
12799
// ("[[1682978460,5.714,5.718,5.714,5.714,243.5678]]")
128-
129-
//Which can be formatted as this
100+
//
101+
//The API response looks like this:
130102
// [
131103
// [
132-
// 1682978460, <-- start/timestamp
133-
// 5.714, <-- low
134-
// 5.718, <-- high
135-
// 5.714, <-- open
136-
// 5.714, <-- close
137-
// 243.5678 <-- volume
104+
// 1682978460, <-- start timestamp
105+
// 5.714, <-- lowest price during time range
106+
// 5.718, <-- highest price during range
107+
// 5.714, <-- price at open
108+
// 5.714, <-- price at close
109+
// 243.5678 <-- volume of ICP traded
138110
// ],
139111
// ]
140-
decoded_text
112+
decoded_text;
141113
};
142114

143115
};

motoko/send_http_post/src/send_http_post_backend/Types.mo

-68
This file was deleted.

0 commit comments

Comments
 (0)