~tristan957/tllt-cp

ref: 03ccb981a2fb2121f66c1ab73ec3189685ad11b0 tllt-cp/lib/tllt-cp-client.c -rw-r--r-- 9.5 KiB
03ccb981 — Tristan Partin Make the recipe UI work. Everying not toast 1 year, 6 months ago
                                                                                
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include <stdarg.h>
#include <stddef.h>
#include <string.h>

#include <curl/curl.h>
#include <glib-object.h>
#include <glib/gi18n.h>
#include <json-glib/json-glib.h>

#include "tllt-cp-client.h"
#include "tllt-cp-config.h"
#include "tllt-cp-error.h"

G_DEFINE_TYPE(TlltCpClient, tllt_cp_client, G_TYPE_OBJECT)

enum TlltCpClientProps
{
	PROP_SERVER = 1,
	N_PROPS,
};

static GParamSpec *obj_properties[N_PROPS];

static void
tllt_cp_client_get_property(GObject *obj, guint prop_id, GValue *val, GParamSpec *pspec)
{
	TlltCpClient *self = TLLT_CP_CLIENT(obj);

	switch (prop_id) {
	case PROP_SERVER:
		g_value_set_string(val, self->server);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop_id, pspec);
		break;
	}
}

static void
tllt_cp_client_set_property(GObject *obj, guint prop_id, const GValue *val, GParamSpec *pspec)
{
	TlltCpClient *self = TLLT_CP_CLIENT(obj);

	switch (prop_id) {
	case PROP_SERVER:
		g_free(self->server);
		self->server = g_value_dup_string(val);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop_id, pspec);
		break;
	}
}

static void
tllt_cp_client_finalize(GObject *obj)
{
	TlltCpClient *self = TLLT_CP_CLIENT(obj);

	if (self->server != NULL) {
		g_free(self->server);
	}

	if (self->handle != NULL) {
		curl_global_cleanup();
		curl_easy_cleanup(self->handle);
	}

	G_OBJECT_CLASS(tllt_cp_client_parent_class)->finalize(obj);
}

static void
tllt_cp_client_class_init(TlltCpClientClass *klass)
{
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);

	obj_class->finalize		= tllt_cp_client_finalize;
	obj_class->get_property = tllt_cp_client_get_property;
	obj_class->set_property = tllt_cp_client_set_property;

	obj_properties[PROP_SERVER] = g_param_spec_string(
		"server", _("Server"), _("Location of the server"), NULL, G_PARAM_READWRITE);

	g_object_class_install_properties(obj_class, N_PROPS, obj_properties);
}

static void
tllt_cp_client_init(TlltCpClient *self)
{
	const CURLcode req_code = curl_global_init(CURL_GLOBAL_ALL);
	g_return_if_fail(req_code == CURLE_OK);
	self->handle = curl_easy_init();
	g_warn_if_fail(self->handle != NULL);
}

TlltCpClient *
tllt_cp_client_new_from_environment()
{
	const char *server = g_getenv("TLLT_CP_SERVER");

	g_return_val_if_fail(server != NULL, NULL);

	return g_object_new(TLLT_CP_TYPE_CLIENT, "server", server, NULL);
}

static gboolean
status_code_valid(const unsigned int status_code)
{
	if (status_code != 200 && status_code != 201 && status_code != 202) {
		return FALSE;
	}

	return TRUE;
}

