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: 2021-04-12 06:04:58 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 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 response 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_GENERIC_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             : struct TALER_MERCHANT_OrderForgetHandle *
     142           0 : TALER_MERCHANT_order_forget (struct GNUNET_CURL_Context *ctx,
     143             :                              const char *merchant_url,
     144             :                              const char *order_id,
     145             :                              unsigned int fields_length,
     146             :                              const char *fields[],
     147             :                              TALER_MERCHANT_ForgetCallback cb,
     148             :                              void *cb_cls)
     149             : {
     150             :   struct TALER_MERCHANT_OrderForgetHandle *ofh;
     151             :   json_t *req_fields;
     152             :   json_t *req_obj;
     153             : 
     154           0 :   req_fields = json_array ();
     155           0 :   if (NULL == req_fields)
     156             :   {
     157           0 :     GNUNET_break (0);
     158           0 :     return NULL;
     159             :   }
     160           0 :   for (unsigned int i = 0; i<fields_length; i++)
     161             :   {
     162           0 :     if (0 !=
     163           0 :         json_array_append_new (req_fields,
     164           0 :                                json_string (fields[i])))
     165             :     {
     166           0 :       GNUNET_break (0);
     167           0 :       json_decref (req_fields);
     168           0 :       return NULL;
     169             :     }
     170             :   }
     171           0 :   req_obj = json_pack ("{s:o}",
     172             :                        "fields",
     173             :                        req_fields);
     174           0 :   if (NULL == req_obj)
     175             :   {
     176           0 :     GNUNET_break (0);
     177           0 :     return NULL;
     178             :   }
     179           0 :   ofh = GNUNET_new (struct TALER_MERCHANT_OrderForgetHandle);
     180           0 :   ofh->ctx = ctx;
     181           0 :   ofh->cb = cb;
     182           0 :   ofh->cb_cls = cb_cls;
     183             :   {
     184             :     char *path;
     185             : 
     186           0 :     GNUNET_asprintf (&path,
     187             :                      "private/orders/%s/forget",
     188             :                      order_id);
     189           0 :     ofh->url = TALER_url_join (merchant_url,
     190             :                                path,
     191             :                                NULL);
     192           0 :     GNUNET_free (path);
     193             :   }
     194           0 :   if (NULL == ofh->url)
     195             :   {
     196           0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     197             :                 "Could not construct request URL.\n");
     198           0 :     json_decref (req_obj);
     199           0 :     GNUNET_free (ofh);
     200           0 :     return NULL;
     201             :   }
     202             :   {
     203             :     CURL *eh;
     204             : 
     205           0 :     eh = curl_easy_init ();
     206           0 :     if (GNUNET_OK !=
     207           0 :         TALER_curl_easy_post (&ofh->post_ctx,
     208             :                               eh,
     209             :                               req_obj))
     210             :     {
     211           0 :       GNUNET_break (0);
     212           0 :       json_decref (req_obj);
     213           0 :       GNUNET_free (ofh);
     214           0 :       return NULL;
     215             :     }
     216             : 
     217           0 :     json_decref (req_obj);
     218           0 :     GNUNET_assert (CURLE_OK == curl_easy_setopt (eh,
     219             :                                                  CURLOPT_URL,
     220             :                                                  ofh->url));
     221           0 :     GNUNET_assert (CURLE_OK ==
     222             :                    curl_easy_setopt (eh,
     223             :                                      CURLOPT_CUSTOMREQUEST,
     224             :                                      MHD_HTTP_METHOD_PATCH));
     225           0 :     ofh->job = GNUNET_CURL_job_add2 (ctx,
     226             :                                      eh,
     227           0 :                                      ofh->post_ctx.headers,
     228             :                                      &handle_forget_finished,
     229             :                                      ofh);
     230             :   }
     231           0 :   return ofh;
     232             : }
     233             : 
     234             : 
     235             : void
     236           0 : TALER_MERCHANT_order_forget_cancel (struct
     237             :                                     TALER_MERCHANT_OrderForgetHandle *ofh)
     238             : {
     239           0 :   if (NULL != ofh->job)
     240             :   {
     241           0 :     GNUNET_CURL_job_cancel (ofh->job);
     242           0 :     ofh->job = NULL;
     243             :   }
     244           0 :   TALER_curl_easy_post_finished (&ofh->post_ctx);
     245           0 :   GNUNET_free (ofh->url);
     246           0 :   GNUNET_free (ofh);
     247           0 : }
     248             : 
     249             : 
     250             : /* end of merchant_api_patch_order_forget.c */

Generated by: LCOV version 1.14