-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathResponse.cpp
71 lines (51 loc) · 1.61 KB
/
Response.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
#include <Response.h>
namespace HTTP {
Response::Response() :
ok(false),
code(0),
codeClass(0),
elapsed(0),
curlCode(CURLE_OK),
headers(Headers::RESPONSE)
{
}
Response::~Response()
{
}
const char *
Response::toString()
{
if (! this->json.empty()) {
return this->json.c_str();
}
rapidjson::Document doc;
rapidjson::Document::AllocatorType & alloc = doc.GetAllocator();
doc.SetObject();
doc.AddMember("ok", this->ok, alloc);
doc.AddMember("code", this->code, alloc);
doc.AddMember("codeClass", this->codeClass, alloc);
doc.AddMember("curlCode", this->curlCode, alloc);
doc.AddMember("elapsed", this->elapsed, alloc);
rapidjson::Value jsonName;
rapidjson::Value jsonValue;
rapidjson::Value headers(rapidjson::kObjectType);
Headers::const_iterator iter;
for (iter = this->headers.begin(); iter != this->headers.end(); ++iter) {
const std::string & name = iter->first;
const std::string & value = iter->second;
jsonName.SetString(name.data(), name.size(), alloc);
jsonValue.SetString(value.data(), value.size(), alloc);
headers.AddMember(jsonName, jsonValue, alloc);
}
doc.AddMember("headers", headers, alloc);
jsonValue.SetString(this->text.data(), this->text.size(), alloc);
doc.AddMember("text", jsonValue, alloc);
jsonValue.SetString(this->error.data(), this->error.size(), alloc);
doc.AddMember("error", jsonValue, alloc);
rapidjson::StringBuffer buf;
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buf);
doc.Accept(writer);
this->json = buf.GetString();
return this->json.c_str();
}
}