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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
#define _POSIX_C_SOURCE 200809L
#include <err.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "client.h"
#include "http.h"
#include "log.h"
#include "string-util.h"
GSClient *gs_client_new(const char *server, const char *client_id,
const char *client_secret)
{
GSClient *c;
c = calloc(1, sizeof(GSClient));
if (!c) {
err(1, "gs_new_client");
return NULL;
}
c->server = strdup(server);
c->client_id = strdup(client_id);
c->client_secret = strdup(client_secret);
return c;
}
void *gs_client_do_api(GSClient *c, int method, const char *url,
const char *req)
{
char *resp;
size_t s = strlen(c->server) + strlen(url) + 1;
char full_url[s];
strlcpy(full_url, c->server, s);
strlcat(full_url, url, s);
switch (method) {
case 1:
resp = gs_http_get(full_url, c->access_token);
break;
case 2:
resp = gs_http_post(full_url, c->access_token, req);
break;
default:
gs_log(GS_WARNING, "gs_client_do_api", "invalid api method");
return NULL;
}
return resp;
}
void gs_client_set_token(GSClient *c, const char *access_token)
{
c->access_token = strdup(access_token);
}
void gs_client_free(GSClient *c)
{
if (!c)
return;
if (c->server)
free(c->server);
if (c->client_id)
free(c->client_id);
if (c->client_secret)
free(c->client_secret);
if (c->access_token)
free(c->access_token);
}
|