Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "main.h"
- #define URL "http://51.75.203.112/user/login"
- #define PORT 8965
- #define FOOD_API_URL "http://fr.openfoodfacts.org/api/v0/produit/"
- #define PRODUCT "3029330003533"
- #define LOGIN "paul"
- #define PASSWORD "123456"
- struct string
- {
- char *ptr;
- size_t len;
- };
- struct user
- {
- char login[255];
- char password[255];
- char token[34];
- }typedef userInfo;
- void init_string(struct string *s) {
- s->len = 0;
- s->ptr = malloc(s->len + 1);
- if (s->ptr == NULL) {
- fprintf(stderr, "malloc() failed\n");
- exit(EXIT_FAILURE);
- }
- s->ptr[0] = '\0';
- }
- size_t writefunc(void *ptr, size_t size, size_t nmemb, struct string *s)
- {
- size_t new_len = s->len + size * nmemb;
- s->ptr = realloc(s->ptr, new_len + 1);
- if (s->ptr == NULL) {
- fprintf(stderr, "realloc() failed\n");
- exit(EXIT_FAILURE);
- }
- memcpy(s->ptr + s->len, ptr, size*nmemb);
- s->ptr[new_len] = '\0';
- s->len = new_len;
- return size * nmemb;
- }
- userInfo* GetUserInfo(char* request)
- {
- const cJSON *id = NULL;
- const cJSON *login = NULL;
- const cJSON *password = NULL;
- const cJSON *token = NULL;
- userInfo *user = malloc(sizeof(userInfo));
- cJSON *jsonRequest = cJSON_Parse(request);
- if (jsonRequest == NULL)
- {
- const char *error_ptr = cJSON_GetErrorPtr();
- if (error_ptr != NULL)
- {
- fprintf(stderr, "Error before: %s\n", error_ptr);
- }
- return NULL;
- }
- login = cJSON_GetObjectItemCaseSensitive(jsonRequest, "login");
- if (cJSON_IsString(login) && (login->valuestring != NULL))
- {
- printf("login: \"%s\"\n", login->valuestring);
- sprintf(user->login, login->valuestring);
- }
- password = cJSON_GetObjectItemCaseSensitive(jsonRequest, "password");
- if (cJSON_IsString(password) && (password->valuestring != NULL))
- {
- printf("password: \"%s\"\n", password->valuestring);
- sprintf(user->password, password->valuestring);
- }
- token = cJSON_GetObjectItemCaseSensitive(jsonRequest, "token");
- if (cJSON_IsString(token) && (token->valuestring != NULL))
- {
- printf("token: \"%s\"\n", token->valuestring);
- sprintf(user->token, token->valuestring);
- }
- return user;
- }
- int LoginUser(CURL *curl, CURLcode res)
- {
- if (curl) {
- struct string s;
- init_string(&s);
- curl_easy_setopt(curl, CURLOPT_URL, URL);
- curl_easy_setopt(curl, CURLOPT_PORT, PORT);
- curl_easy_setopt(curl, CURLOPT_POST, 1L);
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
- curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "{'login' :'paul', 'password' :'123456'}");
- /* Perform the request, res will get the return code */
- res = curl_easy_perform(curl);
- /* Check for errors */
- if (res != CURLE_OK)
- {
- fprintf(stderr, "curl_easy_perform() failed: %s\n",
- curl_easy_strerror(res));
- }
- else
- {
- long http_code = 0;
- curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
- fprintf(stderr, "\nSTATUS CODE : %d \n", http_code);
- printf("Response : %s\n", s.ptr);
- GetUserInfo(s.ptr);
- free(s.ptr);
- }
- }
- }
- void GetBrandName(char* request)
- {
- const cJSON *product = NULL;
- const cJSON *energy = NULL;
- cJSON *request_json = cJSON_Parse(request);
- if (request_json != NULL)
- {
- product = cJSON_GetObjectItemCaseSensitive(request_json, "product");
- if (product != NULL)
- {
- energy = cJSON_GetObjectItemCaseSensitive(product, "nutrition_score_debug");
- if (cJSON_IsString(energy) && (energy->valuestring != NULL))
- {
- printf("\nProduct energy \"%s\"\n", energy->valuestring);
- }
- }
- }
- }
- int OpenFoodFactDemo(CURL *curl, CURLcode res)
- {
- if (curl)
- {
- struct string s;
- init_string(&s);
- curl_easy_setopt(curl, CURLOPT_URL, FOOD_API_URL);
- curl_easy_setopt(curl, CURLOPT_PORT, 80);
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
- /* Perform the request, res will get the return code */
- res = curl_easy_perform(curl);
- /* Check for errors */
- if (res != CURLE_OK)
- {
- fprintf(stderr, "curl_easy_perform() failed: %s\n",
- curl_easy_strerror(res));
- }
- else
- {
- long http_code = 0;
- curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
- fprintf(stderr, "\nSTATUS CODE : %d \n", http_code);
- GetBrandName(s.ptr);
- free(s.ptr);
- }
- }
- }
- int main()
- {
- CURL *curl;
- CURLcode res = 0;
- curl = curl_easy_init();
- // Login user via our API
- LoginUser(curl, res);
- // Food check Openfoodfact API
- OpenFoodFactDemo(curl, res);
- /* always cleanup */
- curl_easy_cleanup(curl);
- system("Pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement