gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[taler-taler-mdb] 07/93: first includes of libjansson


From: gnunet
Subject: [taler-taler-mdb] 07/93: first includes of libjansson
Date: Mon, 18 Nov 2019 21:12:30 +0100

This is an automated email from the git hooks/post-receive script.

marco-boss pushed a commit to branch master
in repository taler-mdb.

commit ae2056f23daf908868c8f501351a2d454537844d
Author: BOSS_Marco <address@hidden>
AuthorDate: Wed Oct 30 19:33:28 2019 +0100

    first includes of libjansson
---
 src/communication.c | 350 +++++++++++++++++++++++++++-------------------------
 src/configuration.h |  24 ++--
 2 files changed, 191 insertions(+), 183 deletions(-)

diff --git a/src/communication.c b/src/communication.c
index db74106..63a21b5 100644
--- a/src/communication.c
+++ b/src/communication.c
@@ -25,210 +25,228 @@ along with
 
 #include <string.h>
 #include <stdbool.h>
+#include <jansson.h>
 
 #include "communication.h"
 #include "wallet.h"
 #include "configuration.h"
 
-int taler_init (CURL **curl)
+int taler_init(CURL **curl)
 {
-  if ( *curl != NULL )
-  {
-    printf ("taler_init: curl handle already initialized\n");
-    return EXIT_FAILURE;
-  }
-
-  // initialize curl
-  CURLcode result = curl_global_init (CURL_GLOBAL_ALL);
-  *curl = curl_easy_init ();
-  if ( ! (*curl) ||(result != CURLE_OK) )
-  {
-    printf ("taler_init: could not inizialize curl handle\n");
-    return EXIT_FAILURE;
-  }
-
-  return EXIT_SUCCESS;
+    if( *curl != NULL ){
+        printf( "taler_init: curl handle already initialized\n" );
+        return EXIT_FAILURE;
+    }
+
+    // initialize curl
+    CURLcode result = curl_global_init( CURL_GLOBAL_ALL );
+    *curl = curl_easy_init();
+    if( !(*curl) || result != CURLE_OK ){
+        printf( "taler_init: could not inizialize curl handle\n" );
+        return EXIT_FAILURE;
+    }
+
+    return EXIT_SUCCESS;
 }
 
-void taler_exit (CURL **curl)
+void taler_exit( CURL **curl )
 {
-  curl_easy_cleanup (*curl);
-  curl_global_cleanup ();
+    curl_easy_cleanup(*curl);
+    curl_global_cleanup();
 }
 
-int taler_alloc_string (char **string, size_t size)
+int taler_alloc_string( char **string, size_t size )
 {
-  *string = malloc (size);
-  if ( ! (*string) )
-  {
-    printf ("taler_alloc: unable to allocate string");
-    exit (EXIT_FAILURE);
-  }
-  return EXIT_SUCCESS;
+    *string = malloc( size );
+    if( !(*string) ){
+        printf( "taler_alloc: unable to allocate string" );
+        exit( EXIT_FAILURE );
+    }
+    return EXIT_SUCCESS;
 }
 
-static size_t _taler_write_response (void *contents, size_t size, size_t nmemb,
-                                     void *userp)
+static size_t _taler_write_response( void *contents, size_t size, size_t 
nmemb, void *userp )
 {
-  size_t realSize = size * nmemb;
-  struct TalerResponse *response = (struct TalerResponse *) userp;
-
-  char *tempString = realloc (response->string, response->size + realSize + 1);
-  if ( ! tempString )
-  {
-    printf ("Allocation failure");
-    return EXIT_FAILURE;
-  }
-
-  response->string = tempString;
-  memcpy (&(response->string[response->size]), contents, realSize);
-  response->size += realSize;
-  response->string[response->size] = 0;
-
-  return realSize;
+    size_t realSize = size * nmemb;
+    struct TalerResponse *response = (struct TalerResponse *)userp;
+
+    char *tempString = realloc( response->string, response->size + realSize + 
1 );
+    if( !tempString ) {
+        printf( "Allocation failure" );
+        return EXIT_FAILURE;
+    }
+
+    response->string = tempString;
+    memcpy(&( response->string[response->size] ), contents, realSize );
+    response->size += realSize;
+    response->string[response->size] = 0;
+
+    return realSize;
 }
 
