-
Notifications
You must be signed in to change notification settings - Fork 572
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
672 additions
and
588 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/Stripe.net/Infrastructure/Http/FormUrlEncodedUTF8Content.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
namespace Stripe.Infrastructure.Http | ||
{ | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Text; | ||
using Stripe.Infrastructure.Extensions; | ||
|
||
public class FormUrlEncodedUTF8Content : ByteArrayContent | ||
{ | ||
public FormUrlEncodedUTF8Content(BaseOptions options) | ||
: base(GetContentByteArray(options)) | ||
{ | ||
this.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded"); | ||
this.Headers.ContentType.CharSet = "utf-8"; | ||
} | ||
|
||
private static byte[] GetContentByteArray(BaseOptions options) | ||
{ | ||
if (options == null) | ||
{ | ||
return new byte[0]; | ||
} | ||
|
||
return Encoding.UTF8.GetBytes(options.ToQueryString()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
namespace Stripe.Infrastructure.Http | ||
{ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
/// <summary> | ||
/// Abstract base class for HTTP clients used to make requests to Stripe's API. | ||
/// </summary> | ||
public abstract class HttpClient | ||
{ | ||
/// <summary>The last request made by this client.</summary> | ||
public Request LastRequest { get; protected set; } | ||
|
||
/// <summary>The last response received by this client.</summary> | ||
public Response LastResponse { get; protected set; } | ||
|
||
/// <summary>Sends a request to Stripe's API as an asynchronous operation.</summary> | ||
/// <param name="request">The parameters of the request to send.</param> | ||
/// <param name="cancellationToken">The cancellation token to cancel operation.</param> | ||
/// <returns>The task object representing the asynchronous operation.</returns> | ||
public abstract Task<Response> MakeRequestAsync( | ||
Request request, | ||
CancellationToken cancellationToken = default(CancellationToken)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
namespace Stripe.Infrastructure.Http | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Text; | ||
using Stripe.Infrastructure.Extensions; | ||
using Stripe.Infrastructure.FormEncoding; | ||
|
||
/// <summary> | ||
/// Represents a request to Stripe's API. | ||
/// </summary> | ||
public class Request | ||
{ | ||
/// <summary>Initializes a new instance of the <see cref="Request"/> class.</summary> | ||
/// <param name="method">The HTTP method.</param> | ||
/// <param name="path">The path of the request.</param> | ||
/// <param name="options">The parameters of the request.</param> | ||
/// <param name="requestOptions">The special modifiers of the request.</param> | ||
public Request( | ||
HttpMethod method, | ||
string path, | ||
BaseOptions options, | ||
RequestOptions requestOptions) | ||
{ | ||
this.Method = method; | ||
|
||
this.Uri = BuildUri(method, path, options, requestOptions); | ||
|
||
this.AuthorizationHeader = new AuthenticationHeaderValue( | ||
"Bearer", | ||
requestOptions?.ApiKey ?? StripeConfiguration.ApiKey); | ||
|
||
this.StripeHeaders = BuildStripeHeaders(requestOptions); | ||
|
||
this.Content = BuildContent(method, options); | ||
} | ||
|
||
/// <summary>The HTTP method for the request (GET, POST or DELETE).</summary> | ||
public HttpMethod Method { get; } | ||
|
||
/// <summary> | ||
/// The URL for the request. If this is a GET or DELETE request, the URL also includes | ||
/// the parameters in the query string. | ||
/// </summary> | ||
public Uri Uri { get; } | ||
|
||
/// <summary>The value of the <c>Authorization</c> header with the API key.</summary> | ||
public AuthenticationHeaderValue AuthorizationHeader { get; } | ||
|
||
/// <summary> | ||
/// Dictionary containing Stripe custom headers (<c>Stripe-Version</c>, | ||
/// <c>Stripe-Account</c>, <c>Idempotency-Key</c>...). | ||
/// </summary> | ||
public Dictionary<string, string> StripeHeaders { get; } | ||
|
||
/// <summary> | ||
/// The body of the request. For POST requests, this will be either a | ||
/// <c>application/x-www-form-urlencoded</c> or a <c>multipart/form-data</c> encoded | ||
/// payload. For non-POST requests, this will be <c>null</c>. | ||
/// </summary> | ||
public HttpContent Content { get; } | ||
|
||
private static Uri BuildUri( | ||
HttpMethod method, | ||
string path, | ||
BaseOptions options, | ||
RequestOptions requestOptions) | ||
{ | ||
var b = new StringBuilder(); | ||
|
||
b.Append(requestOptions?.BaseUrl ?? StripeConfiguration.ApiBase); | ||
b.Append(path); | ||
|
||
if (method != HttpMethod.Post) | ||
{ | ||
var queryString = options?.ToQueryString(); | ||
if (!string.IsNullOrEmpty(queryString)) | ||
{ | ||
b.Append("?"); | ||
b.Append(queryString); | ||
} | ||
} | ||
|
||
return new Uri(b.ToString()); | ||
} | ||
|
||
private static Dictionary<string, string> BuildStripeHeaders(RequestOptions requestOptions) | ||
{ | ||
var stripeHeaders = new Dictionary<string, string> | ||
{ | ||
{ "Stripe-Version", requestOptions?.StripeVersion ?? StripeConfiguration.ApiVersion }, | ||
}; | ||
|
||
if (!string.IsNullOrEmpty(requestOptions?.StripeConnectAccountId)) | ||
{ | ||
stripeHeaders.Add("Stripe-Account", requestOptions.StripeConnectAccountId); | ||
} | ||
|
||
if (!string.IsNullOrEmpty(requestOptions?.IdempotencyKey)) | ||
{ | ||
stripeHeaders.Add("Idempotency-Key", requestOptions.IdempotencyKey); | ||
} | ||
|
||
return stripeHeaders; | ||
} | ||
|
||
private static HttpContent BuildContent(HttpMethod method, BaseOptions options) | ||
{ | ||
if (method != HttpMethod.Post) | ||
{ | ||
return null; | ||
} | ||
|
||
return FormEncoder.EncodeOptionsContent(options); | ||
} | ||
} | ||
} |
Oops, something went wrong.