GObject *
tllt_cp_client_get_request(TlltCpClient *self, const GType type, const char *endpoint, GError **err)
{
	g_return_val_if_fail(err == NULL || *err == NULL, NULL);

	TlltCpBuffer buffer		   = {.buf = NULL, .len = 0};
	struct curl_slist *headers = NULL;
	CURLcode req_code		   = 0;

	if ((req_code = curl_easy_setopt(self->handle, CURLOPT_URL, endpoint)) != CURLE_OK) {
		g_set_error(err, PACKAGE_DOMAIN, ERROR_CURL, "Failed to set CURLOPT_URL: %s",
					curl_easy_strerror(req_code));
		goto on_error;
	}
	if ((req_code = curl_easy_setopt(self->handle, CURLOPT_WRITEFUNCTION,
									 tllt_cp_client_write_cb)) != CURLE_OK) {
		g_set_error(err, PACKAGE_DOMAIN, ERROR_CURL, "Failed to set CURLOPT_WRITEFUNCTION: %s",
					curl_easy_strerror(req_code));
		goto on_error;
	}
	if ((req_code = curl_easy_setopt(self->handle, CURLOPT_WRITEDATA, &buffer)) != CURLE_OK) {
		g_set_error(err, PACKAGE_DOMAIN, ERROR_CURL, "Failed to set CURLOPT_WRITEDATA: %s",
					curl_easy_strerror(req_code));
		goto on_error;
	}
	if ((req_code = curl_easy_setopt(self->handle, CURLOPT_USERAGENT, "libcurl-agent/tllt-cp")) !=
		CURLE_OK) {
		g_set_error(err, PACKAGE_DOMAIN, ERROR_CURL, "Failed to set CURLOPT_USERAGENT: %s",
					curl_easy_strerror(req_code));
		goto on_error;
	}

	// Setting headers
	headers = NULL;
	headers = curl_slist_append(headers, "Accept: application/json");
	if (headers == NULL) {
		g_set_error(err, PACKAGE_DOMAIN, ERROR_CURL, "Failed to set request headers");
		goto on_error;
	}
	if ((req_code = curl_easy_setopt(self->handle, CURLOPT_HTTPHEADER, headers)) != CURLE_OK) {
		g_set_error(err, PACKAGE_DOMAIN, ERROR_CURL, "Failed to set CURLOPT_HEADERDATA: %s",
					curl_easy_strerror(req_code));
		goto on_error;
	}

	if ((req_code = curl_easy_perform(self->handle)) != CURLE_OK) {
		g_set_error(err, PACKAGE_DOMAIN, ERROR_CURL, "Failed to perform request: %s",
					curl_easy_strerror(req_code));
		goto on_error;
	}

	long status_code  = 0;
	CURLcode res_code = CURLE_OK;
	if ((res_code = curl_easy_getinfo(self->handle, CURLINFO_RESPONSE_CODE, &status_code)) !=
		CURLE_OK) {
		g_set_error(err, PACKAGE_DOMAIN, ERROR_CURL, "Failed to get status req_code: %s",
					curl_easy_strerror(res_code));
		goto on_error;
	}

	if (req_code == CURLE_ABORTED_BY_CALLBACK) {
		g_set_error(err, PACKAGE_DOMAIN, ERROR_CURL, "Memory error while reading response data");
		goto on_error;
	}

	if (!status_code_valid(status_code)) {
		g_set_error(err, PACKAGE_DOMAIN, ERROR_CURL, "Invalid response code of %ld", status_code);
		goto on_error;
	}

	curl_slist_free_all(headers);

	return json_gobject_from_data(type, buffer.buf, -1, err);

on_error:
	curl_slist_free_all(headers);
	return NULL;
}

