LCOV - code coverage report
Current view: top level - lib - merchant_api_patch_order_forget.c (source / functions) Hit Total Coverage
Test: GNU Taler coverage report Lines: 0 86 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) 2020 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 lib/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 <taler/taler_json_lib.h>
      32             : #include <taler/taler_curl_lib.h>
      33             : 
      34             : 
      35             : /**
      36             :  * Handle for a PATCH /orders/$ORDER_ID/forget operation.
      37             :  */
      38             : struct TALER_MERCHANT_OrderForgetHandle
      39             : {
      40             : 
      41             :   /**
      42             :    * The url for this request.
      43             :    */
      44             :   char *url;
      45             : 
      46             :   /**
      47             :    * Handle for the request.
      48             :    */
      49             :   struct GNUNET_CURL_Job *job;
      50             : 
      51             :   /**
      52             :    * Function to call with the result.
      53             :    */
      54             :   TALER_MERCHANT_ForgetCallback cb;
      55             : 
      56             :   /**
      57             :    * Closure for @a cb.
      58             :    */
      59             :   void *cb_cls;
      60             : 
      61             :   /**
      62             :    * Reference to the execution context.
      63             :    */
      64             :   struct GNUNET_CURL_Context *ctx;
      65             : 
      66             :   /**
      67             :    * Minor context that holds body and headers.
      68             :    */
      69             :   struct TALER_CURL_PostContext post_ctx;
      70             : 
      71             : };
      72             : 
      73             : 
      74             : /**
      75             :  * Function called when we're done processing the
      76             :  * HTTP PATCH /orders/$ORDER_ID/forget request.
      77             :  *
      78             :  * @param cls the `struct TALER_MERCHANT_OrderForgetHandle`
      79             :  * @param response_code HTTP response code, 0 on error
      80             :  * @param json response body, NULL if not in JSON
      81             :  */
      82             : static void
      83           0 : handle_forget_finished (void *cls,
      84             :                         long response_code,
      85             :                         const void *response)
      86             : {
      87           0 :   struct TALER_MERCHANT_OrderForgetHandle *ofh = cls;
      88           0 :   const json_t *json = response;
      89           0 :   struct TALER_MERCHANT_HttpResponse hr = {
      90           0 :     .http_status = (unsigned int) response_code,
      91             :     .reply = json
      92             :   };
      93             : 
      94           0 :   ofh->job = NULL;
      95           0 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
      96             :               "PATCH /orders/$ORDER_ID/forget completed with response code %u\n",
      97             :               (unsigned int) response_code);
      98           0 :   switch (response_code)
      99             :   {
     100           0 :   case 0:
     101           0 :     hr.ec = TALER_EC_INVALID_RESPONSE;
     102           0 :     break;
     103           0 :   case MHD_HTTP_OK:
     104           0 :     break;
     105           0 :   case MHD_HTTP_BAD_REQUEST:
     106           0 :     hr.ec = TALER_JSON_get_error_code (json);
     107           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     108           0 :     break;
     109           0 :   case MHD_HTTP_NOT_FOUND:
     110           0 :     hr.ec = TALER_JSON_get_error_code (json);
     111           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     112           0 :     break;
     113           0 :   case MHD_HTTP_CONFLICT:
     114           0 :     hr.ec = TALER_JSON_get_error_code (json);
     115           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     116           0 :     break;
     117           0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     118           0 :     hr.ec = TALER_JSON_get_error_code (json);
     119           0 :     hr.hint = TALER_JSON_get_error_hint (json);
     120             :     /* Server had an internal issue; we should retry,
     121             :        but this API leaves this to the application */
     122           0 :     break;
     123           0 :   default:
     124           0 :     TALER_MERCHANT_parse_error_details_ (json,
     125             :                                          response_code,
     126             :                                          &hr);
     127             :     /* unexpected response code */
     128           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     129             :                 "Unexpected response code %u/%d\n",
     130             :                 (unsigned int) response_code,
     131             :                 (int) hr.ec);
     132           0 :     GNUNET_break_op (0);
     133           0 :     break;
     134             :   }
     135           0 :   ofh->cb (ofh->cb_cls,
     136             :            &hr);
     137           0 :   TALER_MERCHANT_order_forget_cancel (ofh);
     138           0 : }
     139             : 
     140             : 
     141             : /**
     142             :  * Run a payment forget operation, which forgets certain fields in an order's
     143             :  * contract terms without changing the hash of the contract terms.
     144             :  *
     145             :  * @param ctx execution context.
     146             :  * @param merchant_url base URL of the merchant.
     147             :  * @param order_id order to forget.
     148             :  * @param fields_length length of @e fields.
     149             :  * @param fields the fields in the contract terms to forget.
     150             :  * @param cb the callback to call when a reply for this request is available.
     151             :  * @param cb_cls closure for @a cb.
     152             :  * @return a handle for this request.
     153             :  */
     154             : struct TALER_MERCHANT_OrderForgetHandle *
     155           0 : TALER_MERCHANT_order_forget (struct GNUNET_CURL_Context *ctx,
     156             :                              const char *merchant_url,
     157             :                              const char *order_id,
     158             :                              unsigned int fields_length,
     159             :                              const char *fields[],
     160             :                              TALER_MERCHANT_ForgetCallback cb,
     161             :                              void *cb_cls)
     162             : {
     163             :   struct TALER_MERCHANT_OrderForgetHandle *ofh;
     164             :   json_t *req_fields;
     165             :   json_t *req_obj;
     166             : 
     167           0 :   req_fields = json_array ();
     168           0 :   if (NULL == req_fields)
     169             :   {
     170           0 :     GNUNET_break (0);
     171           0 :     return NULL;
     172             :   }
     173           0 :   for (unsigned int i = 0; i<fields_length; i++)
     174             :   {
     175           0 :     if (0 !=
     176           0 :         json_array_append_new (req_fields,
     177           0 :                                json_string (fields[i])))
     178             :     {
     179           0 :       GNUNET_break (0);
     180           0 :       json_decref (req_fields);
     181           0 :       return NULL;
     182             :     }
     183             :   }
     184           0 :   req_obj = json_pack ("{s:o}",
     185             :                        "fields",
     186             :                        req_fields);
     187           0 :   if (NULL == req_obj)
     188             :   {
     189           0 :     GNUNET_break (0);
     190           0 :     return NULL;
     191             :   }
     192           0 :   ofh = GNUNET_new (struct TALER_MERCHANT_OrderForgetHandle);
     193           0 :   ofh->ctx = ctx;
     194           0 :   ofh->cb = cb;
     195           0 :   ofh->cb_cls = cb_cls;
     196             :   {
     197             :     char *path;
     198             : 
     199           0 :     GNUNET_asprintf (&path,
     200             :                      "private/orders/%s/forget",
     201             :                      order_id);
     202           0 :     ofh->url = TALER_url_join (merchant_url,
     203             :                                path,
     204             :                                NULL);
     205           0 :     GNUNET_free (path);
     206             :   }
     207           0 :   if (NULL == ofh->url)
     208             :   {
     209           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     210             :                 "Could not construct request URL.\n");
     211           0 :     json_decref (req_obj);
     212           0 :     GNUNET_free (ofh);
     213           0 :     return NULL;
     214             :   }
     215             :   {
     216             :     CURL *eh;
     217             : 
     218           0 :     eh = curl_easy_init ();
     219           0 :     if (GNUNET_OK !=
     220           0 :         TALER_curl_easy_post (&ofh->post_ctx,
     221             :                               eh,
     222             :                               req_obj))
     223             :     {
     224           0 :       GNUNET_break (0);
     225           0 :       json_decref (req_obj);
     226           0 :       GNUNET_free (ofh);
     227           0 :       return NULL;
     228             :     }
     229             : 
     230           0 :     json_decref (req_obj);
     231           0 :     GNUNET_assert (CURLE_OK == curl_easy_setopt (eh,
     232             :                                                  CURLOPT_URL,
     233             :                                                  ofh->url));
     234           0 :     GNUNET_assert (CURLE_OK ==
     235             :                    curl_easy_setopt (eh,
     236             :                                      CURLOPT_CUSTOMREQUEST,
     237             :                                      MHD_HTTP_METHOD_PATCH));
     238           0 :     ofh->job = GNUNET_CURL_job_add2 (ctx,
     239             :                                      eh,
     240           0 :                                      ofh->post_ctx.headers,
     241             :                                      &handle_forget_finished,
     242             :                                      ofh);
     243             :   }
     244           0 :   return ofh;
     245             : }
     246             : 
     247             : 
     248             : /**
     249             :  * Cancel a PATCH /orders/$ID/forget request.  Note that if you cancel a request
     250             :  * like this, you have no assurance that the request has not yet been
     251             :  * forwarded to the merchant.
     252             :  *
     253             :  * @param ofh the forget request handle
     254             :  */
     255             : void
     256           0 : TALER_MERCHANT_order_forget_cancel (struct
     257             :                                     TALER_MERCHANT_OrderForgetHandle *ofh)
     258             : {
     259           0 :   if (NULL != ofh->job)
     260             :   {
     261           0 :     GNUNET_CURL_job_cancel (ofh->job);
     262           0 :     ofh->job = NULL;
     263             :   }
     264           0 :   TALER_curl_easy_post_finished (&ofh->post_ctx);
     265           0 :   GNUNET_free (ofh->url);
     266           0 :   GNUNET_free (ofh);
     267           0 : }
     268             : 
     269             : 
     270             : /* end of merchant_api_patch_order_forget.c */

Generated by: LCOV version 1.14