-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathurlrequests.cpp
90 lines (81 loc) · 2.54 KB
/
urlrequests.cpp
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "provable/eos_api.hpp"
class urlrequests : public eosio::contract
{
private:
void request(
const std::string _query,
const std::string _method,
const std::string _url,
const std::string _kwargs
)
{
std::vector<std::vector<unsigned char>> args = {
string_to_vector(_query),
string_to_vector(_method),
string_to_vector(_url),
string_to_vector(_kwargs)
};
std::vector<unsigned char> myquery = provable_set_computation_args(args);
provable_query("computation", myquery);
}
public:
using contract::contract;
urlrequests(eosio::name receiver, eosio::name code, datastream<const char*> ds) : contract(receiver, code, ds) {}
[[eosio::action]]
void reqheadscust()
{
print("Sending query to Provable...");
request("json(QmdKK319Veha83h6AYgQqhx9YRsJ9MJE7y33oCXyZ4MqHE).headers",
"GET",
"http://httpbin.org/headers",
"{'headers': {'content-type': 'json'}}"
);
}
[[eosio::action]]
void reqbasauth()
{
request("QmdKK319Veha83h6AYgQqhx9YRsJ9MJE7y33oCXyZ4MqHE",
"GET",
"http://httpbin.org/basic-auth/myuser/secretpass",
"{'auth': ('myuser','secretpass'), 'headers': {'content-type': 'json'}}"
);
}
[[eosio::action]]
void reqpost()
{
request("QmdKK319Veha83h6AYgQqhx9YRsJ9MJE7y33oCXyZ4MqHE",
"POST",
"https://api.postcodes.io/postcodes",
"{\"json\": {\"postcodes\" : [\"OX49 5NU\"]}}"
);
}
[[eosio::action]]
void reqput()
{
request("QmdKK319Veha83h6AYgQqhx9YRsJ9MJE7y33oCXyZ4MqHE",
"PUT",
"http://httpbin.org/anything",
"{'json' : {'testing':'it works'}}"
);
}
[[eosio::action]]
void reqcookies()
{
request("QmdKK319Veha83h6AYgQqhx9YRsJ9MJE7y33oCXyZ4MqHE",
"GET",
"http://httpbin.org/cookies",
"{'cookies' : {'thiscookie':'should be saved and visible :)'}}"
);
}
[[eosio::action]]
void callback(
const eosio::checksum256 queryId,
const std::vector<unsigned char> result,
const std::vector<unsigned char> proof
)
{
require_auth(provable_cbAddress());
const std::string result_str = vector_to_string(result);
print("Response: ", result_str);
}
};