LCOV - code coverage report
Current view: top level - lib - merchant_api_patch_order_forget.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 0 89 0.0 %
Date: 2022-08-25 06:17:04 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) 2020, 2021 Taler Systems SA
       4             : 
       5             :   TALER is free software; you can redistribute it and/or modify
       6             :   it under the terms of the GNU Lesser General Public License as
       7             :   published by the Free Software Foundation; either version 2.1,
       8             :   or (at your option) any later version.
       9             : 
      10             :   TALER is distributed in the hope that it will be useful,
      11             :   but WITHOUT ANY WARRANTY; without even the implied warranty of
      12             :   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13             :   GNU Lesser General Public License for more details.
      14             : 
      15             :   You should have received a copy of the GNU Lesser General
      16             :   Public License along with TALER; see the file COPYING.LGPL.
      17             :   If not, see <http://www.gnu.org/licenses/>
      18             : */
      19             : /**
      20             :  * @file merchant_api_patch_order_forget.c
      21             :  * @brief Implementation of the PATCH /orders/$ID/forget request
      22             :  *        of the merchant's HTTP API
      23             :  * @author Jonathan Buchanan
      24             :  */
      25             : #include "platform.h"
      26             : #include <curl/curl.h>
      27             : #include <jansson.h>
      28             : #include <microhttpd.h> /* just for HTTP status codes */
      29             : #include <gnunet/gnunet_util_lib.h>
      30             : #include "taler_merchant_service.h"
      31             : #include "merchant_api_curl_defaults.h"
      32             : #include <taler/taler_json_lib.h>
      33             : #include <taler/taler_curl_lib.h>
      34             : 
      35             : 
      36             : /**
      37             :  * Handle for a PATCH /orders/$ORDER_ID/forget operation.
      38             :  */
      39             : struct TALER_MERCHANT_OrderForgetHandle
      40             : {
      41             : 
      42             :   /**
      43             :    * The url for this request.
      44             :    */
      45             :   char *url;
      46             : 
      47             :   /**
      48             :    * Handle for the request.
      49             :    */
      50             :   struct GNUNET_CURL_Job *job;
      51             : 
      52             :   /**
      53             :    * Function to call with the result.
      54             :    */
      55             :   TALER_MERCHANT_ForgetCallback cb;
      56             : 
      57             :   /**
      58             :    * Closure for @a cb.
      59             :    */
      60             :   void *cb_cls;
      61             : 
      62             :   /**
      63             :    * Reference to the execution context.
      64             :    */
      65             :   struct GNUNET_CURL_Context *ctx;
      66             : 
      67             :   /**
      68             :    * Minor context that holds body and headers.
      69             :    */
      70             :   struct TALER_CURL_PostContext post_ctx;
      71             : 
      72             : };
      73             : 
      74             : 
      75             : /**
      76             :  * Function called when we're done processing the
      77             :  * HTTP PATCH /orders/$ORDER_ID/forget request.
      78             :  *
      79             :  * @param cls the `struct TALER_MERCHANT_OrderForgetHandle`
      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           0 : handle_forget_finished (void *cls,
      85             :                         long response_code,
      86             :                         const void *response)
      87             : {
      88           0 :   struct TALER_MERCHANT_OrderForgetHandle *ofh = cls;
      89           0 :   const json_t *json = response;
      90           0 :   struct TALER_MERCHANT_HttpResponse hr = {
      91           0 :     .http_status = (unsigned int) response_code,
      92             :     .reply = json
      93             :   };
      94             : 
      95           0 :   ofh->job = NULL;
      96           0 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
      97             :               "PATCH /orders/$ORDER_ID/forget completed with response code %u\n",
      98             :               (unsigned int) response_code);
      99           0 :   switch (response_code)
     100             :   {
     101           0 :   case 0:
     102           0 :     hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     103           0 :     break;
     104           0 :   case MHD_HTTP_OK:
     105             :     /* fields were NOW forgotten */
     106           0 :     break;
     107           0 :   case MHD_HTTP_NO_CONTENT:
     108             :     /* fields were already forgotten before */
     109           0 :     break;
     110           0 :   case MHD_HTTP_BAD_REQUEST:
     111           0 :     hr.ec = TALER_JSON_get_error_code (json);
     112           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     113           0 :     break;
     114           0 :   case MHD_HTTP_UNAUTHORIZED:
     115           0 :     hr.ec = TALER_JSON_get_error_code (json);
     116           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     117             :     /* Nothing really to verify, merchant says we need to authenticate. */
     118           0 :     break;
     119           0 :   case MHD_HTTP_NOT_FOUND:
     120           0 :     hr.ec = TALER_JSON_get_error_code (json);
     121           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     122           0 :     break;
     123           0 :   case MHD_HTTP_CONFLICT:
     124           0 :     hr.ec = TALER_JSON_get_error_code (json);
     125           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     126           0 :     break;
     127           0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     128           0 :     hr.ec = TALER_JSON_get_error_code (json);
     129           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     130             :     /* Server had an internal issue; we should retry,
     131             :        but this API leaves this to the application */
     132           0 :     break;
     133           0 :   default:
     134           0 :     TALER_MERCHANT_parse_error_details_ (json,
     135             :                                          response_code,
     136             :                                          &hr);
     137             :     /* unexpected response code */
     138           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     139             :                 "Unexpected response code %u/%d\n",
     140             :                 (unsigned int) response_code,
     141             :                 (int) hr.ec);
     142           0 :     GNUNET_break_op (0);
     143           0 :     break;
     144             :   }
     145           0 :   ofh->cb (ofh->cb_cls,
     146             :            &hr);
     147           0 :   TALER_MERCHANT_order_forget_cancel (ofh);
     148           0 : }
     149             : 
     150             : 
     151             : struct TALER_MERCHANT_OrderForgetHandle *
     152           0 : TALER_MERCHANT_order_forget (struct GNUNET_CURL_Context *ctx,
     153             :                              const char *merchant_url,
     154             :                              const char *order_id,
     155             :                              unsigned int fields_length,
     156             :                              const char *fields[],
     157             :                              TALER_MERCHANT_ForgetCallback cb,
     158             :                              void *cb_cls)
     159             : {
     160             :   struct TALER_MERCHANT_OrderForgetHandle *ofh;
     161             :   json_t *req_fields;
     162             :   json_t *req_obj;
     163             : 
     164           0 :   req_fields = json_array ();
     165           0 :   if (NULL == req_fields)
     166             :   {
     167           0 :     GNUNET_break (0);
     168           0 :     return NULL;
     169             :   }
     170           0 :   for (unsigned int i = 0; i<fields_length; i++)
     171             :   {
     172           0 :     if (0 !=
     173           0 :         json_array_append_new (req_fields,
     174           0 :                                json_string (fields[i])))
     175             :     {
     176           0 :       GNUNET_break (0);
     177           0 :       json_decref (req_fields);
     178           0 :       return NULL;
     179             :     }
     180             :   }
     181           0 :   req_obj = GNUNET_JSON_PACK (
     182             :     GNUNET_JSON_pack_array_steal ("fields",
     183             :                                   req_fields));
     184           0 :   ofh = GNUNET_new (struct TALER_MERCHANT_OrderForgetHandle);
     185           0 :   ofh->ctx = ctx;
     186           0 :   ofh->cb = cb;
     187           0 :   ofh->cb_cls = cb_cls;
     188             :   {
     189             :     char *path;
     190             : 
     191           0 :     GNUNET_asprintf (&path,
     192             :                      "private/orders/%s/forget",
     193             :                      order_id);
     194           0 :     ofh->url = TALER_url_join (merchant_url,
     195             :                                path,
     196             :                                NULL);
     197           0 :     GNUNET_free (path);
     198             :   }
     199           0 :   if (NULL == ofh->url)
     200             :   {
     201           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     202             :                 "Could not construct request URL.\n");
     203           0 :     json_decref (req_obj);
     204           0 :     GNUNET_free (ofh);
     205           0 :     return NULL;
     206             :   }
     207             :   {
     208             :     CURL *eh;
     209             : 
     210           0 :     eh = TALER_MERCHANT_curl_easy_get_ (ofh->url);
     211           0 :     if (GNUNET_OK !=
     212           0 :         TALER_curl_easy_post (&ofh->post_ctx,
     213             :                               eh,
     214             :                               req_obj))
     215             :     {
     216           0 :       GNUNET_break (0);
     217           0 :       curl_easy_cleanup (eh);
     218           0 :       json_decref (req_obj);
     219           0 :       GNUNET_free (ofh);
     220           0 :       return NULL;
     221             :     }
     222           0 :     json_decref (req_obj);
     223           0 :     GNUNET_assert (CURLE_OK ==
     224             :                    curl_easy_setopt (eh,
     225             :                                      CURLOPT_CUSTOMREQUEST,
     226             :                                      MHD_HTTP_METHOD_PATCH));
     227           0 :     ofh->job = GNUNET_CURL_job_add2 (ctx,
     228             :                                      eh,
     229           0 :                                      ofh->post_ctx.headers,
     230             :                                      &handle_forget_finished,
     231             :                                      ofh);
     232             :   }
     233           0 :   return ofh;
     234             : }
     235             : 
     236             : 
     237             : void
     238           0 : TALER_MERCHANT_order_forget_cancel (struct
     239             :                                     TALER_MERCHANT_OrderForgetHandle *ofh)
     240             : {
     241           0 :   if (NULL != ofh->job)
     242             :   {
     243           0 :     GNUNET_CURL_job_cancel (ofh->job);
     244           0 :     ofh->job = NULL;
     245             :   }
     246           0 :   TALER_curl_easy_post_finished (&ofh->post_ctx);
     247           0 :   GNUNET_free (ofh->url);
     248           0 :   GNUNET_free (ofh);
     249           0 : }
     250             : 
     251             : 
     252             : /* end of merchant_api_patch_order_forget.c */

Generated by: LCOV version 1.14