LCOV - code coverage report
Current view: top level - lib - merchant_api_get_products.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 73 0.0 %
Date: 2020-08-15 06:12:35 Functions: 0 4 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   Copyright (C) 2014-2018, 2020 Taler Systems SA
       4             : 
       5             :   TALER is free software; you can redistribute it and/or modify it under the
       6             :   terms of the GNU Lesser General Public License as published by the Free Software
       7             :   Foundation; either version 2.1, or (at your option) any later version.
       8             : 
       9             :   TALER is distributed in the hope that it will be useful, but WITHOUT ANY
      10             :   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
      11             :   A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
      12             : 
      13             :   You should have received a copy of the GNU Lesser General Public License along with
      14             :   TALER; see the file COPYING.LGPL.  If not, see
      15             :   <http://www.gnu.org/licenses/>
      16             : */
      17             : /**
      18             :  * @file lib/merchant_api_get_products.c
      19             :  * @brief Implementation of the GET /products request of the merchant's HTTP API
      20             :  * @author Christian Grothoff
      21             :  */
      22             : #include "platform.h"
      23             : #include <curl/curl.h>
      24             : #include <jansson.h>
      25             : #include <microhttpd.h> /* just for HTTP status codes */
      26             : #include <gnunet/gnunet_util_lib.h>
      27             : #include <gnunet/gnunet_curl_lib.h>
      28             : #include "taler_merchant_service.h"
      29             : #include <taler/taler_json_lib.h>
      30             : #include <taler/taler_signatures.h>
      31             : 
      32             : 
      33             : /**
      34             :  * Handle for a GET /products operation.
      35             :  */
      36             : struct TALER_MERCHANT_ProductsGetHandle
      37             : {
      38             :   /**
      39             :    * The url for this request.
      40             :    */
      41             :   char *url;
      42             : 
      43             :   /**
      44             :    * Handle for the request.
      45             :    */
      46             :   struct GNUNET_CURL_Job *job;
      47             : 
      48             :   /**
      49             :    * Function to call with the result.
      50             :    */
      51             :   TALER_MERCHANT_ProductsGetCallback cb;
      52             : 
      53             :   /**
      54             :    * Closure for @a cb.
      55             :    */
      56             :   void *cb_cls;
      57             : 
      58             :   /**
      59             :    * Reference to the execution context.
      60             :    */
      61             :   struct GNUNET_CURL_Context *ctx;
      62             : 
      63             : };
      64             : 
      65             : 
      66             : /**
      67             :  * Parse product information from @a ia.
      68             :  *
      69             :  * @param ia JSON array (or NULL!) with product data
      70             :  * @param pgh operation handle
      71             :  * @return #GNUNET_OK on success
      72             :  */
      73             : static int
      74           0 : parse_products (const json_t *ia,
      75             :                 struct TALER_MERCHANT_ProductsGetHandle *pgh)
      76           0 : {
      77           0 :   unsigned int ies_len = json_array_size (ia);
      78           0 :   struct TALER_MERCHANT_InventoryEntry ies[ies_len];
      79             :   size_t index;
      80             :   json_t *value;
      81             :   int ret;
      82             : 
      83           0 :   ret = GNUNET_OK;
      84           0 :   json_array_foreach (ia, index, value) {
      85           0 :     struct TALER_MERCHANT_InventoryEntry *ie = &ies[index];
      86             :     struct GNUNET_JSON_Specification spec[] = {
      87           0 :       GNUNET_JSON_spec_string ("product_id",
      88             :                                &ie->product_id),
      89           0 :       GNUNET_JSON_spec_end ()
      90             :     };
      91             : 
      92           0 :     if (GNUNET_OK !=
      93           0 :         GNUNET_JSON_parse (value,
      94             :                            spec,
      95             :                            NULL, NULL))
      96             :     {
      97           0 :       GNUNET_break_op (0);
      98           0 :       ret = GNUNET_SYSERR;
      99           0 :       continue;
     100             :     }
     101           0 :     if (GNUNET_SYSERR == ret)
     102           0 :       break;
     103             :   }
     104           0 :   if (GNUNET_OK == ret)
     105             :   {
     106           0 :     struct TALER_MERCHANT_HttpResponse hr = {
     107             :       .http_status = MHD_HTTP_OK
     108             :     };
     109             : 
     110           0 :     pgh->cb (pgh->cb_cls,
     111             :              &hr,
     112             :              ies_len,
     113             :              ies);
     114           0 :     pgh->cb = NULL; /* just to be sure */
     115             :   }
     116           0 :   return ret;
     117             : }
     118             : 
     119             : 
     120             : /**
     121             :  * Function called when we're done processing the
     122             :  * HTTP /products request.
     123             :  *
     124             :  * @param cls the `struct TALER_MERCHANT_ProductsGetHandle`
     125             :  * @param response_code HTTP response code, 0 on error
     126             :  * @param json response body, NULL if not in JSON
     127             :  */
     128             : static void
     129           0 : handle_get_products_finished (void *cls,
     130             :                               long response_code,
     131             :                               const void *response)
     132             : {
     133           0 :   struct TALER_MERCHANT_ProductsGetHandle *pgh = cls;
     134           0 :   const json_t *json = response;
     135           0 :   struct TALER_MERCHANT_HttpResponse hr = {
     136           0 :     .http_status = (unsigned int) response_code,
     137             :     .reply = json
     138             :   };
     139             : 
     140           0 :   pgh->job = NULL;
     141           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     142             :               "Got /products response with status code %u\n",
     143             :               (unsigned int) response_code);
     144           0 :   switch (response_code)
     145             :   {
     146           0 :   case MHD_HTTP_OK:
     147             :     {
     148             :       json_t *products;
     149             :       struct GNUNET_JSON_Specification spec[] = {
     150           0 :         GNUNET_JSON_spec_json ("products",
     151             :                                &products),
     152           0 :         GNUNET_JSON_spec_end ()
     153             :       };
     154             : 
     155           0 :       if (GNUNET_OK !=
     156           0 :           GNUNET_JSON_parse (json,
     157             :                              spec,
     158             :                              NULL, NULL))
     159             :       {
     160           0 :         hr.http_status = 0;
     161           0 :         hr.ec = TALER_EC_INVALID_RESPONSE;
     162             :       }
     163             :       else
     164             :       {
     165           0 :         if ( (! json_is_array (products)) ||
     166             :              (GNUNET_OK ==
     167           0 :               parse_products (products,
     168             :                               pgh)) )
     169             :         {
     170           0 :           GNUNET_JSON_parse_free (spec);
     171           0 :           TALER_MERCHANT_products_get_cancel (pgh);
     172           0 :           return;
     173             :         }
     174             :         else
     175             :         {
     176           0 :           hr.http_status = 0;
     177           0 :           hr.ec = TALER_EC_INVALID_RESPONSE;
     178             :         }
     179             :       }
     180           0 :       GNUNET_JSON_parse_free (spec);
     181           0 :       break;
     182             :     }
     183           0 :   default:
     184             :     /* unexpected response code */
     185           0 :     hr.ec = TALER_JSON_get_error_code (json);
     186           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     187           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     188             :                 "Unexpected response code %u/%d\n",
     189             :                 (unsigned int) response_code,
     190             :                 (int) hr.ec);
     191           0 :     break;
     192             :   }
     193           0 :   pgh->cb (pgh->cb_cls,
     194             :            &hr,
     195             :            0,
     196             :            NULL);
     197           0 :   TALER_MERCHANT_products_get_cancel (pgh);
     198             : }
     199             : 
     200             : 
     201             : /**
     202             :  * Make a GET /products request.
     203             :  *
     204             :  * @param ctx the context
     205             :  * @param backend_url HTTP base URL for the backend
     206             :  * @param cb function to call with the backend's inventory information
     207             :  * @param cb_cls closure for @a cb
     208             :  * @return the request handle; NULL upon error
     209             :  */
     210             : struct TALER_MERCHANT_ProductsGetHandle *
     211           0 : TALER_MERCHANT_products_get (
     212             :   struct GNUNET_CURL_Context *ctx,
     213             :   const char *backend_url,
     214             :   TALER_MERCHANT_ProductsGetCallback cb,
     215             :   void *cb_cls)
     216             : {
     217             :   struct TALER_MERCHANT_ProductsGetHandle *pgh;
     218             :   CURL *eh;
     219             : 
     220           0 :   pgh = GNUNET_new (struct TALER_MERCHANT_ProductsGetHandle);
     221           0 :   pgh->ctx = ctx;
     222           0 :   pgh->cb = cb;
     223           0 :   pgh->cb_cls = cb_cls;
     224           0 :   pgh->url = TALER_url_join (backend_url,
     225             :                              "private/products",
     226             :                              NULL);
     227           0 :   if (NULL == pgh->url)
     228             :   {
     229           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     230             :                 "Could not construct request URL.\n");
     231           0 :     GNUNET_free (pgh);
     232           0 :     return NULL;
     233             :   }
     234           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     235             :               "Requesting URL '%s'\n",
     236             :               pgh->url);
     237           0 :   eh = curl_easy_init ();
     238           0 :   GNUNET_assert (CURLE_OK ==
     239             :                  curl_easy_setopt (eh,
     240             :                                    CURLOPT_URL,
     241             :                                    pgh->url));
     242           0 :   pgh->job = GNUNET_CURL_job_add (ctx,
     243             :                                   eh,
     244             :                                   &handle_get_products_finished,
     245             :                                   pgh);
     246           0 :   return pgh;
     247             : }
     248             : 
     249             : 
     250             : /**
     251             :  * Cancel /products request.  Must not be called by clients after
     252             :  * the callback was invoked.
     253             :  *
     254             :  * @param pgh request to cancel.
     255             :  */
     256             : void
     257           0 : TALER_MERCHANT_products_get_cancel (
     258             :   struct TALER_MERCHANT_ProductsGetHandle *pgh)
     259             : {
     260           0 :   if (NULL != pgh->job)
     261           0 :     GNUNET_CURL_job_cancel (pgh->job);
     262           0 :   GNUNET_free (pgh->url);
     263           0 :   GNUNET_free (pgh);
     264           0 : }

Generated by: LCOV version 1.14