-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRestClient.cpp
94 lines (80 loc) · 2.84 KB
/
RestClient.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
//
// RestClient.cpp
// zeron
//
// Created by Atsushi Yoshida on 2014/07/09.
//
//
#include "RestClient.h"
#include <future>
#include "curl/curl.h"
#include "Env.h"
#include <iostream>
const std::string RestClient::URL_PREFIX = API_URL;
const std::string RestClient::ASSET_URL_PREFIX = ASSET_URL;
RestClient* RestClient::getInstance()
{
static RestClient s_instance;
return &s_instance;
}
void RestClient::post(const std::string path, const ccHttpRequestCallback callback)
{
cocos2d::log("POST to %s", path.c_str());
cocos2d::log("%s", RestClient::URL_PREFIX.c_str());
cocos2d::log("%s", RestClient::ASSET_URL_PREFIX.c_str());
auto* request = new HttpRequest();
request->setUrl((RestClient::URL_PREFIX + path).c_str());
request->setRequestType(HttpRequest::Type::POST);
request->setResponseCallback(callback);
HttpClient::getInstance()->send(request);
request->release();
}
void RestClient::post(const std::string& path, PostData& postData, const ccHttpRequestCallback callback)
{
send(HttpRequest::Type::POST, path, postData, callback);
}
void RestClient::get(const std::string path, const ccHttpRequestCallback callback)
{
cocos2d::log("%s", RestClient::URL_PREFIX.c_str());
cocos2d::log("%s", RestClient::ASSET_URL_PREFIX.c_str());
auto* request = new HttpRequest();
std::string url;
if(path.substr(0, 4) == "http"){
request->setUrl(path.c_str());
}else{
request->setUrl((RestClient::URL_PREFIX + path).c_str());
}
request->setRequestType(HttpRequest::Type::GET);
request->setResponseCallback(callback);
HttpClient::getInstance()->send(request);
request->release();
}
void RestClient::put(const std::string& path, PostData& postData, const ccHttpRequestCallback callback)
{
send(HttpRequest::Type::PUT, path, postData, callback);
}
void RestClient::destroy(const std::string& path, PostData& postData, const ccHttpRequestCallback callback)
{
send(HttpRequest::Type::DELETE, path, postData, callback);
}
void RestClient::send(HttpRequest::Type type, const std::string& path, PostData& postData, const ccHttpRequestCallback& callback)
{
std::string postString;
for(auto pair: postData){
std::string k = std::get<0>(pair);
std::string v = std::get<1>(pair);
if(postString.length() > 0){
postString += "&";
}
postString += curl_escape(k.c_str(), static_cast<int>(k.length()));
postString += "=";
postString += curl_escape(v.c_str(), static_cast<int>(v.length()));
}
auto* request = new HttpRequest();
request->setUrl((RestClient::URL_PREFIX + path).c_str());
request->setRequestType(type);
request->setResponseCallback(callback);
request->setRequestData(postString.c_str(), postString.length());
HttpClient::getInstance()->send(request);
request->release();
}