-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathpbcc_actions_api.c
651 lines (566 loc) · 23.9 KB
/
pbcc_actions_api.c
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
/* -*- c-file-style:"stroustrup"; indent-tabs-mode: nil -*- */
#if PUBNUB_USE_ACTIONS_API
#include "pubnub_internal.h"
#include "pubnub_version.h"
#include "pubnub_assert.h"
#include "pubnub_log.h"
#include "pubnub_helper.h"
#include "pubnub_url_encode.h"
#include "pubnub_json_parse.h"
#include "lib/pb_strnlen_s.h"
#include "pbcc_actions_api.h"
#include <stdio.h>
#include <stdlib.h>
/* Maximum number of actions to return in response */
#define MAX_ACTIONS_LIMIT 100
#define MAX_ACTION_TYPE_LENGTH 15
enum pubnub_res pbcc_form_the_action_object(struct pbcc_context* pb,
char* obj_buffer,
size_t buffer_size,
enum pubnub_action_type actype,
char const** val) {
char const* type_literal;
switch(actype) {
case pbactypReaction:
type_literal = "\"reaction\"";
break;
case pbactypReceipt:
type_literal = "\"receipt\"";
break;
case pbactypCustom:
type_literal = "\"custom\"";
break;
default:
PUBNUB_LOG_ERROR("pbcc_form_the_action_object(pbcc=%p) - "
"unknown action type = %d\n",
pb,
actype);
return PNR_INVALID_PARAMETERS;
}
return pbcc_form_the_action_object_str(pb, obj_buffer, buffer_size, type_literal, val);
}
enum pubnub_res pbcc_form_the_action_object_str(struct pbcc_context* pb,
char* obj_buffer,
size_t buffer_size,
char const* action_type,
char const** val) {
char const* user_id = pbcc_user_id_get(pb);
PUBNUB_ASSERT_OPT(user_id != NULL);
if (NULL == user_id) {
PUBNUB_LOG_ERROR("pbcc_form_the_action_object(pbcc=%p) - user_id not set.\n", pb);
return PNR_INVALID_PARAMETERS;
}
if (('\"' != **val) || ('\"' != *(*val + pb_strnlen_s(*val, PUBNUB_MAX_OBJECT_LENGTH) - 1))) {
PUBNUB_LOG_ERROR("pbcc_form_the_action_object(pbcc=%p) - "
"quotation marks on value ends are missing: "
"value = %s\n",
pb,
*val);
return PNR_INVALID_PARAMETERS;
}
if (('\"' != *action_type) || ('\"' != *(action_type + pb_strnlen_s(action_type, MAX_ACTION_TYPE_LENGTH) - 1))) {
PUBNUB_LOG_ERROR("pbcc_form_the_action_object(pbcc=%p) - "
"quotation marks on action type ends are missing: "
"action_type = %s\n",
pb,
action_type);
return PNR_INVALID_PARAMETERS;
}
if (buffer_size < sizeof("{\"type\":\"\",\"value\":,\"user_id\":\"\"}") +
pb_strnlen_s(action_type, MAX_ACTION_TYPE_LENGTH) +
pb_strnlen_s(*val, PUBNUB_MAX_OBJECT_LENGTH) +
pb->user_id_len) {
PUBNUB_LOG_ERROR("pbcc_form_the_action_object(pbcc=%p) - "
"buffer size is too small: "
"current_buffer_size = %lu\n"
"required_buffer_size = %lu\n",
pb,
(unsigned long)buffer_size,
(unsigned long)(sizeof("{\"type\":\"\",\"value\":,\"user_id\":\"\"}") +
pb_strnlen_s(action_type, MAX_ACTION_TYPE_LENGTH) +
pb_strnlen_s(*val, PUBNUB_MAX_OBJECT_LENGTH) +
pb->user_id_len));
return PNR_TX_BUFF_TOO_SMALL;
}
snprintf(obj_buffer,
buffer_size,
"{\"type\":%s,\"value\":%s,\"user_id\":\"%s\"}",
action_type,
*val,
user_id);
*val = obj_buffer;
return PNR_OK;
}
enum pubnub_res pbcc_add_action_prep(struct pbcc_context* pb,
char const* channel,
char const* message_timetoken,
char const* value)
{
char const* const uname = pubnub_uname();
char const* user_id = pbcc_user_id_get(pb);
enum pubnub_res rslt = PNR_OK;
PUBNUB_ASSERT_OPT(user_id != NULL);
PUBNUB_ASSERT_OPT(channel != NULL);
PUBNUB_ASSERT_OPT(message_timetoken != NULL);
PUBNUB_ASSERT_OPT(value != NULL);
pb->http_content_len = 0;
pb->msg_ofs = pb->msg_end = 0;
pb->http_buf_len = snprintf(pb->http_buf,
sizeof pb->http_buf,
"/v1/message-actions/%s/channel/",
pb->subscribe_key);
APPEND_URL_ENCODED_M(pb, channel);
APPEND_URL_LITERAL_M(pb, "/message/");
APPEND_URL_ENCODED_M(pb, message_timetoken);
URL_PARAMS_INIT(qparam, PUBNUB_MAX_URL_PARAMS);
if (uname) { ADD_URL_PARAM(qparam, pnsdk, uname); }
if (user_id) { ADD_URL_PARAM(qparam, uuid, user_id); }
#if PUBNUB_CRYPTO_API
if (pb->secret_key == NULL) { ADD_URL_AUTH_PARAM(pb, qparam, auth); }
ADD_TS_TO_URL_PARAM();
#else
ADD_URL_AUTH_PARAM(pb, qparam, auth);
#endif
#if PUBNUB_CRYPTO_API
SORT_URL_PARAMETERS(qparam);
#endif
ENCODE_URL_PARAMETERS(pb, qparam);
#if PUBNUB_CRYPTO_API
if (pb->secret_key != NULL) {
rslt = pbcc_sign_url(pb, value, pubnubSendViaPOST, true);
if (rslt != PNR_OK) {
PUBNUB_LOG_ERROR("pbcc_sign_url failed. pb=%p, message=%s, method=%d", pb, value, pubnubSendViaPOST);
}
}
#endif
APPEND_MESSAGE_BODY_M(PNR_OK, pb, value);
PUBNUB_LOG_DEBUG("pbcc_add_action_prep. REQUEST =%s\n", pb->http_buf);
return (rslt != PNR_OK) ? rslt : PNR_STARTED;
}
pubnub_chamebl_t pbcc_get_message_timetoken(struct pbcc_context* pb)
{
pubnub_chamebl_t result;
char const* reply = pb->http_reply;
int replylen = pb->http_buf_len;
struct pbjson_elem elem;
struct pbjson_elem parsed;
enum pbjson_object_name_parse_result json_rslt;
if (pb->last_result != PNR_OK) {
PUBNUB_LOG_ERROR("pbcc_get_message_timetoken(pb=%p) can be called only if "
"previous transactin PBTT_ADD_ACTION(%d) is finished successfully. "
"Transaction result was: %d('%s')\n",
pb,
PBTT_ADD_ACTION,
pb->last_result,
pubnub_res_2_string(pb->last_result));
result.ptr = NULL;
result.size = 0;
return result;
}
elem.start = reply;
elem.end = pbjson_find_end_element(reply, reply + replylen) + 1;
pbjson_get_object_value(&elem, "data", &parsed);
json_rslt = pbjson_get_object_value(&parsed, "messageTimetoken", &elem);
if (json_rslt != jonmpOK) {
PUBNUB_LOG_ERROR("pbcc_get_message_timetoken(pbcc=%p) - Invalid response: "
"pbjson_get_object_value(\"messageTimetoken\") reported an error: %s\n",
pb,
pbjson_object_name_parse_result_2_string(json_rslt));
result.ptr = NULL;
result.size = 0;
return result;
}
result.ptr = (char*)elem.start;
result.size = elem.end - elem.start;
return result;
}
pubnub_chamebl_t pbcc_get_action_timetoken(struct pbcc_context* pb)
{
pubnub_chamebl_t result;
char const* reply = pb->http_reply;
int replylen = pb->http_buf_len;
struct pbjson_elem elem;
struct pbjson_elem parsed;
enum pbjson_object_name_parse_result json_rslt;
if (pb->last_result != PNR_OK) {
PUBNUB_LOG_ERROR("pbcc_get_action_timetoken(pb=%p) can be called only if "
"previous transaction PBTT_ADD_ACTION(%d) is finished successfully. "
"Transaction result was: %d('%s')\n",
pb,
PBTT_ADD_ACTION,
pb->last_result,
pubnub_res_2_string(pb->last_result));
result.ptr = NULL;
result.size = 0;
return result;
}
elem.start = reply;
elem.end = pbjson_find_end_element(reply, reply + replylen) + 1;
pbjson_get_object_value(&elem, "data", &parsed);
json_rslt = pbjson_get_object_value(&parsed, "actionTimetoken", &elem);
if (json_rslt != jonmpOK) {
PUBNUB_LOG_ERROR("pbcc_get_action_timetoken(pbcc=%p) - Invalid response: "
"pbjson_get_object_value(\"actionTimetoken\") reported an error: %s\n",
pb,
pbjson_object_name_parse_result_2_string(json_rslt));
result.ptr = NULL;
result.size = 0;
return result;
}
result.ptr = (char*)elem.start;
result.size = elem.end - elem.start;
return result;
}
enum pubnub_res pbcc_remove_action_prep(struct pbcc_context* pb,
char const* channel,
pubnub_chamebl_t message_timetoken,
pubnub_chamebl_t action_timetoken)
{
char const* const uname = pubnub_uname();
char const* user_id = pbcc_user_id_get(pb);
enum pubnub_res rslt = PNR_OK;
PUBNUB_ASSERT_OPT(user_id != NULL);
PUBNUB_ASSERT_OPT(channel != NULL);
PUBNUB_ASSERT_OPT(message_timetoken.ptr != NULL);
PUBNUB_ASSERT_OPT(action_timetoken.ptr != NULL);
if ((*message_timetoken.ptr != '\"') ||
(*(message_timetoken.ptr + message_timetoken.size - 1) != '\"')) {
PUBNUB_LOG_ERROR("pbcc_remove_action_prep(pbcc=%p) - "
"message timetoken is missing quotation marks: "
"message_timetoken = %.*s\n",
pb,
(int)message_timetoken.size,
message_timetoken.ptr);
return PNR_INVALID_PARAMETERS;
}
if ((*action_timetoken.ptr != '\"') ||
(*(action_timetoken.ptr + action_timetoken.size - 1) != '\"')) {
PUBNUB_LOG_ERROR("pbcc_remove_action_prep(pbcc=%p) - "
"action timetoken is missing quotation marks: "
"action_timetoken = %.*s\n",
pb,
(int)action_timetoken.size,
action_timetoken.ptr);
return PNR_INVALID_PARAMETERS;
}
pb->http_content_len = 0;
pb->msg_ofs = pb->msg_end = 0;
pb->http_buf_len = snprintf(pb->http_buf,
sizeof pb->http_buf,
"/v1/message-actions/%s/channel/",
pb->subscribe_key);
APPEND_URL_ENCODED_M(pb, channel);
APPEND_URL_LITERAL_M(pb, "/message/");
APPEND_URL_STRING_M(pb, message_timetoken.ptr + 1, message_timetoken.size - 2);
APPEND_URL_LITERAL_M(pb, "/action/");
APPEND_URL_STRING_M(pb, action_timetoken.ptr + 1, action_timetoken.size - 2);
URL_PARAMS_INIT(qparam, PUBNUB_MAX_URL_PARAMS);
if (uname) { ADD_URL_PARAM(qparam, pnsdk, uname); }
if (user_id) { ADD_URL_PARAM(qparam, uuid, user_id); }
#if PUBNUB_CRYPTO_API
if (pb->secret_key == NULL){
ADD_URL_AUTH_PARAM(pb, qparam, auth);
}
ADD_TS_TO_URL_PARAM();
#else
ADD_URL_AUTH_PARAM(pb, qparam, auth);
#endif
#if PUBNUB_CRYPTO_API
SORT_URL_PARAMETERS(qparam);
#endif
ENCODE_URL_PARAMETERS(pb, qparam);
#if PUBNUB_CRYPTO_API
if (pb->secret_key != NULL) {
rslt = pbcc_sign_url(pb, "", pubnubUseDELETE, true);
if (rslt != PNR_OK) {
PUBNUB_LOG_ERROR("pbcc_sign_url failed. pb=%p, message=%s, method=%d", pb, "", pubnubUseDELETE);
}
}
#endif
PUBNUB_LOG_DEBUG("pbcc_remove_action_prep. REQUEST =%s\n", pb->http_buf);
return (rslt != PNR_OK) ? rslt : PNR_STARTED;
}
enum pubnub_res pbcc_get_actions_prep(struct pbcc_context* pb,
char const* channel,
char const* start,
char const* end,
size_t limit)
{
char const* const uname = pubnub_uname();
char const* user_id = pbcc_user_id_get(pb);
enum pubnub_res rslt = PNR_OK;
PUBNUB_ASSERT_OPT(user_id != NULL);
PUBNUB_ASSERT_OPT(channel != NULL);
PUBNUB_ASSERT_OPT(limit <= MAX_ACTIONS_LIMIT);
pb->http_content_len = 0;
pb->msg_ofs = pb->msg_end = 0;
pb->http_buf_len = snprintf(pb->http_buf,
sizeof pb->http_buf,
"/v1/message-actions/%s/channel/",
pb->subscribe_key);
APPEND_URL_ENCODED_M(pb, channel);
URL_PARAMS_INIT(qparam, PUBNUB_MAX_URL_PARAMS);
if (uname) { ADD_URL_PARAM(qparam, pnsdk, uname); }
if (user_id) { ADD_URL_PARAM(qparam, uuid, user_id); }
#if PUBNUB_CRYPTO_API
if (pb->secret_key == NULL) { ADD_URL_AUTH_PARAM(pb, qparam, auth); }
ADD_TS_TO_URL_PARAM();
#else
ADD_URL_AUTH_PARAM(pb, qparam, auth);
#endif
if (start) { ADD_URL_PARAM(qparam, start, start); }
if (end) { ADD_URL_PARAM(qparam, end, end); }
if (limit != 0) { ADD_URL_PARAM_SIZET(qparam, limit, limit); }
#if PUBNUB_CRYPTO_API
SORT_URL_PARAMETERS(qparam);
#endif
ENCODE_URL_PARAMETERS(pb, qparam);
#if PUBNUB_CRYPTO_API
if (pb->secret_key != NULL) {
rslt = pbcc_sign_url(pb, "", pubnubSendViaGET, true);
if (rslt != PNR_OK) {
PUBNUB_LOG_ERROR("pbcc_sign_url failed. pb=%p, message=%s, method=%d", pb, "", pubnubSendViaGET);
}
}
#endif
PUBNUB_LOG_DEBUG("pbcc_get_actions_prep. REQUEST =%s\n", pb->http_buf);
return (rslt != PNR_OK) ? rslt : PNR_STARTED;
}
enum pubnub_res pbcc_get_actions_more_prep(struct pbcc_context* pb)
{
enum pubnub_res rslt = PNR_OK;
char const* const uname = pubnub_uname();
char const* user_id = pbcc_user_id_get(pb);
char const* reply = pb->http_reply;
int replylen = pb->http_buf_len;
struct pbjson_elem elem;
struct pbjson_elem parsed;
enum pbjson_object_name_parse_result json_rslt;
PUBNUB_ASSERT_OPT(user_id != NULL);
if (pb->last_result != PNR_OK) {
PUBNUB_LOG_ERROR("pbcc_get_actions_more_prep(pb=%p) can be called only if "
"previous transaction is finished successfully. "
"Transaction result was: %d('%s')\n",
pb,
pb->last_result,
pubnub_res_2_string(pb->last_result));
return PNR_INTERNAL_ERROR;
}
elem.start = reply;
elem.end = pbjson_find_end_element(reply, reply + replylen) + 1;
json_rslt = pbjson_get_object_value(&elem, "more", &parsed);
if (jonmpKeyNotFound == json_rslt) {
return PNR_GOT_ALL_ACTIONS;
}
else if (json_rslt != jonmpOK) {
PUBNUB_LOG_ERROR("pbcc_get_actions_more_prep(pbcc=%p) - Invalid response: "
"pbjson_get_object_value(\"more\") reported an error: %s\n",
pb,
pbjson_object_name_parse_result_2_string(json_rslt));
return PNR_FORMAT_ERROR;
}
json_rslt = pbjson_get_object_value(&parsed, "url", &elem);
if (json_rslt != jonmpOK) {
PUBNUB_LOG_ERROR("pbcc_get_actions_more_prep(pbcc=%p) - Invalid response: "
"pbjson_get_object_value(\"url\") reported an error: %s\n",
pb,
pbjson_object_name_parse_result_2_string(json_rslt));
return PNR_FORMAT_ERROR;
}
pb->http_content_len = 0;
pb->msg_ofs = pb->msg_end = 0;
pb->http_buf_len = snprintf(pb->http_buf,
sizeof pb->http_buf,
"%.*s",
(int)(elem.end - elem.start - 2),
elem.start + 1);
URL_PARAMS_INIT(qparam, PUBNUB_MAX_URL_PARAMS);
if (uname) { ADD_URL_PARAM(qparam, pnsdk, uname); }
if (user_id) { ADD_URL_PARAM(qparam, uuid, user_id); }
#if PUBNUB_CRYPTO_API
if (pb->secret_key == NULL) { ADD_URL_AUTH_PARAM(pb, qparam, auth); }
ADD_TS_TO_URL_PARAM();
#else
ADD_URL_AUTH_PARAM(pb, qparam, auth);
#endif
#if PUBNUB_CRYPTO_API
SORT_URL_PARAMETERS(qparam);
#endif
ENCODE_URL_PARAMETERS(pb, qparam);
#if PUBNUB_CRYPTO_API
if (pb->secret_key != NULL) {
rslt = pbcc_sign_url(pb, "", pubnubSendViaGET, true);
if (rslt != PNR_OK) {
PUBNUB_LOG_ERROR("pbcc_sign_url failed. pb=%p, message=%s, method=%d", pb, "", pubnubSendViaGET);
}
}
#endif
PUBNUB_LOG_DEBUG("pbcc_get_actions_more_prep. REQUEST =%s\n", pb->http_buf);
return (rslt != PNR_OK) ? rslt : PNR_STARTED;
}
enum pubnub_res pbcc_history_with_actions_prep(struct pbcc_context* pb,
char const* channel,
char const* start,
char const* end,
size_t limit)
{
char const* const uname = pubnub_uname();
char const* user_id = pbcc_user_id_get(pb);
enum pubnub_res rslt = PNR_OK;
PUBNUB_ASSERT_OPT(user_id != NULL);
PUBNUB_ASSERT_OPT(channel != NULL);
PUBNUB_ASSERT_OPT(limit <= MAX_ACTIONS_LIMIT);
pb->http_content_len = 0;
pb->msg_ofs = pb->msg_end = 0;
pb->http_buf_len = snprintf(pb->http_buf,
sizeof pb->http_buf,
"/v3/history-with-actions/sub-key/%s/channel/",
pb->subscribe_key);
APPEND_URL_ENCODED_M(pb, channel);
URL_PARAMS_INIT(qparam, PUBNUB_MAX_URL_PARAMS);
if (uname) { ADD_URL_PARAM(qparam, pnsdk, uname); }
if (user_id) { ADD_URL_PARAM(qparam, uuid, user_id); }
#if PUBNUB_CRYPTO_API
if (pb->secret_key == NULL) { ADD_URL_AUTH_PARAM(pb, qparam, auth); }
ADD_TS_TO_URL_PARAM();
#else
ADD_URL_AUTH_PARAM(pb, qparam, auth);
#endif
if (start) { ADD_URL_PARAM(qparam, start, start); }
if (end) { ADD_URL_PARAM(qparam, end, end); }
if (limit != 0) { ADD_URL_PARAM_SIZET(qparam, limit, limit); }
#if PUBNUB_CRYPTO_API
SORT_URL_PARAMETERS(qparam);
#endif
ENCODE_URL_PARAMETERS(pb, qparam);
#if PUBNUB_CRYPTO_API
if (pb->secret_key != NULL) {
rslt = pbcc_sign_url(pb, "", pubnubSendViaGET, true);
if (rslt != PNR_OK) {
PUBNUB_LOG_ERROR("pbcc_sign_url failed. pb=%p, message=%s, method=%d", pb, "", pubnubSendViaGET);
}
}
#endif
PUBNUB_LOG_DEBUG("pbcc_history_with_actions_prep. REQUEST =%s\n", pb->http_buf);
return (rslt != PNR_OK) ? rslt : PNR_STARTED;
}
enum pubnub_res pbcc_parse_actions_api_response(struct pbcc_context* pb)
{
char const* reply = pb->http_reply;
int replylen = pb->http_buf_len;
struct pbjson_elem elem;
struct pbjson_elem parsed;
enum pbjson_object_name_parse_result json_rslt;
if ((replylen < 2) || (reply[0] != '{')) {
return PNR_FORMAT_ERROR;
}
pb->chan_ofs = pb->chan_end = 0;
pb->msg_ofs = 0;
pb->msg_end = replylen + 1;
elem.end = pbjson_find_end_element(reply, reply + replylen);
/* elem.end has to be just behind end curly brace */
if ((*reply != '{') || (*(elem.end++) != '}')) {
PUBNUB_LOG_ERROR("pbcc_parse_actions_api_response(pbcc=%p) - Invalid: "
"response from server is not JSON - response='%s'\n",
pb,
reply);
return PNR_FORMAT_ERROR;
}
elem.start = reply;
if (pbjson_value_for_field_found(&elem, "status", "403")) {
PUBNUB_LOG_ERROR("pbcc_parse_actions_api_response(pbcc=%p) - AccessDenied: "
"response from server - response='%s'\n",
pb,
reply);
return PNR_ACCESS_DENIED;
}
json_rslt = pbjson_get_object_value(&elem, "data", &parsed);
if (jonmpKeyNotFound == json_rslt) {
json_rslt = pbjson_get_object_value(&elem, "error", &parsed);
if (json_rslt != jonmpOK) {
PUBNUB_LOG_ERROR("pbcc_parse_actions_api_response(pbcc=%p) - Invalid response: "
"pbjson_get_object_value(\"error\") reported an error: %s\n",
pb,
pbjson_object_name_parse_result_2_string(json_rslt));
return PNR_FORMAT_ERROR;
}
PUBNUB_LOG_WARNING("pbcc_parse_actions_api_response(pbcc=%p): \"error\"='%.*s'\n",
pb,
(int)(parsed.end - parsed.start),
parsed.start);
return PNR_ACTIONS_API_ERROR;
}
else if (json_rslt != jonmpOK) {
PUBNUB_LOG_ERROR("pbcc_parse_actions_api_response(pbcc=%p) - Invalid response: "
"pbjson_get_object_value(\"data\") reported an error: %s\n",
pb,
pbjson_object_name_parse_result_2_string(json_rslt));
return PNR_FORMAT_ERROR;
}
PUBNUB_LOG_TRACE("pbcc_parse_actions_api_response(pbcc=%p): \"data\"='%.*s'\n",
pb,
(int)(parsed.end - parsed.start),
parsed.start);
return PNR_OK;
}
enum pubnub_res pbcc_parse_history_with_actions_response(struct pbcc_context* pb)
{
char const* reply = pb->http_reply;
int replylen = pb->http_buf_len;
struct pbjson_elem elem;
struct pbjson_elem parsed;
enum pbjson_object_name_parse_result json_rslt;
if ((replylen < 2) || (reply[0] != '{')) {
return PNR_FORMAT_ERROR;
}
pb->chan_ofs = pb->chan_end = 0;
pb->msg_ofs = 0;
pb->msg_end = replylen + 1;
elem.end = pbjson_find_end_element(reply, reply + replylen);
/* elem.end has to be just behind end curly brace */
if ((*reply != '{') || (*(elem.end++) != '}')) {
PUBNUB_LOG_ERROR("pbcc_parse_history_with_actions_response(pbcc=%p) - Invalid: "
"response from server is not JSON - response='%s'\n",
pb,
reply);
return PNR_FORMAT_ERROR;
}
elem.start = reply;
if (pbjson_value_for_field_found(&elem, "status", "403")) {
PUBNUB_LOG_ERROR("pbcc_parse_history_with_actions_response(pbcc=%p) - AccessDenied: "
"response from server - response='%s'\n",
pb,
reply);
return PNR_ACCESS_DENIED;
}
json_rslt = pbjson_get_object_value(&elem, "channels", &parsed);
if (jonmpKeyNotFound == json_rslt) {
json_rslt = pbjson_get_object_value(&elem, "error_message", &parsed);
if (json_rslt != jonmpOK) {
PUBNUB_LOG_ERROR("pbcc_parse_history_with_actions_response(pbcc=%p) - Invalid response: "
"pbjson_get_object_value(\"error_message\") reported an error: %s\n",
pb,
pbjson_object_name_parse_result_2_string(json_rslt));
return PNR_FORMAT_ERROR;
}
PUBNUB_LOG_WARNING("pbcc_parse_history_with_actions_response(pbcc=%p): "
"\"error_message\"='%.*s'\n",
pb,
(int)(parsed.end - parsed.start),
parsed.start);
return PNR_ACTIONS_API_ERROR;
}
else if (json_rslt != jonmpOK) {
PUBNUB_LOG_ERROR("pbcc_parse_history_with_actions_response(pbcc=%p) - Invalid response: "
"pbjson_get_object_value(\"channels\") reported an error: %s\n",
pb,
pbjson_object_name_parse_result_2_string(json_rslt));
return PNR_FORMAT_ERROR;
}
PUBNUB_LOG_TRACE("pbcc_parse_history_with_actions_response(pbcc=%p): \"channels\"='%.*s'\n",
pb,
(int)(parsed.end - parsed.start),
parsed.start);
return PNR_OK;
}
#endif /* PUBNUB_USE_ACTIONS_API */