-int taler_create_order_req (ProductOrder *product)
+int taler_create_order_req( ProductOrder *product )
 {
-  char buffer[256];
+    // create the string representing the amount, e.g. "KUDOS:2.5" ( +2 
because of ':' and '\0' )
+    char amountStr[ strlen(product->currency) + strlen(product->amount) + 2 ];
+    sprintf( amountStr, "%s:%s", product->currency, product->amount );
+
+    // create the fullfillment ur, e.g. 
"taler://fulfillment-success/Enjoy+your+ice+cream!"; ( +2 because of '!' and 
'\0' )
+    char fullfillmentUrl[ strlen(FULLFILLMENT_URL) + strlen(FULLFILLMENT_MSG) 
+ strlen(product->product) + 2 ];
+    sprintf( fullfillmentUrl, "%s%s%s!", FULLFILLMENT_URL, FULLFILLMENT_MSG, 
product->product );
+
+    // create the json object for the order request
+    json_t *orderReq = json_pack( "{ s: { s:s, s:s, s:s }}", "order", 
"summary", product->product, "amount", amountStr, "fullfilment_url", 
fullfillmentUrl );
+    if( !orderReq ){
+        printf( "taler_create_order_req: error creating json object\n" );
+        return EXIT_FAILURE;
+    }
+
+    // create the string to send to taler in compact format
+    char *buffer = json_dumps( orderReq, JSON_COMPACT );
+    if( !buffer ){
+        printf("taler_create_order_req: error converting json to string\n");
+        return EXIT_FAILURE;
+    }
 
-  sprintf (buffer, JSON_ORDER, product->product, product->currency,
-           product->amount, product->product);
+    // allocate memory for the string and copy it into the product
+    char* temp = realloc( product->orderRequest, strlen(buffer) + 1 );
+    if( !temp ){
+        printf( "could not allocate order\n" );
+        return EXIT_FAILURE;
+    }
+    product->orderRequest = temp;
+    strcpy( product->orderRequest, buffer );
 
-  char*temp = realloc (product->orderRequest, strlen (buffer) + 1);
-  if ( ! temp )
-  {
-    printf ("could not allocate order\n");
-    return EXIT_FAILURE;
-  }
+    // print the created order request and free the json object
+    printf( "Created order Request: \n" );
+    json_dumpf( orderReq, stdout, JSON_INDENT(2) );
+    json_delete( orderReq );
 
-  product->orderRequest = temp;
-  strcpy (product->orderRequest, buffer);
-  printf ("Created order Request: \n%s\n\n", product->orderRequest);
+    // free the allocated buffer from json_dumps
+    free( buffer );
 
-  return EXIT_SUCCESS;
+    return EXIT_SUCCESS;
 }
 
-int taler_create_order (CURL *curl, ProductOrder *product)
+int taler_create_order( CURL *curl, ProductOrder *product )
 {
-  // reset the response size
-  product->response->size = 0;
-
-  // set the url
-  char url[ strlen (BACKEND_BASE_URL) + strlen (ORDER) + 1 ];
-  sprintf (url, "%s%s", BACKEND_BASE_URL, ORDER);
-  curl_easy_setopt (curl, CURLOPT_URL, url);
-
-  // set the authentication headers
-  struct curl_slist *list = NULL;
-  list = curl_slist_append (list, AUTH_HEADER);
-  curl_easy_setopt (curl, CURLOPT_HTTPHEADER, list);
-
-  // curl option "post"
-  curl_easy_setopt (curl, CURLOPT_HTTPPOST, true);
-  curl_easy_setopt (curl, CURLOPT_POSTFIELDS, product->orderRequest);
-
-  // pass the write function and the struct to write to
-  curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, _taler_write_response);
-  curl_easy_setopt (curl, CURLOPT_WRITEDATA, (void *) product->response);
-
-  // perform the call
-  CURLcode result = curl_easy_perform (curl);
-  if ( result != CURLE_OK )
-  {
-    printf ("could not communicate with \"%s\"\n", url);
-    return EXIT_FAILURE;
-  }
-
-  // reset the curl options
-  curl_easy_reset (curl);
-
-  // clean up
-  curl_slist_free_all (list);
-
-  return EXIT_SUCCESS;
+    // reset the response size
+    product->response->size = 0;
+
+    // set the url
+    char url[ strlen(BACKEND_BASE_URL) + strlen( ORDER ) + 1 ];
+    sprintf( url, "%s%s", BACKEND_BASE_URL, ORDER );
+    curl_easy_setopt( curl, CURLOPT_URL, url );
+
+    // set the authentication headers
+    struct curl_slist *list = NULL;
+    list = curl_slist_append( list, AUTH_HEADER );
+    curl_easy_setopt( curl, CURLOPT_HTTPHEADER, list );
+
+    // curl option "post"
+    curl_easy_setopt( curl, CURLOPT_HTTPPOST, true );
+    curl_easy_setopt( curl, CURLOPT_POSTFIELDS, product->orderRequest );
+
+    // pass the write function and the struct to write to
+    curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, _taler_write_response );
+    curl_easy_setopt( curl, CURLOPT_WRITEDATA, (void *)product->response );
+
+    // perform the call
+    CURLcode result = curl_easy_perform( curl );
+    if( result != CURLE_OK ){
+        printf( "could not communicate with \"%s\"\n", url  );
+        return EXIT_FAILURE;
+    }
+
+    // reset the curl options
+    curl_easy_reset( curl );
+
+    // clean up
+    curl_slist_free_all( list );
+
+    return EXIT_SUCCESS;
 }
 
