-
-
Save nliviu/b5add551f9787a0186e994b301dc4104 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2021 nliviu | |
* All rights reserved | |
* | |
* Licensed under the Apache License, Version 2.0 (the ""License""); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an ""AS IS"" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
#include "mgos.h" | |
/* | |
* https://reqres.in/ | |
*/ | |
struct request { | |
const char *api; | |
const char *payload; | |
}; | |
static const char *s_host = "reqres.in"; | |
static const char *s_uri = "/api/"; | |
static struct mg_connection *s_http_connection = NULL; | |
static struct request requests[] = { | |
/* success */ | |
{"register", "{\"email\": \"eve.holt@reqres.in\", \"password\": \"123\"}"}, | |
/* fail */ | |
{"register", "{\"email\": \"eve.holt@fife.com\"}"}, | |
/* fail */ | |
{"login", "{\"email\": \"eve.holt@reqres.in\"}"}, | |
/* success */ | |
{"login", "{\"email\": \"eve.holt@reqres.in\", \"password\": \"123\"}"}, | |
}; | |
static const size_t requests_count = sizeof(requests) / sizeof(requests[0]); | |
static void ev_handler(struct mg_connection *c, int ev, void *ev_data, | |
void *user_data) { | |
switch (ev) { | |
case MG_EV_CONNECT: { | |
int status = *(int *) ev_data; | |
if (status != 0) { | |
mg_http_send_error(c, 502, "Error!"); | |
LOG(LL_INFO, ("%.3lf - connect error %d (%s)", mgos_uptime(), status, | |
strerror(status))); | |
} else { | |
LOG(LL_INFO, | |
("%.3lf - connected OK (mg_connection %p)", mgos_uptime(), c)); | |
} | |
break; | |
} | |
case MG_EV_HTTP_REPLY: { | |
struct http_message *hm = (struct http_message *) ev_data; | |
LOG(LL_INFO, ("%.3lf - Reply: %.*s", mgos_uptime(), (int) hm->body.len, | |
hm->body.p)); | |
break; | |
} | |
case MG_EV_CLOSE: { | |
LOG(LL_INFO, ("%.3lf - mg_connection %p closed", mgos_uptime(), c)); | |
s_http_connection = NULL; | |
break; | |
} | |
} | |
(void) user_data; | |
} | |
static void send_request(size_t index) { | |
const char *extra_headers = | |
"Content-Type: application/json\r\n" | |
"Accept: application/json\r\n"; | |
const char *payload = requests[index].payload; | |
if (s_http_connection == NULL) { | |
char url_buf[64]; | |
char *url = url_buf; | |
mg_asprintf(&url, sizeof(url_buf), "https://%s%s%s", s_host, s_uri, | |
requests[index].api); | |
LOG(LL_INFO, ("%.3lf - Request (new connection): url=%s, payload=%s", | |
mgos_uptime(), url, requests[index].payload)); | |
s_http_connection = mg_connect_http(mgos_get_mgr(), ev_handler, NULL, url, | |
extra_headers, payload); | |
if (url != url_buf) { | |
free(url); | |
} | |
} else { | |
int payload_len = strlen(payload); | |
const char *fmt = | |
"POST %s HTTP/1.1\r\nHost: %s\r\nContent-Length: %d\r\n%s\r\n%s"; | |
char uri[32]; | |
snprintf(uri, sizeof(uri), "%s%s", s_uri, requests[index].api); | |
LOG(LL_INFO, | |
("%.3lf - Request (reusing connection): host: %s, uri: %s, payload: %s", | |
mgos_uptime(), s_host, uri, payload)); | |
mg_printf(s_http_connection, fmt, uri, s_host, payload_len, extra_headers, | |
payload); | |
} | |
} | |
static void timer_cb(void *arg) { | |
static size_t index = 0; | |
send_request(index); | |
++index; | |
if (index >= requests_count) { | |
index = 0; | |
} | |
(void) arg; | |
} | |
static void net_event_handler(int ev, void *evd, void *arg) { | |
static bool init = false; | |
if (ev == MGOS_NET_EV_IP_ACQUIRED) { | |
if (!init) { | |
init = true; | |
mgos_set_timer(15000, MGOS_TIMER_REPEAT | MGOS_TIMER_RUN_NOW, timer_cb, | |
NULL); | |
} | |
} | |
(void) evd; | |
(void) arg; | |
} | |
enum mgos_app_init_result mgos_app_init(void) { | |
mgos_event_add_group_handler(MGOS_EVENT_GRP_NET, net_event_handler, NULL); | |
return MGOS_APP_INIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment