LCOV - code coverage report
Current view: top level - lib - merchant_api_get_products.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 0 76 0.0 %
Date: 2022-08-25 06:17:04 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 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 "merchant_api_curl_defaults.h"
      30             : #include <taler/taler_json_lib.h>
      31             : #include <taler/taler_signatures.h>
      32             : 
      33             : 
      34             : /**
      35             :  * Handle for a GET /products operation.
      36             :  */
      37             : struct TALER_MERCHANT_ProductsGetHandle
      38             : {
      39             :   /**
      40             :    * The url for this request.
      41             :    */
      42             :   char *url;
      43             : 
      44             :   /**
      45             :    * Handle for the request.
      46             :    */
      47             :   struct GNUNET_CURL_Job *job;
      48             : 
      49             :   /**
      50             :    * Function to call with the result.
      51             :    */
      52             :   TALER_MERCHANT_ProductsGetCallback cb;
      53             : 
      54             :   /**
      55             :    * Closure for @a cb.
      56             :    */
      57             :   void *cb_cls;
      58             : 
      59             :   /**
      60             :    * Reference to the execution context.
      61             :    */
      62             :   struct GNUNET_CURL_Context *ctx;
      63             : 
      64             : };
      65             : 
      66             : 
      67             : /**
      68             :  * Parse product information from @a ia.
      69             :  *
      70             :  * @param ia JSON array (or NULL!) with product data
      71             :  * @param pgh operation handle
      72             :  * @return #GNUNET_OK on success
      73             :  */
      74             : static int
      75           0 : parse_products (const json_t *ia,
      76             :                 struct TALER_MERCHANT_ProductsGetHandle *pgh)
      77           0 : {
      78           0 :   unsigned int ies_len = json_array_size (ia);
      79           0 :   struct TALER_MERCHANT_InventoryEntry ies[ies_len];
      80             :   size_t index;
      81             :   json_t *value;
      82             :   int ret;
      83             : 
      84           0 :   ret = GNUNET_OK;
      85           0 :   json_array_foreach (ia, index, value) {
      86           0 :     struct TALER_MERCHANT_InventoryEntry *ie = &ies[index];
      87             :     struct GNUNET_JSON_Specification spec[] = {
      88           0 :       GNUNET_JSON_spec_string ("product_id",
      89             :                                &ie->product_id),
      90           0 :       GNUNET_JSON_spec_end ()
      91             :     };
      92             : 
      93           0 :     if (GNUNET_OK !=
      94           0 :         GNUNET_JSON_parse (value,
      95             :                            spec,
      96             :                            NULL, NULL))
      97             :     {
      98           0 :       GNUNET_break_op (0);
      99           0 :       ret = GNUNET_SYSERR;
     100           0 :       continue;
     101             :     }
     102           0 :     if (GNUNET_SYSERR == ret)
     103           0 :       break;
     104             :   }
     105           0 :   if (GNUNET_OK == ret)
     106             :   {
     107           0 :     struct TALER_MERCHANT_HttpResponse hr = {
     108             :       .http_status = MHD_HTTP_OK
     109             :     };
     110             : 
     111           0 :     pgh->cb (pgh->cb_cls,
     112             :              &hr,
     113             :              ies_len,
     114             :              ies);
     115           0 :     pgh->cb = NULL; /* just to be sure */
     116             :   }
     117           0 :   return ret;
     118             : }
     119             : 
     120             : 
     121             : /**
     122             :  * Function called when we're done processing the
     123             :  * HTTP /products request.
     124             :  *
     125             :  * @param cls the `struct TALER_MERCHANT_ProductsGetHandle`
     126             :  * @param response_code HTTP response code, 0 on error
     127             :  * @param response response body, NULL if not in JSON
     128             :  */
     129             : static void
     130           0 : handle_get_products_finished (void *cls,
     131             :                               long response_code,
     132             :                               const void *response)
     133             : {
     134           0 :   struct TALER_MERCHANT_ProductsGetHandle *pgh = cls;
     135           0 :   const json_t *json = response;
     136           0 :   struct TALER_MERCHANT_HttpResponse hr = {
     137           0 :     .http_status = (unsigned int) response_code,
     138             :     .reply = json
     139             :   };
     140             : 
     141           0 :   pgh->job = NULL;
     142           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     143             :               "Got /products response with status code %u\n",
     144             :               (unsigned int) response_code);
     145           0 :   switch (response_code)
     146             :   {
     147           0 :   case MHD_HTTP_OK:
     148             :     {
     149             :       json_t *products;
     150             :       struct GNUNET_JSON_Specification spec[] = {
     151           0 :         GNUNET_JSON_spec_json ("products",
     152             :                                &products),
     153           0 :         GNUNET_JSON_spec_end ()
     154             :       };
     155             : 
     156           0 :       if (GNUNET_OK !=
     157           0 :           GNUNET_JSON_parse (json,
     158             :                              spec,
     159             :                              NULL, NULL))
     160             :       {
     161           0 :         hr.http_status = 0;
     162           0 :         hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     163             :       }
     164             :       else
     165             :       {
     166           0 :         if ( (! json_is_array (products)) ||
     167             :              (GNUNET_OK ==
     168           0 :               parse_products (products,
     169             :                               pgh)) )
     170             :         {
     171           0 :           GNUNET_JSON_parse_free (spec);
     172           0 :           TALER_MERCHANT_products_get_cancel (pgh);
     173           0 :           return;
     174             :         }
     175             :         else
     176             :         {
     177           0 :           hr.http_status = 0;
     178           0 :           hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     179             :         }
     180             :       }
     181           0 :       GNUNET_JSON_parse_free (spec);
     182           0 :       break;
     183             :     }
     184           0 :   case MHD_HTTP_UNAUTHORIZED:
     185           0 :     hr.ec = TALER_JSON_get_error_code (json);
     186           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     187             :     /* Nothing really to verify, merchant says we need to authenticate. */
     188           0 :     break;
     189           0 :   default:
     190             :     /* unexpected response code */
     191           0 :     hr.ec = TALER_JSON_get_error_code (json);
     192           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     193           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     194             :                 "Unexpected response code %u/%d\n",
     195             :                 (unsigned int) response_code,
     196             :                 (int) hr.ec);
     197           0 :     break;
     198             :   }
     199           0 :   pgh->cb (pgh->cb_cls,
     200             :            &hr,
     201             :            0,
     202             :            NULL);
     203           0 :   TALER_MERCHANT_products_get_cancel (pgh);
     204             : }
     205             : 
     206             : 
     207             : struct TALER_MERCHANT_ProductsGetHandle *
     208           0 : TALER_MERCHANT_products_get (
     209             :   struct GNUNET_CURL_Context *ctx,
     210             :   const char *backend_url,
     211             :   TALER_MERCHANT_ProductsGetCallback cb,
     212             :   void *cb_cls)
     213             : {
     214             :   struct TALER_MERCHANT_ProductsGetHandle *pgh;
     215             :   CURL *eh;
     216             : 
     217           0 :   pgh = GNUNET_new (struct TALER_MERCHANT_ProductsGetHandle);
     218           0 :   pgh->ctx = ctx;
     219           0 :   pgh->cb = cb;
     220           0 :   pgh->cb_cls = cb_cls;
     221           0 :   pgh->url = TALER_url_join (backend_url,
     222             :                              "private/products",
     223             :                              NULL);
     224           0 :   if (NULL == pgh->url)
     225             :   {
     226           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     227             :                 "Could not construct request URL.\n");
     228           0 :     GNUNET_free (pgh);
     229           0 :     return NULL;
     230             :   }
     231           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     232             :               "Requesting URL '%s'\n",
     233             :               pgh->url);
     234           0 :   eh = TALER_MERCHANT_curl_easy_get_ (pgh->url);
     235           0 :   pgh->job = GNUNET_CURL_job_add (ctx,
     236             :                                   eh,
     237             :                                   &handle_get_products_finished,
     238             :                                   pgh);
     239           0 :   return pgh;
     240             : }
     241             : 
     242             : 
     243             : void
     244           0 : TALER_MERCHANT_products_get_cancel (
     245             :   struct TALER_MERCHANT_ProductsGetHandle *pgh)
     246             : {
     247           0 :   if (NULL != pgh->job)
     248           0 :     GNUNET_CURL_job_cancel (pgh->job);
     249           0 :   GNUNET_free (pgh->url);
     250           0 :   GNUNET_free (pgh);
     251           0 : }

Generated by: LCOV version 1.14