LCOV - code coverage report
Current view: top level - lib - merchant_api_delete-private-products-PRODUCT_ID.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 77.0 % 61 47
Test Date: 2026-09-11 17:45:24 Functions: 100.0 % 4 4

            Line data    Source code
       1              : /*
       2              :   This file is part of TALER
       3              :   Copyright (C) 2014-2026 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 src/lib/merchant_api_delete-private-products-PRODUCT_ID.c
      19              :  * @brief Implementation of the DELETE /private/products/$PRODUCT_ID request
      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/delete-private-products-PRODUCT_ID.h>
      29              : #include "merchant_api_curl_defaults.h"
      30              : #include <taler/taler_json_lib.h>
      31              : 
      32              : 
      33              : /**
      34              :  * Handle for a DELETE /private/products/$PRODUCT_ID operation.
      35              :  */
      36              : struct TALER_MERCHANT_DeletePrivateProductHandle
      37              : {
      38              :   /**
      39              :    * Base URL of the merchant backend.
      40              :    */
      41              :   char *base_url;
      42              : 
      43              :   /**
      44              :    * The full URL for this request.
      45              :    */
      46              :   char *url;
      47              : 
      48              :   /**
      49              :    * Handle for the request.
      50              :    */
      51              :   struct GNUNET_CURL_Job *job;
      52              : 
      53              :   /**
      54              :    * Function to call with the result.
      55              :    */
      56              :   TALER_MERCHANT_DeletePrivateProductCallback cb;
      57              : 
      58              :   /**
      59              :    * Closure for @a cb.
      60              :    */
      61              :   TALER_MERCHANT_DELETE_PRIVATE_PRODUCT_RESULT_CLOSURE *cb_cls;
      62              : 
      63              :   /**
      64              :    * Reference to the execution context.
      65              :    */
      66              :   struct GNUNET_CURL_Context *ctx;
      67              : 
      68              :   /**
      69              :    * Identifier of the product to delete.
      70              :    */
      71              :   char *product_id;
      72              : };
      73              : 
      74              : 
      75              : /**
      76              :  * Function called when we're done processing the
      77              :  * HTTP DELETE /private/products/$PRODUCT_ID request.
      78              :  *
      79              :  * @param cls the `struct TALER_MERCHANT_DeletePrivateProductHandle`
      80              :  * @param response_code HTTP response code, 0 on error
      81              :  * @param response response body, NULL if not in JSON
      82              :  */
      83              : static void
      84           10 : handle_delete_product_finished (void *cls,
      85              :                                 long response_code,
      86              :                                 const void *response)
      87              : {
      88           10 :   struct TALER_MERCHANT_DeletePrivateProductHandle *dph = cls;
      89           10 :   const json_t *json = response;
      90           10 :   struct TALER_MERCHANT_DeletePrivateProductResponse dpr = {
      91           10 :     .hr.http_status = (unsigned int) response_code,
      92              :     .hr.reply = json
      93              :   };
      94              : 
      95           10 :   dph->job = NULL;
      96           10 :   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
      97              :               "Got /private/products/$PRODUCT_ID DELETE response with status code %u\n",
      98              :               (unsigned int) response_code);
      99           10 :   switch (response_code)
     100              :   {
     101            6 :   case MHD_HTTP_NO_CONTENT:
     102            6 :     break;
     103            0 :   case MHD_HTTP_UNAUTHORIZED:
     104            0 :     dpr.hr.ec = TALER_JSON_get_error_code (json);
     105            0 :     dpr.hr.hint = TALER_JSON_get_error_hint (json);
     106            0 :     break;
     107            2 :   case MHD_HTTP_NOT_FOUND:
     108            2 :     dpr.hr.ec = TALER_JSON_get_error_code (json);
     109            2 :     dpr.hr.hint = TALER_JSON_get_error_hint (json);
     110            2 :     break;
     111            2 :   case MHD_HTTP_CONFLICT:
     112            2 :     dpr.hr.ec = TALER_JSON_get_error_code (json);
     113            2 :     dpr.hr.hint = TALER_JSON_get_error_hint (json);
     114            2 :     break;
     115            0 :   default:
     116            0 :     dpr.hr.ec = TALER_JSON_get_error_code (json);
     117            0 :     dpr.hr.hint = TALER_JSON_get_error_hint (json);
     118            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     119              :                 "Unexpected response code %u/%d for DELETE /private/products/$PRODUCT_ID\n",
     120              :                 (unsigned int) response_code,
     121              :                 (int) dpr.hr.ec);
     122            0 :     break;
     123              :   }
     124           10 :   dph->cb (dph->cb_cls,
     125              :            &dpr);
     126           10 :   TALER_MERCHANT_delete_private_product_cancel (dph);
     127           10 : }
     128              : 
     129              : 
     130              : struct TALER_MERCHANT_DeletePrivateProductHandle *
     131           10 : TALER_MERCHANT_delete_private_product_create (
     132              :   struct GNUNET_CURL_Context *ctx,
     133              :   const char *url,
     134              :   const char *product_id)
     135              : {
     136              :   struct TALER_MERCHANT_DeletePrivateProductHandle *dph;
     137              : 
     138           10 :   dph = GNUNET_new (struct TALER_MERCHANT_DeletePrivateProductHandle);
     139           10 :   dph->ctx = ctx;
     140           10 :   dph->base_url = GNUNET_strdup (url);
     141           10 :   dph->product_id = GNUNET_strdup (product_id);
     142           10 :   return dph;
     143              : }
     144              : 
     145              : 
     146              : enum TALER_ErrorCode
     147           10 : TALER_MERCHANT_delete_private_product_start (
     148              :   struct TALER_MERCHANT_DeletePrivateProductHandle *dph,
     149              :   TALER_MERCHANT_DeletePrivateProductCallback cb,
     150              :   TALER_MERCHANT_DELETE_PRIVATE_PRODUCT_RESULT_CLOSURE *cb_cls)
     151              : {
     152              :   CURL *eh;
     153              : 
     154           10 :   dph->cb = cb;
     155           10 :   dph->cb_cls = cb_cls;
     156              :   {
     157              :     char *path;
     158              : 
     159           10 :     GNUNET_asprintf (&path,
     160              :                      "private/products/%s",
     161              :                      dph->product_id);
     162           10 :     dph->url = TALER_url_join (dph->base_url,
     163              :                                path,
     164              :                                NULL);
     165           10 :     GNUNET_free (path);
     166              :   }
     167           10 :   if (NULL == dph->url)
     168            0 :     return TALER_EC_GENERIC_CONFIGURATION_INVALID;
     169           10 :   eh = TALER_MERCHANT_curl_easy_get_ (dph->url);
     170           10 :   if (NULL == eh)
     171            0 :     return TALER_EC_GENERIC_CONFIGURATION_INVALID;
     172           10 :   GNUNET_assert (CURLE_OK ==
     173              :                  curl_easy_setopt (eh,
     174              :                                    CURLOPT_CUSTOMREQUEST,
     175              :                                    MHD_HTTP_METHOD_DELETE));
     176           10 :   dph->job = GNUNET_CURL_job_add (dph->ctx,
     177              :                                   eh,
     178              :                                   &handle_delete_product_finished,
     179              :                                   dph);
     180           10 :   if (NULL == dph->job)
     181            0 :     return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
     182           10 :   return TALER_EC_NONE;
     183              : }
     184              : 
     185              : 
     186              : void
     187           10 : TALER_MERCHANT_delete_private_product_cancel (
     188              :   struct TALER_MERCHANT_DeletePrivateProductHandle *dph)
     189              : {
     190           10 :   if (NULL != dph->job)
     191              :   {
     192            0 :     GNUNET_CURL_job_cancel (dph->job);
     193            0 :     dph->job = NULL;
     194              :   }
     195           10 :   GNUNET_free (dph->url);
     196           10 :   GNUNET_free (dph->product_id);
     197           10 :   GNUNET_free (dph->base_url);
     198           10 :   GNUNET_free (dph);
     199           10 : }
     200              : 
     201              : 
     202              : /* end of merchant_api_delete-private-products-PRODUCT_ID-new.c */
        

Generated by: LCOV version 2.0-1