-int taler_check_payment_status (CURL *curl, ProductOrder *product)
+int taler_check_payment_status( CURL *curl, ProductOrder *product )
 {
-  // reset the response size
-  product->response->size = 0;
+    // reset the response size
+    product->response->size = 0;
 
-  // set the url
-  curl_easy_setopt (curl, CURLOPT_URL, product->checkUrl);
+    // set the url
+    curl_easy_setopt( curl, CURLOPT_URL, product->checkUrl );
 
-  // set the authentication headers
-  struct curl_slist *list = NULL;
-  list = curl_slist_append (list, AUTH_HEADER);
-  curl_easy_setopt (curl, CURLOPT_HTTPHEADER, list);
+    // set the authentication headers
+    struct curl_slist *list = NULL;
+    list = curl_slist_append( list, AUTH_HEADER );
+    curl_easy_setopt( curl, CURLOPT_HTTPHEADER, list );
 
-  // curl option "get"
-  curl_easy_setopt (curl, CURLOPT_HTTPGET, true);
+    // curl option "get"
+    curl_easy_setopt( curl, CURLOPT_HTTPGET, true );
 
-  // pass the write function and the struct to write to
-  curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, _taler_write_response);
-  curl_easy_setopt (curl, CURLOPT_WRITEDATA, (void *) product->response);
+    // pass the write function and the struct to write to
+    curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, _taler_write_response );
+    curl_easy_setopt( curl, CURLOPT_WRITEDATA, (void *)product->response );
 
-  // perform the call
-  CURLcode result = curl_easy_perform (curl);
-  if ( result != CURLE_OK )
-  {
-    printf ("could not communicate with \"%s\"\n", product->checkUrl);
-    return EXIT_FAILURE;
-  }
+    // perform the call
+    CURLcode result = curl_easy_perform(curl);
+    if( result != CURLE_OK ){
+        printf( "could not communicate with \"%s\"\n", product->checkUrl  );
+        return EXIT_FAILURE;
+    }
 
-  // reset the curl options
-  curl_easy_reset (curl);
+    // reset the curl options
+    curl_easy_reset( curl );
 
-  // clean up
-  curl_slist_free_all (list);
+    // clean up
+    curl_slist_free_all( list );
 
-  return EXIT_SUCCESS;
+    return EXIT_SUCCESS;
 }
 
