StripeKit 0.1.1
Stripe integration toolkit for C++
Loading...
Searching...
No Matches
HTTPClient.h
1/** HTTPClient.h
2 *
3 * Backend API used to communicate with Stripe's REST API.
4 *
5 * @author Hans de Ruiter
6 *
7 * @License See LICENSE for details.
8 */
9
10#pragma once
11
12#include "StripeKit/Async.h"
13#include "StripeKit/RequestOptions.h"
14
15#include <exception>
16#include <functional>
17#include <string>
18#include <unordered_map>
19#include <variant>
20
21namespace stripekit {
22
23/**
24 * @addtogroup adapter_interfaces
25 * @{
26 */
27
28/**
29 * HTTP method for a Stripe API request.
30 */
31enum class HttpMethod {
32 Get, /**< Fetch an object. */
33 Post, /**< Create or update an object. */
34 Delete /**< Remove an object. */
35};
36
37/**
38 * A request StripeKit wants sent to Stripe.
39 *
40 * StripeKit fills this in; the adapter turns it into a real HTTP request.
41 */
43 /** Method to use. */
45
46 /**
47 * Path relative to the Stripe API base URL, for example "/v1/customers".
48 *
49 * Join it to the base URL, normally "https://api.stripe.com".
50 */
51 std::string path;
52
53 /**
54 * Headers StripeKit needs sent.
55 *
56 * The adapter adds its own on top, notably the "Authorization" header
57 * carrying the secret key.
58 */
59 std::unordered_map<std::string, std::string> headers;
60
61 /**
62 * The request body, already encoded as "application/x-www-form-urlencoded".
63 *
64 * Send it unchanged. Stripe's API does not accept JSON request bodies.
65 */
66 std::string body;
67
68 /**
69 * Per-call settings the adapter must honour.
70 *
71 * `idempotency_key` becomes the "Idempotency-Key" header when set,
72 * `timeout` overrides the adapter's default, and `headers` are merged over
73 * the adapter's defaults. Ignoring the idempotency key means a retried call
74 * can charge a customer twice.
75 */
77};
78
79/**
80 * Stripe's answer to a request.
81 */
83 /** HTTP status Stripe returned. */
84 int status_code = 0;
85
86 /** Response headers, with lower-case keys. StripeKit looks them up that way. */
87 std::unordered_map<std::string, std::string> headers;
88
89 /** Response body, unmodified. */
90 std::string body;
91};
92
93/**
94 * Sends StripeKit's requests to Stripe.
95 *
96 * This is the outbound half of an adapter. Implement it to put StripeKit on
97 * top of an existing HTTP stack; applications normally use a ready-made
98 * adapter such as stripekit::drogon::HTTPClient instead.
99 *
100 * A rejected request is not an error here. Report a 4xx or 5xx from Stripe as
101 * a normal HttpResponse and let StripeKit parse Stripe's error document and
102 * throw StripeApiException itself. Reserve exceptions for failures that stopped
103 * the request reaching Stripe at all, such as DNS, TLS, or timeout failures.
104 *
105 * Implementations must be thread-safe and must tolerate several requests being
106 * in flight at once.
107 *
108 * @sa @ref writing_an_adapter
109 */
111public:
112 /** Completion callback for the callback form of send(). */
113 using Completion = std::function<void(std::variant<HttpResponse, std::exception_ptr>)>;
114
115 /**
116 * Destroys the HTTP client instance.
117 */
118 virtual ~HTTPClient() = default;
119
120 /**
121 * Sends a request to Stripe.
122 *
123 * @param request What to send, including the options to honour.
124 * @return A task producing Stripe's response.
125 * @throws std::exception Stripe could not be reached. A response with an
126 * error status is returned normally, not thrown.
127 */
128 virtual Task<HttpResponse> send_async(const HttpRequest& request) = 0;
129
130 /**
131 * Sends a request to Stripe, reporting the result through a callback.
132 *
133 * The callback must be invoked exactly once, on every path including the
134 * failure paths. Calling it twice corrupts whatever is waiting on it;
135 * never calling it hangs the request. OnceCallback exists to enforce this.
136 *
137 * @param request What to send, including the options to honour.
138 * @param completion Receives Stripe's response, or the transport failure.
139 */
140 virtual void send(const HttpRequest& request, Completion completion) = 0;
141};
142
143/** @} */
144
145} // namespace stripekit
Sends StripeKit's requests to Stripe.
Definition HTTPClient.h:110
virtual void send(const HttpRequest &request, Completion completion)=0
Sends a request to Stripe, reporting the result through a callback.
std::function< void(std::variant< HttpResponse, std::exception_ptr >)> Completion
Completion callback for the callback form of send().
Definition HTTPClient.h:113
virtual ~HTTPClient()=default
Destroys the HTTP client instance.
virtual Task< HttpResponse > send_async(const HttpRequest &request)=0
Sends a request to Stripe.
The result of an asynchronous StripeKit operation.
Definition Async.h:45
HttpMethod
HTTP method for a Stripe API request.
Definition HTTPClient.h:31
@ Post
Create or update an object.
Definition HTTPClient.h:33
@ Get
Fetch an object.
Definition HTTPClient.h:32
@ Delete
Remove an object.
Definition HTTPClient.h:34
Async.h.
Definition Async.h:20
A request StripeKit wants sent to Stripe.
Definition HTTPClient.h:42
HttpMethod method
Method to use.
Definition HTTPClient.h:44
std::string path
Path relative to the Stripe API base URL, for example "/v1/customers".
Definition HTTPClient.h:51
std::unordered_map< std::string, std::string > headers
Headers StripeKit needs sent.
Definition HTTPClient.h:59
RequestOptions options
Per-call settings the adapter must honour.
Definition HTTPClient.h:76
std::string body
The request body, already encoded as "application/x-www-form-urlencoded".
Definition HTTPClient.h:66
Stripe's answer to a request.
Definition HTTPClient.h:82
std::string body
Response body, unmodified.
Definition HTTPClient.h:90
int status_code
HTTP status Stripe returned.
Definition HTTPClient.h:84
std::unordered_map< std::string, std::string > headers
Response headers, with lower-case keys.
Definition HTTPClient.h:87
Per-call settings accepted by every client operation.