Sign In Register

How can we help you today?

Start a new topic

C++ connection to Web API

We're uploading data from JSON files on local disk to Metadata tables on the Preview server (and from there out into production). The data is designer driven, so we ideally want to automate this into our build pipeline.

I had a look at Insert Large Data To Metadata Collection( https://support.gamesparks.net/support/discussions/topics/1000073290), which looks like it should be a workable solution. Since our game's C++, I ported the source code to C++.

 

// setup before batch processing files
std::string Pwd = "foo@bar.com:555aintgonnahappen";
Pwd = num::Base64::Encode(Pwd);
std::string Stage = "preview";
std::string GameID = "somegame";
std::string Auth += "{ \"Authorization\" : \"Basic \"" + Pwd + "\" }";
std::string URL = "https://portal.gamesparks.net/rest/games/" + GameID + "/mongo/" + Stage + "/";

And then per file

  

util::FileIO	File;
if (!File.Open(pFile->Filename.Str, util::FileIO::FILE_READ | util::FileIO::FILE_BINARY)) 
{
	_LOG(MSG_ERROR, "unable to open JSON file for loading");
	return "";
}
size_t Size = File.GetSize();
unsigned char*	BufferA = new unsigned char[Size];
File.Read(BufferA, Size);
	
std::string Collection = pFile->Name.Str;
std::string URL = m_URL + Collection + "/insert";

CURL *curl = curl_easy_init();
if (!curl) return "";

curl_easy_setopt(curl, CURLOPT_URL, m_URL.c_str());

curl_slist* pHeader = NULL;
pHeader = curl_slist_append(pHeader, m_Auth.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, pHeader);

// portal will return CURLE_SSL_CACERT if verified
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);

// attach data as POST
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, Size);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, BufferA);

// get feedback
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ReceiveCurlDataCB);

// post mode
curl_easy_setopt(curl, CURLOPT_POST, 1L);

CURLcode Result = curl_easy_perform(curl); 

   At this point it happily returns everything okay, but nothing gets stored.

I've tried both a clean JSON struct (of the kind you'd copy-paste into the insert statement):

 

  [
	{
		"Name" : "bst_combo1",
		"Sprite" : "items/bst_combo1.png",
		"Text" : "bst_combo1_TXT",
		"Description" : "bst_combo1_DESC",
		"ScoreBonus" : "1",
		"TimeBonus" : "1",
		"ComboBonus" : "1.05",
		"Action" : ""
	}	
  ]

 
And data formatted similar to the Insert Large Data example:

 

{
  "document" : 
  [
	{
		"Name" : "bst_combo1",
		"Sprite" : "items/bst_combo1.png",
		"Text" : "bst_combo1_TXT",
		"Description" : "bst_combo1_DESC",
		"ScoreBonus" : "1",
		"TimeBonus" : "1",
		"ComboBonus" : "1.05",
		"Action" : ""
	}	
  ]
}

 No errors, no returned data, no returned error codes.. but nothing is inserted into the metadata database.

Thoughts on what I'm doing wrong?
Thanks,

Allan


Hi Allan,


That topic you linked to show an example using the old REST Api which has since been deprecated. You can instead find examples of how to use the new API's here.

https://docs.gamesparks.com/api-documentation/rest-apis/


-Pádraig

This does look like something you might want to expose in the main C++ API, since I can't imagine we're the only ones interested in doing build-side interaction with the server.

Login to post a comment