LCOV - code coverage report
Current view: top level - lib - merchant_api_get_product.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 69 0.0 %
Date: 2020-08-15 06:12:35 Functions: 0 3 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_product.c
      19             :  * @brief Implementation of the GET /product/$ID 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/$ID operation.
      35             :  */
      36             : struct TALER_MERCHANT_ProductGetHandle
      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_ProductGetCallback 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             :  * Function called when we're done processing the
      68             :  * HTTP GET /products/$ID request.
      69             :  *
      70             :  * @param cls the `struct TALER_MERCHANT_ProductGetHandle`
      71             :  * @param response_code HTTP response code, 0 on error
      72             :  * @param json response body, NULL if not in JSON
      73             :  */
      74             : static void
      75           0 : handle_get_product_finished (void *cls,
      76             :                              long response_code,
      77             :                              const void *response)
      78             : {
      79           0 :   struct TALER_MERCHANT_ProductGetHandle *pgh = cls;
      80           0 :   const json_t *json = response;
      81           0 :   struct TALER_MERCHANT_HttpResponse hr = {
      82           0 :     .http_status = (unsigned int) response_code,
      83             :     .reply = json
      84             :   };
      85             : 
      86           0 :   pgh->job = NULL;
      87           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
      88             :               "Got /products/$ID response with status code %u\n",
      89             :               (unsigned int) response_code);
      90           0 :   switch (response_code)
      91             :   {
      92           0 :   case MHD_HTTP_OK:
      93             :     {
      94             :       const char *description;
      95             :       json_t *description_i18n;
      96             :       const char *unit;
      97             :       struct TALER_Amount price;
      98             :       json_t *image;
      99             :       json_t *taxes;
     100             :       int64_t total_stock;
     101             :       uint64_t total_sold;
     102             :       uint64_t total_lost;
     103             :       json_t *address;
     104           0 :       bool rst_ok = true;
     105           0 :       struct GNUNET_TIME_Absolute next_restock = {0};
     106             :       struct GNUNET_JSON_Specification spec[] = {
     107           0 :         GNUNET_JSON_spec_string ("description",
     108             :                                  &description),
     109           0 :         GNUNET_JSON_spec_json ("description_i18n",
     110             :                                &description_i18n),
     111           0 :         GNUNET_JSON_spec_string ("unit",
     112             :                                  &unit),
     113           0 :         TALER_JSON_spec_amount ("price",
     114             :                                 &price),
     115           0 :         GNUNET_JSON_spec_json ("image",
     116             :                                &image),
     117           0 :         GNUNET_JSON_spec_json ("taxes",
     118             :                                &taxes),
     119           0 :         GNUNET_JSON_spec_int64 ("total_stock",
     120             :                                 &total_stock),
     121           0 :         GNUNET_JSON_spec_uint64 ("total_sold",
     122             :                                  &total_sold),
     123           0 :         GNUNET_JSON_spec_uint64 ("total_lost",
     124             :                                  &total_lost),
     125           0 :         GNUNET_JSON_spec_json ("address",
     126             :                                &address),
     127           0 :         GNUNET_JSON_spec_end ()
     128             :       };
     129             : 
     130           0 :       if (NULL !=
     131           0 :           json_object_get (json,
     132             :                            "next_restock"))
     133             :       {
     134             :         struct GNUNET_JSON_Specification spect[] = {
     135           0 :           TALER_JSON_spec_absolute_time ("next_restock",
     136             :                                          &next_restock),
     137           0 :           GNUNET_JSON_spec_end ()
     138             :         };
     139             : 
     140           0 :         if (GNUNET_OK !=
     141           0 :             GNUNET_JSON_parse (json,
     142             :                                spect,
     143             :                                NULL, NULL))
     144           0 :           rst_ok = false;
     145             :       }
     146             : 
     147             : 
     148           0 :       if ( (rst_ok) &&
     149             :            (GNUNET_OK ==
     150           0 :             GNUNET_JSON_parse (json,
     151             :                                spec,
     152             :                                NULL, NULL)) )
     153             :       {
     154           0 :         pgh->cb (pgh->cb_cls,
     155             :                  &hr,
     156             :                  description,
     157             :                  description_i18n,
     158             :                  unit,
     159             :                  &price,
     160             :                  image,
     161             :                  taxes,
     162             :                  total_stock,
     163             :                  total_sold,
     164             :                  total_lost,
     165             :                  address,
     166             :                  next_restock);
     167           0 :         GNUNET_JSON_parse_free (spec);
     168           0 :         TALER_MERCHANT_product_get_cancel (pgh);
     169           0 :         return;
     170             :       }
     171           0 :       hr.http_status = 0;
     172           0 :       hr.ec = TALER_EC_INVALID_RESPONSE;
     173           0 :       GNUNET_JSON_parse_free (spec);
     174           0 :       break;
     175             :     }
     176           0 :   default:
     177             :     /* unexpected response code */
     178           0 :     hr.ec = TALER_JSON_get_error_code (json);
     179           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     180           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     181             :                 "Unexpected response code %u/%d\n",
     182             :                 (unsigned int) response_code,
     183             :                 (int) hr.ec);
     184           0 :     break;
     185             :   }
     186           0 :   pgh->cb (pgh->cb_cls,
     187             :            &hr,
     188             :            NULL,
     189             :            NULL,
     190             :            NULL,
     191             :            NULL,
     192             :            NULL,
     193             :            NULL,
     194             :            0,
     195             :            0,
     196             :            0,
     197             :            NULL,
     198             :            GNUNET_TIME_UNIT_FOREVER_ABS);
     199           0 :   TALER_MERCHANT_product_get_cancel (pgh);
     200             : }
     201             : 
     202             : 
     203             : /**
     204             :  * Make a GET /product/$ID request to get details about an
     205             :  * individual product.
     206             :  *
     207             :  * @param ctx the context
     208             :  * @param backend_url HTTP base URL for the backend
     209             :  * @param product_id identifier of the product to inquire about
     210             :  * @param cb function to call with the backend's product information
     211             :  * @param cb_cls closure for @a cb
     212             :  * @return the request handle; NULL upon error
     213             :  */
     214             : struct TALER_MERCHANT_ProductGetHandle *
     215           0 : TALER_MERCHANT_product_get (
     216             :   struct GNUNET_CURL_Context *ctx,
     217             :   const char *backend_url,
     218             :   const char *product_id,
     219             :   TALER_MERCHANT_ProductGetCallback cb,
     220             :   void *cb_cls)
     221             : {
     222             :   struct TALER_MERCHANT_ProductGetHandle *pgh;
     223             :   CURL *eh;
     224             : 
     225           0 :   pgh = GNUNET_new (struct TALER_MERCHANT_ProductGetHandle);
     226           0 :   pgh->ctx = ctx;
     227           0 :   pgh->cb = cb;
     228           0 :   pgh->cb_cls = cb_cls;
     229             :   {
     230             :     char *path;
     231             : 
     232           0 :     GNUNET_asprintf (&path,
     233             :                      "private/products/%s",
     234             :                      product_id);
     235           0 :     pgh->url = TALER_url_join (backend_url,
     236             :                                path,
     237             :                                NULL);
     238           0 :     GNUNET_free (path);
     239             :   }
     240           0 :   if (NULL == pgh->url)
     241             :   {
     242           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     243             :                 "Could not construct request URL.\n");
     244           0 :     GNUNET_free (pgh);
     245           0 :     return NULL;
     246             :   }
     247           0 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     248             :               "Requesting URL '%s'\n",
     249             :               pgh->url);
     250           0 :   eh = curl_easy_init ();
     251           0 :   GNUNET_assert (CURLE_OK ==
     252             :                  curl_easy_setopt (eh,
     253             :                                    CURLOPT_URL,
     254             :                                    pgh->url));
     255           0 :   pgh->job = GNUNET_CURL_job_add (ctx,
     256             :                                   eh,
     257             :                                   &handle_get_product_finished,
     258             :                                   pgh);
     259           0 :   return pgh;
     260             : }
     261             : 
     262             : 
     263             : /**
     264             :  * Cancel GET /product/$ID request.  Must not be called by clients after
     265             :  * the callback was invoked.
     266             :  *
     267             :  * @param pgh request to cancel.
     268             :  */
     269             : void
     270           0 : TALER_MERCHANT_product_get_cancel (
     271             :   struct TALER_MERCHANT_ProductGetHandle *pgh)
     272             : {
     273           0 :   if (NULL != pgh->job)
     274           0 :     GNUNET_CURL_job_cancel (pgh->job);
     275           0 :   GNUNET_free (pgh->url);
     276           0 :   GNUNET_free (pgh);
     277           0 : }

Generated by: LCOV version 1.14