1
Fork 0
mirror of https://github.com/SapphireServer/Sapphire.git synced 2025-04-29 07:37:45 +00:00
sapphire/src/servers/sapphire_lobby/RestConnector.cpp

324 lines
7.4 KiB
C++
Raw Normal View History

2017-08-08 13:53:47 +02:00
#include "RestConnector.h"
#include "LobbySession.h"
#include "ServerLobby.h"
2018-03-06 22:22:19 +01:00
#include <Logging/Logger.h>
#include <Crypt/base64.h>
2017-08-08 13:53:47 +02:00
#include <time.h>
#include <iomanip>
2018-10-26 20:36:11 +11:00
#include <nlohmann/json.hpp>
2017-08-08 13:53:47 +02:00
extern Core::Logger g_log;
2018-10-26 21:00:50 +11:00
typedef std::vector< std::tuple< std::string, uint32_t, uint64_t, std::string > > CharList;
2017-08-08 13:53:47 +02:00
Core::Network::RestConnector::RestConnector()
{
}
Core::Network::RestConnector::~RestConnector()
{
}
HttpResponse Core::Network::RestConnector::requestApi( std::string endpoint, std::string data )
{
HttpClient client( restHost );
std::string reqstr = "/sapphire-api/lobby/" + endpoint;
HttpResponse r;
try
{
r = client.request( "POST", reqstr, data );
}
catch( std::exception& e )
{
g_log.error( endpoint + " failed, REST is not reachable: " + std::string( e.what() ) );
return nullptr;
}
return r;
2017-08-08 13:53:47 +02:00
}
Core::LobbySessionPtr Core::Network::RestConnector::getSession( char* sId )
{
std::string json_string = "{\"sId\": \"" + std::string( sId ) + "\",\"secret\": \"" + serverSecret + "\"}";
2017-08-08 13:53:47 +02:00
HttpResponse r = requestApi( "checkSession", json_string );
2017-08-08 13:53:47 +02:00
if( r == nullptr )
return nullptr;
2017-08-08 13:53:47 +02:00
std::string content = std::string( std::istreambuf_iterator< char >( r->content ), {} );
if( r->status_code.find( "200" ) != std::string::npos )
{
2018-10-26 20:36:11 +11:00
nlohmann::json json;
2017-08-08 13:53:47 +02:00
try
{
2018-10-26 22:14:12 +11:00
json = json.parse( content );
}
catch( std::exception& e )
{
g_log.debug( "Could not parse REST response: " + std::string( e.what() ) );
return nullptr;
}
if( content.find( "invalid" ) == std::string::npos )
{
LobbySessionPtr pSession( new Core::LobbySession() );
2018-10-26 20:36:11 +11:00
pSession->setAccountID( json["result"].get< uint32_t >() );
pSession->setSessionId( ( uint8_t* ) sId );
return pSession;
}
else
{
2017-08-08 13:53:47 +02:00
return nullptr;
}
}
else
{
return nullptr;
}
2017-08-08 13:53:47 +02:00
}
bool Core::Network::RestConnector::checkNameTaken( std::string name )
{
std::string json_string = "{\"name\": \"" + name + "\",\"secret\": \"" + serverSecret + "\"}";
2017-08-08 13:53:47 +02:00
HttpResponse r = requestApi( "checkNameTaken", json_string );
2017-08-08 13:53:47 +02:00
if( r == nullptr )
return true;
2017-08-08 13:53:47 +02:00
std::string content = std::string( std::istreambuf_iterator< char >( r->content ), {} );
if( r->status_code.find( "200" ) != std::string::npos )
{
2018-10-26 21:00:50 +11:00
auto json = nlohmann::json();
2017-08-08 13:53:47 +02:00
try
{
2018-10-26 22:14:12 +11:00
json = json.parse( content );
}
catch( std::exception& e )
{
g_log.debug( "Could not parse REST response: " + std::string( e.what() ) );
2017-08-08 13:53:47 +02:00
return true;
}
2018-10-26 21:00:50 +11:00
if( json["result"] != "invalid" && json["result"] == "false" )
return false;
return true;
}
else
{
return true;
}
2017-08-08 13:53:47 +02:00
}
uint32_t Core::Network::RestConnector::getNextCharId()
{
std::string json_string = "{\"secret\": \"" + serverSecret + "\"}";
2017-08-08 13:53:47 +02:00
HttpResponse r = requestApi( "getNextCharId", json_string );
2017-08-08 13:53:47 +02:00
if( r == nullptr )
return -1;
2017-08-08 13:53:47 +02:00
std::string content = std::string( std::istreambuf_iterator< char >( r->content ), {} );
if( r->status_code.find( "200" ) != std::string::npos )
{
2018-10-26 21:00:50 +11:00
auto json = nlohmann::json();
2017-08-08 13:53:47 +02:00
try
{
2018-10-26 22:14:12 +11:00
json = json.parse( content );
}
catch( std::exception& e )
{
g_log.debug( "Could not parse REST response: " + std::string( e.what() ) );
return -1;
}
if( content.find( "invalid" ) == std::string::npos )
{
2018-10-26 21:00:50 +11:00
return json["result"].get< uint32_t >();
}
else
{
2017-08-08 13:53:47 +02:00
return -1;
}
}
else
{
return -1;
}
2017-08-08 13:53:47 +02:00
}
uint64_t Core::Network::RestConnector::getNextContentId()
{
std::string json_string = "{\"secret\": \"" + serverSecret + "\"}";
2017-08-08 13:53:47 +02:00
HttpResponse r = requestApi( "getNextContentId", json_string );
2017-08-08 13:53:47 +02:00
if( r == nullptr )
return -1;
2017-08-08 13:53:47 +02:00
std::string content = std::string( std::istreambuf_iterator< char >( r->content ), {} );
if( r->status_code.find( "200" ) != std::string::npos )
{
2018-10-26 21:00:50 +11:00
auto json = nlohmann::json();
2017-08-08 13:53:47 +02:00
try
{
2018-10-26 22:14:12 +11:00
json = json.parse( content );
}
catch( std::exception& e )
{
g_log.debug( "Could not parse REST response: " + std::string( e.what() ) );
return -1;
}
if( content.find( "invalid" ) == std::string::npos )
{
2018-10-26 21:00:50 +11:00
return json["result"].get< uint64_t >();
}
else
{
2017-08-08 13:53:47 +02:00
return -1;
}
}
else
{
return -1;
}
2017-08-08 13:53:47 +02:00
}
CharList Core::Network::RestConnector::getCharList( char* sId )
2017-08-08 13:53:47 +02:00
{
std::string json_string = "{\"sId\": \"" + std::string( sId, 56 ) + "\",\"secret\": \"" + serverSecret + "\"}";
HttpResponse r = requestApi( "getCharacterList", json_string );
CharList list;
if( r == nullptr )
return list;
std::string content = std::string( std::istreambuf_iterator< char >( r->content ), {} );
g_log.debug( content );
if( r->status_code.find( "200" ) != std::string::npos )
{
2018-10-26 21:00:50 +11:00
auto json = nlohmann::json();
try
{
2018-10-26 22:14:12 +11:00
json = json.parse( content );
}
catch( std::exception& e )
{
g_log.debug( "Could not parse REST response: " + std::string( e.what() ) );
2017-08-08 13:53:47 +02:00
return list;
}
2017-08-08 13:53:47 +02:00
2018-10-26 21:00:50 +11:00
if( json["result"].get< std::string >().find( "invalid" ) == std::string::npos )
{
2017-08-08 13:53:47 +02:00
2018-10-26 21:00:50 +11:00
g_log.debug( json["result"] );
2017-08-08 13:53:47 +02:00
2018-10-26 21:00:50 +11:00
for( auto& child : json["charArray"] )
2017-08-08 13:53:47 +02:00
{
2018-10-26 21:00:50 +11:00
g_log.debug( child["contentId"] );
//std::string, uint32_t, uint64_t, std::string
2018-10-26 22:14:12 +11:00
list.push_back( { child["name"].get< std::string >(),
child["charId"].get< uint32_t >(),
child["contentId"].get< uint64_t >(),
child["infoJson"].get< std::string >()
} );
2017-08-08 13:53:47 +02:00
}
return list;
2017-08-08 13:53:47 +02:00
}
else
{
2017-08-08 13:53:47 +02:00
return list;
}
}
else
{
return list;
}
2017-08-08 13:53:47 +02:00
}
bool Core::Network::RestConnector::deleteCharacter( char* sId, std::string name )
{
std::string json_string =
"{\"sId\": \"" + std::string( sId, 56 ) + "\",\"secret\": \"" + serverSecret + "\",\"name\": \"" + name + "\"}";
HttpResponse r = requestApi( "deleteCharacter", json_string );
if( r == nullptr )
return false;
std::string content = std::string( std::istreambuf_iterator< char >( r->content ), {} );
if( r->status_code.find( "200" ) != std::string::npos )
{
2018-10-26 21:00:50 +11:00
auto json = nlohmann::json();
try
{
2018-10-26 22:14:12 +11:00
json = json.parse( content );
}
catch( std::exception& e )
{
g_log.debug( "Could not parse REST response: " + std::string( e.what() ) );
2017-08-08 13:53:47 +02:00
return false;
}
2017-08-08 13:53:47 +02:00
if( content.find( "invalid" ) == std::string::npos )
return true;
return false;
}
else
{
return false;
}
2017-08-08 13:53:47 +02:00
}
int Core::Network::RestConnector::createCharacter( char* sId, std::string name, std::string infoJson )
2017-08-08 13:53:47 +02:00
{
std::string json_string =
"{\"sId\": \"" + std::string( sId, 56 ) + "\",\"secret\": \"" + serverSecret + "\",\"name\": \"" + name +
"\",\"infoJson\": \"" + Core::Util::base64_encode( ( uint8_t* ) infoJson.c_str(), infoJson.length() ) + "\"}";
HttpResponse r = requestApi( "createCharacter", json_string );
if( r == nullptr )
return -1;
std::string content = std::string( std::istreambuf_iterator< char >( r->content ), {} );
g_log.debug( content );
if( r->status_code.find( "200" ) != std::string::npos )
{
2018-10-26 21:00:50 +11:00
auto json = nlohmann::json();
try
{
2018-10-26 22:14:12 +11:00
json = json.parse( content );
}
catch( std::exception& e )
{
g_log.debug( "Could not parse REST response: " + std::string( e.what() ) );
2017-08-08 13:53:47 +02:00
return -1;
}
if( content.find( "invalid" ) == std::string::npos )
2018-10-26 21:00:50 +11:00
return json["result"].get< int >();
return -1;
}
else
{
return -1;
}
2017-10-27 09:06:16 +02:00
}