1
0

[Unit Test] SiaCurl::Get() - Basic get test

Added json parser
This commit is contained in:
Scott E. Graves
2017-02-02 21:51:33 -06:00
parent e53ad03dd7
commit 4ef968180d
9 changed files with 12722 additions and 11 deletions

View File

@@ -20,21 +20,25 @@ CStringA CSiaCurl::ConstructPath(const CString& relativePath) const
return ret;
}
SiaCurlError CSiaCurl::Get(const CString& path, CString& result)
SiaCurlError CSiaCurl::Get(const CString& path, json& response)
{
CStringA result;
SiaCurlError ret = SiaCurlError::Success;
curl_easy_setopt(_curlHandle, CURLOPT_USERAGENT, "Sia-Agent");
curl_easy_setopt(_curlHandle, CURLOPT_URL, static_cast<LPCSTR>(ConstructPath(path)));
curl_easy_setopt(_curlHandle, CURLOPT_WRITEFUNCTION, static_cast<size_t(*)(char*, size_t, size_t, void *)>([](char *buffer, size_t size, size_t nitems, void *outstream) -> size_t
{
(*reinterpret_cast<CString*>(outstream)) += CString(reinterpret_cast<LPCSTR>(buffer), size * nitems);
(*reinterpret_cast<CStringA*>(outstream)) += CString(reinterpret_cast<LPCSTR>(buffer), size * nitems);
return size * nitems;
}));
curl_easy_setopt(_curlHandle, CURLOPT_WRITEDATA, &result);
const CURLcode res = curl_easy_perform(_curlHandle);
if (res != CURLE_OK)
{
ret = SiaCurlError::Failed;
}
return SiaCurlError::Success;
response = json::parse((LPCSTR)result);
return ret;
}