GObject *
tllt_cp_client_post_request(TlltCpClient *self, const GType type, const char *endpoint,
							GObject *data, GError **err)
{
	g_return_val_if_fail(err == NULL || *err == NULL, NULL);

	TlltCpBuffer buffer		   = {.buf = NULL, .len = 0};
	struct curl_slist *headers = NULL;
	CURLcode req_code		   = 0;

	if ((req_code = curl_easy_setopt(self->handle, CURLOPT_URL, endpoint)) != CURLE_OK) {
		g_set_error(err, PACKAGE_DOMAIN, ERROR_CURL, "Failed to set CURLOPT_URL: %s",
					curl_easy_strerror(req_code));
		goto on_error;
	}
	if ((req_code = curl_easy_setopt(self->handle, CURLOPT_POST, TRUE)) != CURLE_OK) {
		g_set_error(err, PACKAGE_DOMAIN, ERROR_CURL, "Failed to set CURLOPT_POST: %s",
					curl_easy_strerror(req_code));
		goto on_error;
	}

	if (data != NULL) {
		gsize length	   = 0;
		char *deserialized = json_gobject_to_data(data, &length);
		if (deserialized == NULL || length == 0) {
			g_set_error(err, PACKAGE_DOMAIN, ERROR_JSON, "Failed to deserialize object to JSON");
			goto on_error;
		}

		if ((req_code = curl_easy_setopt(self->handle, CURLOPT_COPYPOSTFIELDS, deserialized)) !=
			CURLE_OK) {
			g_set_error(err, PACKAGE_DOMAIN, ERROR_CURL, "Failed to set CURLOPT_COPYPOSTFIELDS: %s",
						curl_easy_strerror(req_code));
			g_free(deserialized);
			goto on_error;
		}

		g_print("%s\n", deserialized);

		g_free(deserialized);
	}

	// if (type != G_TYPE_NONE) {
	if ((req_code = curl_easy_setopt(self->handle, CURLOPT_WRITEFUNCTION,
									 tllt_cp_client_write_cb)) != CURLE_OK) {
		g_set_error(err, PACKAGE_DOMAIN, ERROR_CURL, "Failed to set CURLOPT_WRITEFUNCTION: %s",
					curl_easy_strerror(req_code));
		goto on_error;
	}
	if ((req_code = curl_easy_setopt(self->handle, CURLOPT_WRITEDATA, &buffer)) != CURLE_OK) {
		g_set_error(err, PACKAGE_DOMAIN, ERROR_CURL, "Failed to set CURLOPT_WRITEDATA: %s",
					curl_easy_strerror(req_code));
		goto on_error;
	}
	// }

	if ((req_code = curl_easy_setopt(self->handle, CURLOPT_USERAGENT, "libcurl/tllt-cp")) !=
		CURLE_OK) {
		g_set_error(err, PACKAGE_DOMAIN, ERROR_CURL, "Failed to set CURLOPT_USERAGENT: %s",
					curl_easy_strerror(req_code));
		goto on_error;
	}

	// Setting headers
	headers = curl_slist_append(headers, "Accept: application/json");
	headers = curl_slist_append(headers, "Content-Type: application/json");
	headers = curl_slist_append(headers, "charsets: utf-8");
	if (headers == NULL) {
		g_set_error(err, PACKAGE_DOMAIN, ERROR_CURL, "Failed to set request headers");
		goto on_error;
	}
	if ((req_code = curl_easy_setopt(self->handle, CURLOPT_HTTPHEADER, headers)) != CURLE_OK) {
		g_set_error(err, PACKAGE_DOMAIN, ERROR_CURL, "Failed to set CURLOPT_HEADEROPT: %s",
					curl_easy_strerror(req_code));
		goto on_error;
	}

	if ((req_code = curl_easy_perform(self->handle)) != CURLE_OK) {
		g_set_error(err, PACKAGE_DOMAIN, ERROR_CURL, "Failed to perform request: %s",
					curl_easy_strerror(req_code));
		goto on_error;
	}

	long status_code  = 0;
	CURLcode res_code = CURLE_OK;
	if ((res_code = curl_easy_getinfo(self->handle, CURLINFO_RESPONSE_CODE, &status_code)) !=
		CURLE_OK) {
		g_set_error(err, PACKAGE_DOMAIN, ERROR_CURL, "Failed to get status req_code: %s",
					curl_easy_strerror(res_code));
		goto on_error;
	}

	if (req_code == CURLE_ABORTED_BY_CALLBACK) {
		g_set_error(err, PACKAGE_DOMAIN, ERROR_CURL, "Memory error while reading response data");
		goto on_error;
	}

	if (!status_code_valid(status_code)) {
		g_set_error(err, PACKAGE_DOMAIN, ERROR_CURL, "Invalid response code of %ld", status_code);
		goto on_error;
	}

	GObject *obj = NULL;
	if (type != G_TYPE_NONE) {
		obj = json_gobject_from_data(type, buffer.buf, -1, err);
	}

	curl_slist_free_all(headers);
	curl_easy_reset(self->handle);
	g_free(buffer.buf);

	return obj;

on_error:
	curl_slist_free_all(headers);
	curl_easy_reset(self->handle);
	g_free(buffer.buf);

	return NULL;
}

size_t
tllt_cp_client_write_cb(char *content, size_t size, size_t nmemb, void *user_data)
{
	const size_t real_size = size * nmemb;
	TlltCpBuffer *buffer   = user_data;

	if (buffer->buf == NULL) {
		buffer->buf = g_malloc(1);
		g_return_val_if_fail(buffer->buf != NULL, 0);
		buffer->buf[0] = 0;
		buffer->len	= 0;
	}

	const size_t new_len = strlen(buffer->buf) + buffer->len;
	buffer->buf			 = g_realloc(buffer->buf, new_len + real_size + 1);
	g_return_val_if_fail(buffer->buf != NULL, 0);

	memcpy(&buffer->buf[buffer->len], content, real_size);

	buffer->len += real_size;
	buffer->buf[buffer->len] = 0;

	return real_size;
}