LCOV - code coverage report
Current view: top level - lib - merchant_api_patch-private-templates-TEMPLATE_ID.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 59.2 % 103 61
Test Date: 2026-09-11 17:45:24 Functions: 80.0 % 5 4

            Line data    Source code
       1              : /*
       2              :   This file is part of TALER
       3              :   Copyright (C) 2022-2026 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 src/lib/merchant_api_patch-private-templates-TEMPLATE_ID.c
      21              :  * @brief Implementation of the PATCH /private/templates/$TEMPLATE_ID request
      22              :  * @author Christian Grothoff
      23              :  */
      24              : #include "platform.h"
      25              : #include <curl/curl.h>
      26              : #include <jansson.h>
      27              : #include <microhttpd.h> /* just for HTTP status codes */
      28              : #include <gnunet/gnunet_util_lib.h>
      29              : #include <gnunet/gnunet_curl_lib.h>
      30              : #include <taler/merchant/patch-private-templates-TEMPLATE_ID.h>
      31              : #include "merchant_api_curl_defaults.h"
      32              : #include "merchant_api_common.h"
      33              : #include <taler/taler_json_lib.h>
      34              : #include <taler/taler_curl_lib.h>
      35              : 
      36              : 
      37              : /**
      38              :  * Handle for a PATCH /private/templates/$TEMPLATE_ID operation.
      39              :  */
      40              : struct TALER_MERCHANT_PatchPrivateTemplateHandle
      41              : {
      42              :   /**
      43              :    * Base URL of the merchant backend.
      44              :    */
      45              :   char *base_url;
      46              : 
      47              :   /**
      48              :    * The full URL for this request.
      49              :    */
      50              :   char *url;
      51              : 
      52              :   /**
      53              :    * Handle for the request.
      54              :    */
      55              :   struct GNUNET_CURL_Job *job;
      56              : 
      57              :   /**
      58              :    * Function to call with the result.
      59              :    */
      60              :   TALER_MERCHANT_PatchPrivateTemplateCallback cb;
      61              : 
      62              :   /**
      63              :    * Closure for @a cb.
      64              :    */
      65              :   TALER_MERCHANT_PATCH_PRIVATE_TEMPLATE_RESULT_CLOSURE *cb_cls;
      66              : 
      67              :   /**
      68              :    * Reference to the execution context.
      69              :    */
      70              :   struct GNUNET_CURL_Context *ctx;
      71              : 
      72              :   /**
      73              :    * Minor context that holds body and headers.
      74              :    */
      75              :   struct TALER_CURL_PostContext post_ctx;
      76              : 
      77              :   /**
      78              :    * Identifier of the template to update.
      79              :    */
      80              :   char *template_id;
      81              : 
      82              :   /**
      83              :    * New human-readable description.
      84              :    */
      85              :   char *template_description;
      86              : 
      87              :   /**
      88              :    * New OTP device ID, or NULL to remove association.
      89              :    */
      90              :   char *otp_id;
      91              : 
      92              :   /**
      93              :    * New template contract (JSON).
      94              :    */
      95              :   json_t *template_contract;
      96              : 
      97              :   /**
      98              :    * Optional editable defaults (JSON object).
      99              :    */
     100              :   json_t *editable_defaults;
     101              : };
     102              : 
     103              : 
     104              : /**
     105              :  * Function called when we're done processing the
     106              :  * HTTP PATCH /private/templates/$TEMPLATE_ID request.
     107              :  *
     108              :  * @param cls the `struct TALER_MERCHANT_PatchPrivateTemplateHandle`
     109              :  * @param response_code HTTP response code, 0 on error
     110              :  * @param response response body, NULL if not in JSON
     111              :  */
     112              : static void
     113           10 : handle_patch_template_finished (void *cls,
     114              :                                 long response_code,
     115              :                                 const void *response)
     116              : {
     117           10 :   struct TALER_MERCHANT_PatchPrivateTemplateHandle *tph = cls;
     118           10 :   const json_t *json = response;
     119           10 :   struct TALER_MERCHANT_PatchPrivateTemplateResponse tpr = {
     120           10 :     .hr.http_status = (unsigned int) response_code,
     121              :     .hr.reply = json
     122              :   };
     123              : 
     124           10 :   tph->job = NULL;
     125           10 :   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
     126              :               "PATCH /private/templates/$TEMPLATE_ID completed with response code %u\n",
     127              :               (unsigned int) response_code);
     128           10 :   switch (response_code)
     129              :   {
     130            0 :   case 0:
     131            0 :     tpr.hr.ec = TALER_EC_GENERIC_INVALID_RESPONSE;
     132            0 :     break;
     133            4 :   case MHD_HTTP_NO_CONTENT:
     134            4 :     break;
     135            2 :   case MHD_HTTP_BAD_REQUEST:
     136            2 :     tpr.hr.ec = TALER_JSON_get_error_code (json);
     137            2 :     tpr.hr.hint = TALER_JSON_get_error_hint (json);
     138            2 :     GNUNET_break_op (0);
     139              :     /* This should never happen, either us
     140              :      * or the merchant is buggy (or API version conflict);
     141              :      * just pass JSON reply to the application */
     142            2 :     break;
     143            0 :   case MHD_HTTP_UNAUTHORIZED:
     144            0 :     tpr.hr.ec = TALER_JSON_get_error_code (json);
     145            0 :     tpr.hr.hint = TALER_JSON_get_error_hint (json);
     146              :     /* Nothing really to verify, merchant says we need to authenticate. */
     147            0 :     break;
     148            0 :   case MHD_HTTP_FORBIDDEN:
     149            0 :     tpr.hr.ec = TALER_JSON_get_error_code (json);
     150            0 :     tpr.hr.hint = TALER_JSON_get_error_hint (json);
     151            0 :     break;
     152            4 :   case MHD_HTTP_NOT_FOUND:
     153            4 :     tpr.hr.ec = TALER_JSON_get_error_code (json);
     154            4 :     tpr.hr.hint = TALER_JSON_get_error_hint (json);
     155            4 :     break;
     156            0 :   case MHD_HTTP_INTERNAL_SERVER_ERROR:
     157            0 :     tpr.hr.ec = TALER_JSON_get_error_code (json);
     158            0 :     tpr.hr.hint = TALER_JSON_get_error_hint (json);
     159              :     /* Server had an internal issue; we should retry,
     160              :        but this API leaves this to the application */
     161            0 :     break;
     162            0 :   default:
     163            0 :     TALER_MERCHANT_parse_error_details_ (json,
     164              :                                          response_code,
     165              :                                          &tpr.hr);
     166            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     167              :                 "Unexpected response code %u/%d\n",
     168              :                 (unsigned int) response_code,
     169              :                 (int) tpr.hr.ec);
     170            0 :     GNUNET_break_op (0);
     171            0 :     break;
     172              :   }
     173           10 :   tph->cb (tph->cb_cls,
     174              :            &tpr);
     175           10 :   TALER_MERCHANT_patch_private_template_cancel (tph);
     176           10 : }
     177              : 
     178              : 
     179              : struct TALER_MERCHANT_PatchPrivateTemplateHandle *
     180           10 : TALER_MERCHANT_patch_private_template_create (
     181              :   struct GNUNET_CURL_Context *ctx,
     182              :   const char *url,
     183              :   const char *template_id,
     184              :   const char *template_description,
     185              :   const char *otp_id,
     186              :   json_t *template_contract)
     187              : {
     188              :   struct TALER_MERCHANT_PatchPrivateTemplateHandle *tph;
     189              : 
     190           10 :   tph = GNUNET_new (struct TALER_MERCHANT_PatchPrivateTemplateHandle);
     191           10 :   tph->ctx = ctx;
     192           10 :   tph->base_url = GNUNET_strdup (url);
     193           10 :   tph->template_id = GNUNET_strdup (template_id);
     194           10 :   tph->template_description = GNUNET_strdup (template_description);
     195           10 :   if (NULL != otp_id)
     196            6 :     tph->otp_id = GNUNET_strdup (otp_id);
     197           10 :   tph->template_contract = json_incref (template_contract);
     198           10 :   return tph;
     199              : }
     200              : 
     201              : 
     202              : enum GNUNET_GenericReturnValue
     203            0 : TALER_MERCHANT_patch_private_template_set_options_ (
     204              :   struct TALER_MERCHANT_PatchPrivateTemplateHandle *tph,
     205              :   unsigned int num_options,
     206              :   const struct TALER_MERCHANT_PatchPrivateTemplateOptionValue *options)
     207              : {
     208            0 :   for (unsigned int i = 0; i < num_options; i++)
     209              :   {
     210            0 :     switch (options[i].option)
     211              :     {
     212            0 :     case TALER_MERCHANT_PATCH_PRIVATE_TEMPLATE_OPTION_END:
     213            0 :       return GNUNET_OK;
     214            0 :     case TALER_MERCHANT_PATCH_PRIVATE_TEMPLATE_OPTION_EDITABLE_DEFAULTS:
     215            0 :       json_decref (tph->editable_defaults);
     216              :       tph->editable_defaults
     217            0 :         = json_incref ((json_t *) options[i].details.editable_defaults);
     218            0 :       break;
     219            0 :     default:
     220            0 :       GNUNET_break (0);
     221            0 :       return GNUNET_SYSERR;
     222              :     }
     223              :   }
     224            0 :   return GNUNET_OK;
     225              : }
     226              : 
     227              : 
     228              : enum TALER_ErrorCode
     229           10 : TALER_MERCHANT_patch_private_template_start (
     230              :   struct TALER_MERCHANT_PatchPrivateTemplateHandle *tph,
     231              :   TALER_MERCHANT_PatchPrivateTemplateCallback cb,
     232              :   TALER_MERCHANT_PATCH_PRIVATE_TEMPLATE_RESULT_CLOSURE *cb_cls)
     233              : {
     234              :   json_t *req_obj;
     235              :   CURL *eh;
     236              : 
     237           10 :   tph->cb = cb;
     238           10 :   tph->cb_cls = cb_cls;
     239              :   {
     240              :     char *path;
     241              : 
     242           10 :     GNUNET_asprintf (&path,
     243              :                      "private/templates/%s",
     244              :                      tph->template_id);
     245           10 :     tph->url = TALER_url_join (tph->base_url,
     246              :                                path,
     247              :                                NULL);
     248           10 :     GNUNET_free (path);
     249              :   }
     250           10 :   if (NULL == tph->url)
     251            0 :     return TALER_EC_GENERIC_CONFIGURATION_INVALID;
     252           10 :   req_obj = GNUNET_JSON_PACK (
     253              :     GNUNET_JSON_pack_string ("template_description",
     254              :                              tph->template_description),
     255              :     GNUNET_JSON_pack_allow_null (
     256              :       GNUNET_JSON_pack_string ("otp_id",
     257              :                                tph->otp_id)),
     258              :     GNUNET_JSON_pack_object_incref ("template_contract",
     259              :                                     (json_t *) tph->template_contract),
     260              :     GNUNET_JSON_pack_allow_null (
     261              :       GNUNET_JSON_pack_object_incref ("editable_defaults",
     262              :                                       tph->editable_defaults)));
     263           10 :   eh = TALER_MERCHANT_curl_easy_get_ (tph->url);
     264           20 :   if ( (NULL == eh) ||
     265              :        (GNUNET_OK !=
     266           10 :         TALER_curl_easy_post (&tph->post_ctx,
     267              :                               eh,
     268              :                               req_obj)) )
     269              :   {
     270            0 :     GNUNET_break (0);
     271            0 :     json_decref (req_obj);
     272            0 :     if (NULL != eh)
     273            0 :       curl_easy_cleanup (eh);
     274            0 :     return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
     275              :   }
     276           10 :   json_decref (req_obj);
     277           10 :   GNUNET_assert (CURLE_OK ==
     278              :                  curl_easy_setopt (eh,
     279              :                                    CURLOPT_CUSTOMREQUEST,
     280              :                                    MHD_HTTP_METHOD_PATCH));
     281           20 :   tph->job = GNUNET_CURL_job_add2 (tph->ctx,
     282              :                                    eh,
     283           10 :                                    tph->post_ctx.headers,
     284              :                                    &handle_patch_template_finished,
     285              :                                    tph);
     286           10 :   if (NULL == tph->job)
     287            0 :     return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
     288           10 :   return TALER_EC_NONE;
     289              : }
     290              : 
     291              : 
     292              : void
     293           10 : TALER_MERCHANT_patch_private_template_cancel (
     294              :   struct TALER_MERCHANT_PatchPrivateTemplateHandle *tph)
     295              : {
     296           10 :   if (NULL != tph->job)
     297              :   {
     298            0 :     GNUNET_CURL_job_cancel (tph->job);
     299            0 :     tph->job = NULL;
     300              :   }
     301           10 :   TALER_curl_easy_post_finished (&tph->post_ctx);
     302           10 :   json_decref (tph->template_contract);
     303           10 :   GNUNET_free (tph->url);
     304           10 :   GNUNET_free (tph->base_url);
     305           10 :   GNUNET_free (tph->template_id);
     306           10 :   GNUNET_free (tph->template_description);
     307           10 :   GNUNET_free (tph->otp_id);
     308           10 :   json_decref (tph->editable_defaults);
     309           10 :   GNUNET_free (tph);
     310           10 : }
     311              : 
     312              : 
     313              : /* end of merchant_api_patch-private-templates-TEMPLATE_ID-new.c */
        

Generated by: LCOV version 2.0-1