mirror of
https://github.com/UltraCoderRU/telebotxx.git
synced 2026-01-28 04:05:13 +00:00
JSON-parsing functions moved to namespace telebotxx.
This commit is contained in:
parent
1c2c8384bc
commit
1e43989b77
1 changed files with 64 additions and 62 deletions
126
src/BotApi.cpp
126
src/BotApi.cpp
|
|
@ -13,70 +13,72 @@
|
|||
|
||||
#include <boost/log/trivial.hpp>
|
||||
|
||||
namespace telebotxx
|
||||
{
|
||||
const rapidjson::Value& parseResponse(const rapidjson::Document& doc)
|
||||
{
|
||||
using namespace rapidjson;
|
||||
if (!doc.IsObject())
|
||||
throw ParseError("Object expected");
|
||||
|
||||
// Get status
|
||||
if (!doc.HasMember("ok") || !doc["ok"].IsBool())
|
||||
throw ParseError("Field 'ok' not found or has invalid type");
|
||||
bool ok = doc["ok"].GetBool();
|
||||
|
||||
if (ok)
|
||||
{
|
||||
if (!doc.HasMember("result") || !doc["result"].IsObject())
|
||||
throw ParseError("Field 'result' not found or has invalid type");
|
||||
return doc["result"];
|
||||
} else
|
||||
{
|
||||
if (!doc.HasMember("error_code") || !doc["error_code"].IsInt())
|
||||
throw ParseError("Field 'error_code' not found or has invalid type");
|
||||
int code = doc["error_code"].GetInt();
|
||||
|
||||
if (!doc.HasMember("description") || !doc["description"].IsString())
|
||||
throw ParseError("Field 'description' not found or has invalid type");
|
||||
std::string description(doc["description"].GetString());
|
||||
|
||||
throw ApiError(code, description);
|
||||
}
|
||||
}
|
||||
|
||||
User parseUser(const rapidjson::Value& obj)
|
||||
{
|
||||
if (!obj.HasMember("id") || !obj["id"].IsInt())
|
||||
throw ParseError("Field 'id' not found or has invalid type");
|
||||
int id = obj["id"].GetInt();
|
||||
|
||||
if (!obj.HasMember("first_name") || !obj["first_name"].IsString())
|
||||
throw ParseError("Field 'first_name' not found or has invalid type");
|
||||
std::string firstName(obj["first_name"].GetString());
|
||||
|
||||
std::string lastName;
|
||||
if (obj.HasMember("last_name"))
|
||||
{
|
||||
if (obj["last_name"].IsString())
|
||||
lastName = obj["last_name"].GetString();
|
||||
else
|
||||
throw ParseError("Field 'last_name' has invalid type");
|
||||
}
|
||||
|
||||
std::string username;
|
||||
if (obj.HasMember("username"))
|
||||
{
|
||||
if (obj["username"].IsString())
|
||||
username = obj["username"].GetString();
|
||||
else
|
||||
throw ParseError("Field 'username' has invalid type");
|
||||
}
|
||||
|
||||
return User(id, firstName, lastName, username);
|
||||
}
|
||||
}
|
||||
|
||||
using namespace telebotxx;
|
||||
|
||||
const rapidjson::Value& parseResponse(const rapidjson::Document& doc)
|
||||
{
|
||||
using namespace rapidjson;
|
||||
if (!doc.IsObject())
|
||||
throw ParseError("Object expected");
|
||||
|
||||
// Get status
|
||||
if (!doc.HasMember("ok") || !doc["ok"].IsBool())
|
||||
throw ParseError("Field 'ok' not found or has invalid type");
|
||||
bool ok = doc["ok"].GetBool();
|
||||
|
||||
if (ok)
|
||||
{
|
||||
if (!doc.HasMember("result") || !doc["result"].IsObject())
|
||||
throw ParseError("Field 'result' not found or has invalid type");
|
||||
return doc["result"];
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!doc.HasMember("error_code") || !doc["error_code"].IsInt())
|
||||
throw ParseError("Field 'error_code' not found or has invalid type");
|
||||
int code = doc["error_code"].GetInt();
|
||||
|
||||
if (!doc.HasMember("description") || !doc["description"].IsString())
|
||||
throw ParseError("Field 'description' not found or has invalid type");
|
||||
std::string description(doc["description"].GetString());
|
||||
|
||||
throw ApiError(code, description);
|
||||
}
|
||||
}
|
||||
|
||||
User parseUser(const rapidjson::Value& obj)
|
||||
{
|
||||
if (!obj.HasMember("id") || !obj["id"].IsInt())
|
||||
throw ParseError("Field 'id' not found or has invalid type");
|
||||
int id = obj["id"].GetInt();
|
||||
|
||||
if (!obj.HasMember("first_name") || !obj["first_name"].IsString())
|
||||
throw ParseError("Field 'first_name' not found or has invalid type");
|
||||
std::string firstName(obj["first_name"].GetString());
|
||||
|
||||
std::string lastName;
|
||||
if (obj.HasMember("last_name"))
|
||||
{
|
||||
if (obj["last_name"].IsString())
|
||||
lastName = obj["last_name"].GetString();
|
||||
else
|
||||
throw ParseError("Field 'last_name' has invalid type");
|
||||
}
|
||||
|
||||
std::string username;
|
||||
if (obj.HasMember("username"))
|
||||
{
|
||||
if (obj["username"].IsString())
|
||||
username = obj["username"].GetString();
|
||||
else
|
||||
throw ParseError("Field 'username' has invalid type");
|
||||
}
|
||||
|
||||
return User(id, firstName, lastName, username);
|
||||
}
|
||||
|
||||
class BotApi::Impl
|
||||
{
|
||||
public:
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue