Advertisement
Guest User

Untitled

a guest
Apr 9th, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. #include "main.h"
  2.  
  3. #define URL "http://51.75.203.112/user/login"
  4. #define PORT 8965
  5.  
  6.  
  7. #define FOOD_API_URL "http://fr.openfoodfacts.org/api/v0/produit/"
  8. #define PRODUCT "3029330003533"
  9.  
  10. #define LOGIN "paul"
  11. #define PASSWORD "123456"
  12.  
  13. struct string
  14. {
  15. char *ptr;
  16. size_t len;
  17. };
  18.  
  19. struct user
  20. {
  21. char login[255];
  22. char password[255];
  23. char token[34];
  24. }typedef userInfo;
  25.  
  26. void init_string(struct string *s) {
  27. s->len = 0;
  28. s->ptr = malloc(s->len + 1);
  29. if (s->ptr == NULL) {
  30. fprintf(stderr, "malloc() failed\n");
  31. exit(EXIT_FAILURE);
  32. }
  33. s->ptr[0] = '\0';
  34. }
  35.  
  36. size_t writefunc(void *ptr, size_t size, size_t nmemb, struct string *s)
  37. {
  38. size_t new_len = s->len + size * nmemb;
  39. s->ptr = realloc(s->ptr, new_len + 1);
  40. if (s->ptr == NULL) {
  41. fprintf(stderr, "realloc() failed\n");
  42. exit(EXIT_FAILURE);
  43. }
  44. memcpy(s->ptr + s->len, ptr, size*nmemb);
  45. s->ptr[new_len] = '\0';
  46. s->len = new_len;
  47.  
  48. return size * nmemb;
  49. }
  50.  
  51. userInfo* GetUserInfo(char* request)
  52. {
  53. const cJSON *id = NULL;
  54. const cJSON *login = NULL;
  55. const cJSON *password = NULL;
  56. const cJSON *token = NULL;
  57.  
  58. userInfo *user = malloc(sizeof(userInfo));
  59.  
  60. cJSON *jsonRequest = cJSON_Parse(request);
  61. if (jsonRequest == NULL)
  62. {
  63. const char *error_ptr = cJSON_GetErrorPtr();
  64. if (error_ptr != NULL)
  65. {
  66. fprintf(stderr, "Error before: %s\n", error_ptr);
  67. }
  68. return NULL;
  69. }
  70.  
  71. login = cJSON_GetObjectItemCaseSensitive(jsonRequest, "login");
  72. if (cJSON_IsString(login) && (login->valuestring != NULL))
  73. {
  74. printf("login: \"%s\"\n", login->valuestring);
  75. sprintf(user->login, login->valuestring);
  76. }
  77.  
  78. password = cJSON_GetObjectItemCaseSensitive(jsonRequest, "password");
  79. if (cJSON_IsString(password) && (password->valuestring != NULL))
  80. {
  81. printf("password: \"%s\"\n", password->valuestring);
  82. sprintf(user->password, password->valuestring);
  83. }
  84.  
  85. token = cJSON_GetObjectItemCaseSensitive(jsonRequest, "token");
  86. if (cJSON_IsString(token) && (token->valuestring != NULL))
  87. {
  88. printf("token: \"%s\"\n", token->valuestring);
  89. sprintf(user->token, token->valuestring);
  90. }
  91.  
  92. return user;
  93. }
  94.  
  95. int LoginUser(CURL *curl, CURLcode res)
  96. {
  97. if (curl) {
  98. struct string s;
  99. init_string(&s);
  100.  
  101. curl_easy_setopt(curl, CURLOPT_URL, URL);
  102. curl_easy_setopt(curl, CURLOPT_PORT, PORT);
  103. curl_easy_setopt(curl, CURLOPT_POST, 1L);
  104. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
  105. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
  106.  
  107. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "{'login' :'paul', 'password' :'123456'}");
  108.  
  109.  
  110. /* Perform the request, res will get the return code */
  111. res = curl_easy_perform(curl);
  112. /* Check for errors */
  113. if (res != CURLE_OK)
  114. {
  115. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  116. curl_easy_strerror(res));
  117. }
  118. else
  119. {
  120. long http_code = 0;
  121. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
  122. fprintf(stderr, "\nSTATUS CODE : %d \n", http_code);
  123. printf("Response : %s\n", s.ptr);
  124. GetUserInfo(s.ptr);
  125. free(s.ptr);
  126. }
  127.  
  128. }
  129. }
  130.  
  131. void GetBrandName(char* request)
  132. {
  133. const cJSON *product = NULL;
  134. const cJSON *energy = NULL;
  135.  
  136. cJSON *request_json = cJSON_Parse(request);
  137.  
  138. if (request_json != NULL)
  139. {
  140. product = cJSON_GetObjectItemCaseSensitive(request_json, "product");
  141.  
  142. if (product != NULL)
  143. {
  144. energy = cJSON_GetObjectItemCaseSensitive(product, "nutrition_score_debug");
  145.  
  146. if (cJSON_IsString(energy) && (energy->valuestring != NULL))
  147. {
  148. printf("\nProduct energy \"%s\"\n", energy->valuestring);
  149. }
  150. }
  151. }
  152. }
  153.  
  154. int OpenFoodFactDemo(CURL *curl, CURLcode res)
  155. {
  156. if (curl)
  157. {
  158. struct string s;
  159. init_string(&s);
  160.  
  161. curl_easy_setopt(curl, CURLOPT_URL, FOOD_API_URL);
  162. curl_easy_setopt(curl, CURLOPT_PORT, 80);
  163.  
  164.  
  165. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
  166. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
  167.  
  168.  
  169. /* Perform the request, res will get the return code */
  170. res = curl_easy_perform(curl);
  171. /* Check for errors */
  172. if (res != CURLE_OK)
  173. {
  174. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  175. curl_easy_strerror(res));
  176. }
  177. else
  178. {
  179. long http_code = 0;
  180. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
  181. fprintf(stderr, "\nSTATUS CODE : %d \n", http_code);
  182. GetBrandName(s.ptr);
  183. free(s.ptr);
  184. }
  185. }
  186. }
  187.  
  188. int main()
  189. {
  190. CURL *curl;
  191. CURLcode res = 0;
  192.  
  193. curl = curl_easy_init();
  194.  
  195. // Login user via our API
  196. LoginUser(curl, res);
  197.  
  198. // Food check Openfoodfact API
  199. OpenFoodFactDemo(curl, res);
  200.  
  201. /* always cleanup */
  202. curl_easy_cleanup(curl);
  203.  
  204. system("Pause");
  205. return 0;
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement