-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
53 lines (41 loc) · 1.13 KB
/
client.cpp
File metadata and controls
53 lines (41 loc) · 1.13 KB
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
#include "client.h"
#include <iostream>
using namespace std;
static auto writeCallback(void *contents, size_t size, size_t nmemb, string *output)
{
size_t total_size = size * nmemb;
output->append((char*)contents, total_size);
return total_size;
}
bool Client::init(const string &token)
{
curl_global_init(CURL_GLOBAL_DEFAULT);
if (!mCurl)
mCurl = curl_easy_init();
if (!mCurl)
return false;
struct curl_slist *headers = NULL;
if (!token.empty())
headers = curl_slist_append(headers, ("Authorization: Discogs token="+token).c_str());
curl_easy_setopt(mCurl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(mCurl, CURLOPT_USERAGENT, "cppdiscogs/1.0");
curl_easy_setopt(mCurl, CURLOPT_WRITEFUNCTION, writeCallback);
return true;
}
string Client::getNext(const string &url)
{
curl_easy_setopt(mCurl, CURLOPT_URL, url.c_str());
string response;
curl_easy_setopt(mCurl, CURLOPT_WRITEDATA, &response);
auto res = curl_easy_perform(mCurl);
if (res != CURLE_OK)
return "";
return response;
}
Client::~Client()
{
std::cout << "\nClosing connection, ciao!...\n";
if (mCurl)
curl_easy_cleanup(mCurl);
curl_global_cleanup();
}