-int taler_parse_json (const TalerResponse *response, const char *memberName,
-                      char **returnStr, bool isBoolean)
+int taler_parse_json( const TalerResponse *response, const char *memberName, 
char **returnStr, bool isBoolean )
 {
-  if ( ! (*returnStr) )
-    taler_alloc_string (returnStr, 1);
-  char *result = NULL;
-  char *temp = NULL;
-  char mbr[64];
-
-  if ( isBoolean )
-  {
-    // If the wanted member is of type boolean
-    sprintf (mbr, "\"%s\": true", memberName);
-    if ( (temp = strstr (response->string, mbr)) != NULL )
-      result = "true";
-    else
-      result = "false";
-  }
-  else{
-    // String type members
-    sprintf (mbr, "\"%s\":", memberName);
-    if ( (temp = strstr (response->string, mbr)) != NULL )
-    {
-      if ( (temp = strstr (response->string, ": ")) != NULL )
-      {
-        result = strtok (temp, "\"");
-        result = strtok (NULL, "\"");
-      }
+    if( !(*returnStr) )
+        taler_alloc_string( returnStr, 1 );
+    char *result = NULL;
+    char *temp = NULL;
+    char mbr[64];
+
+    json_error_t error;
+    json_t *root = json_loads( response->string, JSON_DISABLE_EOF_CHECK, 
&error );
+    if( !root ){
+        printf("taler_parse_json: %s\n", error.text );
+        return EXIT_FAILURE;
     }
-  }
-  if ( ! result )
-  {
-    printf ("taler_parse_json: no member named \"%s\" found!\n\n", memberName);
-    return EXIT_FAILURE;
-  }
-  temp = realloc (*returnStr, strlen (result) + 1);
-  if ( ! temp )
-  {
-    printf ("taler_parse_json: could not allocate memory for member\n");
-    return EXIT_FAILURE;
-  }
-  *returnStr = temp;
-  strcpy (*returnStr, result);
-
-  return EXIT_SUCCESS;
+    if( isBoolean ){
+        // If the wanted member is of type boolean
+        sprintf( mbr, "\"%s\": true", memberName );
+        if( (temp = strstr( response->string, mbr)) != NULL )
+            result = "true";
+        else
+            result = "false";
+    }else{
+        // String type members
+        sprintf( mbr, "\"%s\":", memberName );
+        if( (temp = strstr( response->string, mbr )) != NULL ){
+            if( (temp = strstr( response->string, ": ")) != NULL ){
+                result = strtok( temp, "\"");
+                result = strtok( NULL, "\"");
+            }
+        }
+    }
+    if( !result ){
+        printf( "taler_parse_json: no member named \"%s\" found!\n\n", 
memberName );
+        return EXIT_FAILURE;
+    }
+    temp = realloc( *returnStr, strlen(result) + 1 );
+    if( !temp ){
+        printf( "taler_parse_json: could not allocate memory for member\n" );
+        return EXIT_FAILURE;
+    }
+    *returnStr = temp;
+    strcpy( *returnStr, result );
+
+    return EXIT_SUCCESS;
 }
+
+
diff --git a/src/configuration.h b/src/configuration.h
index 4e008ab..897f0c3 100644
--- a/src/configuration.h
+++ b/src/configuration.h
@@ -26,11 +26,14 @@ along with
 #ifndef URL_H
 #define URL_H
 
-static const char*const ORDER = "/order";
-static const char*const CHECK = "/check-payment";
-static const char*const ORDER_CHECK = "?order_id=";
+static const char* const ORDER = "/order";
+static const char* const CHECK = "/check-payment";
 
-static const char*const BACKEND_BASE_URL = "https://backend.demo.taler.net";;
+static const char* const ORDER_CHECK = "?order_id=";
+
+static const char* const BACKEND_BASE_URL = "https://backend.demo.taler.net";;
+static const char* const FULLFILLMENT_URL = "taler://fulfillment-success";
+static const char* const FULLFILLMENT_MSG = "/Enjoy+your+";
 
 static const char*const AUTH_HEADER = "Authorization: ApiKey sandbox";
 
@@ -38,17 +41,4 @@ static const char*const JSON_PAID = "paid";
 static const char*const JSON_PAY_URI = "taler_pay_uri";
 static const char*const JSON_ORDER_ID = "order_id";
 
-
-
-//// will be replaced with libjansson functionalites 
-----------------------------------------------------------------------------------
-// ajust sprintf in taler_create_order_req communication.c if changed
-static const char*const JSON_ORDER = "{\n"
-                                     "  \"order\":"
-                                     "  {\n"
-                                     "      \"summary\": \"%s\",\n"
-                                     "      \"amount\": \"%s:%s\",\n"
-                                     "      \"fulfillment_url\": 
\"taler://fulfillment-success/Enjoy+your+%s!\"\n"
-                                     "  }\n"
-                                     "}";
-
 #endif // URL_H

-- 
To stop receiving notification emails like this one, please contact
address@hidden.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]