Skip to content

Commit

Permalink
feat: http request
Browse files Browse the repository at this point in the history
  • Loading branch information
c0utin committed Sep 28, 2024
1 parent 6e86224 commit 27c9d07
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 3 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/mops-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: mops test

on:
push:
branches:
- main
- master
pull_request:

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: ZenVoich/setup-mops@v1

- name: make sure moc is installed
run: mops toolchain bin moc || mops toolchain use moc latest

- name: run tests
run: mops test
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ dist/

# environment variables
.env

.mops
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Rafael Coutinho

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2 changes: 1 addition & 1 deletion dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"defaults": {
"build": {
"args": "",
"packtool": ""
"packtool": "mops sources"
}
},
"output_env_file": ".env",
Expand Down
10 changes: 10 additions & 0 deletions mops.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "parsekoto"
version = "1.0.0"
description = "csv parser for motoko"
repository = "https://github.com/c0utin/parsekoto"
keywords = [ "csv", "parser", "serde", "ai", "data-science", "data" ]
license = "MIT"

[dependencies]
base = "0.13.0"
73 changes: 71 additions & 2 deletions src/parsekoto_backend/main.mo
Original file line number Diff line number Diff line change
@@ -1,5 +1,74 @@
import Debug "mo:base/Debug";
import Blob "mo:base/Blob";
import Cycles "mo:base/ExperimentalCycles";
import Error "mo:base/Error";
import Array "mo:base/Array";
import Nat8 "mo:base/Nat8";
import Nat64 "mo:base/Nat64";
import Text "mo:base/Text";

import Types "types";

actor {
public query func greet(name : Text) : async Text {
return "Hello, " # name # "!";

public query func transform(raw : Types.TransformArgs) : async Types.CanisterHttpResponsePayload {
let transformed : Types.CanisterHttpResponsePayload = {
status = raw.response.status;
body = raw.response.body;
headers = [
{
name = "Content-Security-Policy";
value = "default-src 'self'";
},
{ name = "Referrer-Policy"; value = "strict-origin" },
{ name = "Permissions-Policy"; value = "geolocation=(self)" },
{
name = "Strict-Transport-Security";
value = "max-age=63072000";
},
{ name = "X-Frame-Options"; value = "DENY" },
{ name = "X-Content-Type-Options"; value = "nosniff" },
];
};
transformed;
};

public func get_icp_usd_exchange(url: Text) : async Text {

let ic : Types.IC = actor ("aaaaa-aa");


let request_headers = [
{ name = "Host"; value = "raw.githubusercontent.com" },
{ name = "User-Agent"; value = "exchange_rate_canister" },
];


let transform_context : Types.TransformContext = {
function = transform;
context = Blob.fromArray([]);
};

let http_request : Types.HttpRequestArgs = {
url = url;
max_response_bytes = null;
headers = request_headers;
body = null;
method = #get;
transform = ?transform_context;
};

Cycles.add(230_949_972_000);

let http_response : Types.HttpResponsePayload = await ic.http_request(http_request);

let response_body: Blob = Blob.fromArray(http_response.body);
let decoded_text: Text = switch (Text.decodeUtf8(response_body)) {
case (null) { "No value returned" };
case (?y) { y };
};

decoded_text
};

};
56 changes: 56 additions & 0 deletions src/parsekoto_backend/types.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
module Types {

public type Timestamp = Nat64;

public type HttpRequestArgs = {
url : Text;
max_response_bytes : ?Nat64;
headers : [HttpHeader];
body : ?[Nat8];
method : HttpMethod;
transform : ?TransformRawResponseFunction;
};

public type HttpHeader = {
name : Text;
value : Text;
};

public type HttpMethod = {
#get;
#post;
#head;
};

public type HttpResponsePayload = {
status : Nat;
headers : [HttpHeader];
body : [Nat8];
};

public type TransformRawResponseFunction = {
function : shared query TransformArgs -> async HttpResponsePayload;
context : Blob;
};

public type TransformArgs = {
response : HttpResponsePayload;
context : Blob;
};

public type CanisterHttpResponsePayload = {
status : Nat;
headers : [HttpHeader];
body : [Nat8];
};

public type TransformContext = {
function : shared query TransformArgs -> async HttpResponsePayload;
context : Blob;
};

public type IC = actor {
http_request : HttpRequestArgs -> async HttpResponsePayload;
};

}

0 comments on commit 27c9d07

Please sign in to comment.