This repository has been archived by the owner on Sep 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.cpp
181 lines (143 loc) · 3.52 KB
/
Main.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# include "HTTPClient.hpp"
# include <Siv3D.hpp> // OpenSiv3D v0.4.3
std::string CreateTestJSONData()
{
JSONWriter json;
json.startObject();
{
json.key(U"body").write(U"Hello, Siv3D!");
json.key(U"date").write(DateTime::Now().format());
}
json.endObject();
return json.get().toUTF8();
}
void Main()
{
if (!SimpleHTTP::InitCURL())
{
return;
}
# if 0
//
// HTTP GET
//
const FilePath localFilePath = U"logo.png";
if (const HTTPResponse response = SimpleHTTP::DownloadFile(U"https://raw.githubusercontent.com/Siv3D/siv3d.docs.images/master/logo/logo.png", localFilePath))
{
Print << response.getHeader();
Print << response.getStatusCode();
}
const Texture texture(localFilePath);
while (System::Update())
{
texture.draw();
}
# elif 0
//
// HTTP GET - Bearer Authentication
//
const URL url = U"https://httpbin.org/bearer";
const HTTPHeader header =
{
{ U"Authorization", U"Bearer RequestFromSiv3D" }
};
const FilePath localFilePath = U"resultAuth.json";
if (const HTTPResponse response = SimpleHTTP::Get(url, header, localFilePath))
{
Print << TextReader(localFilePath).readAll();
Print << response.getStatusCode();
}
else
{
Print << U"Failed";
}
while (System::Update())
{
}
# elif 1
//
// HTTP GET - Async Communication
//
const URL url = U"http://httpbin.org/drip?duration=2&numbytes=1000&code=200&delay=0";
const FilePath localFilePath = U"drip.txt";
AsyncHTTPTask task;
Font font(50);
Rect PFrame(Arg::center(Scene::Center()), 400, 50);
double progressPercentage = 0;
while (System::Update())
{
if (SimpleGUI::ButtonAt(U"Download Start!", Scene::Center() - Point(0, 100), unspecified,
task.getProgress().status == HTTPAsyncStatus::None))
{
task = SimpleHTTP::DownloadFileAsync(url, localFilePath);
}
progressPercentage = task.getProgress().getDownloadProgress().value_or(0);
PFrame.drawFrame();
RectF(PFrame.pos, progressPercentage * PFrame.w, 50)
.draw(task.currentStatus() == HTTPAsyncStatus::Succeeded ? Palette::Yellowgreen : Palette::Orange);
font(U"{:.1f}"_fmt(progressPercentage * 100), U"%").drawAt(PFrame.center());
switch (task.currentStatus())
{
case HTTPAsyncStatus::None:
font(U"None").drawAt(Scene::Center() + Point(0, 100));
break;
case HTTPAsyncStatus::Working:
font(U"Downloading").drawAt(Scene::Center() + Point(0, 100));
break;
case HTTPAsyncStatus::Failed:
font(U"Failed").drawAt(Scene::Center() + Point(0, 100));
break;
case HTTPAsyncStatus::Canceled:
font(U"Canceled").drawAt(Scene::Center() + Point(0, 100));
break;
case HTTPAsyncStatus::Succeeded:
font(U"Done!").drawAt(Scene::Center() + Point(0, 100));
break;
default:
break;
}
if (MouseR.down())
{
task.cancelTask();
}
if (task.isDone())
{
if (task.getResponse())
{
BinaryReader reader(localFilePath);
Print << U"received size : " << reader.size();
Print << task.getResponse().getStatusCode();
}
else
{
//通信失敗
Print << U"Failed";
}
}
}
# else
//
// HTTP POST
//
const URL url = U"https://httpbin.org/post";
const HTTPHeader header =
{
{ U"Content-Type", U"application/json" }
};
const std::string json = CreateTestJSONData();
const FilePath localFilePath = U"result.json";
if (const HTTPResponse response = SimpleHTTP::Post(url, header, json.data(), json.size(), localFilePath))
{
Print << TextReader(localFilePath).readAll();
Print << response.getStatusCode();
}
else
{
Print << U"Failed";
}
while (System::Update())
{
}
# endif
SimpleHTTP::CleanupCURL();
}