Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Stripe.net/Infrastructure/Public/LiveApiRequestor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public override async Task<StripeResponse> RawRequestAsync(
}

requestOptions = requestOptions.WithUsage(RawRequestUsage);
var apiMode = requestOptions?.ApiMode ?? ApiMode.V1;
var apiMode = ApiModeUtils.GetApiMode(path);
var uri = StripeRequest.BuildUri(
requestOptions?.BaseUrl ?? this.GetBaseUrl(BaseAddress.Api),
method,
Expand Down
6 changes: 6 additions & 0 deletions src/Stripe.net/Infrastructure/Public/StripeClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public class StripeClientOptions
/// <value>The base URL for Stripe's Meter Events API.</value>
public string MeterEventsBase { get; set; }

/// <summary>Gets and sets the Stripe-Account header value for requests made from this client.</summary>
public string StripeAccount { get; set; }

/// <summary>Gets and sets the Stripe-Context header value for requests made from this client.</summary>
public string StripeContext { get; set; }

internal StripeClientOptions Clone()
{
return (StripeClientOptions)this.MemberwiseClone();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Stripe
using System.Threading;
using System.Threading.Tasks;

public class BankAccountService : ServiceNested<BankAccount>,
public class BankAccountService : Service,
INestedCreatable<BankAccount, BankAccountCreateOptions>,
INestedDeletable<BankAccount, BankAccountDeleteOptions>,
INestedListable<BankAccount, BankAccountListOptions>,
Expand Down
2 changes: 1 addition & 1 deletion src/Stripe.net/Services/Cards/CardService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Stripe
using System.Threading;
using System.Threading.Tasks;

public class CardService : ServiceNested<Card>,
public class CardService : Service,
INestedCreatable<Card, CardCreateOptions>,
INestedDeletable<Card, CardDeleteOptions>,
INestedListable<Card, CardListOptions>,
Expand Down
2 changes: 1 addition & 1 deletion src/Stripe.net/Services/CreditNotes/CreditNoteService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Stripe
using System.Threading;
using System.Threading.Tasks;

public partial class CreditNoteService : Service<CreditNote>,
public partial class CreditNoteService : Service,
ICreatable<CreditNote, CreditNoteCreateOptions>,
IListable<CreditNote, CreditNoteListOptions>,
IRetrievable<CreditNote, CreditNoteGetOptions>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Stripe
using System.Threading;
using System.Threading.Tasks;

public partial class CreditNoteService : Service<CreditNote>,
public partial class CreditNoteService : Service,
ICreatable<CreditNote, CreditNoteCreateOptions>,
IListable<CreditNote, CreditNoteListOptions>,
IRetrievable<CreditNote, CreditNoteGetOptions>,
Expand Down
2 changes: 1 addition & 1 deletion src/Stripe.net/Services/Discounts/DiscountService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Stripe
using System.Threading;
using System.Threading.Tasks;

public class DiscountService : Service<Discount>
public class DiscountService : Service
{
public DiscountService()
: base()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Stripe
using System.Threading;
using System.Threading.Tasks;

public class EphemeralKeyService : Service<EphemeralKey>,
public class EphemeralKeyService : Service,
ICreatable<EphemeralKey, EphemeralKeyCreateOptions>,
IDeletable<EphemeralKey, EphemeralKeyDeleteOptions>
{
Expand Down
2 changes: 1 addition & 1 deletion src/Stripe.net/Services/OAuth/OAuthTokenService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Stripe
using System.Threading.Tasks;
using Stripe.Infrastructure.FormEncoding;

public class OAuthTokenService : Service<OAuthToken>,
public class OAuthTokenService : Service,
ICreatable<OAuthToken, OAuthTokenCreateOptions>
{
public OAuthTokenService()
Expand Down
2 changes: 1 addition & 1 deletion src/Stripe.net/Services/Sources/SourceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Stripe
using System.Threading;
using System.Threading.Tasks;

public partial class SourceService : Service<Source>,
public partial class SourceService : Service,
ICreatable<Source, SourceCreateOptions>,
IRetrievable<Source, SourceGetOptions>,
IUpdatable<Source, SourceUpdateOptions>,
Expand Down
2 changes: 1 addition & 1 deletion src/Stripe.net/Services/Sources/SourceService.partial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Stripe
using System.Threading;
using System.Threading.Tasks;

public partial class SourceService : Service<Source>,
public partial class SourceService : Service,
ICreatable<Source, SourceCreateOptions>,
IRetrievable<Source, SourceGetOptions>,
IUpdatable<Source, SourceUpdateOptions>,
Expand Down
8 changes: 4 additions & 4 deletions src/Stripe.net/Services/_base/Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ internal IEnumerable<T> V1ListRequestAutoPagingSync<T>(
options,
requestOptions);

options = options ?? new ListOptions();
options = ((ListOptions)options?.Clone()) ?? new ListOptions();
bool iterateBackward = false;

// Backward iterating activates if we have an `EndingBefore`
Expand Down Expand Up @@ -360,7 +360,7 @@ internal async IAsyncEnumerable<T> V1ListRequestAutoPagingAsync<T>(
requestOptions,
cancellationToken).ConfigureAwait(false);

options = options ?? new ListOptions();
options = ((ListOptions)options?.Clone()) ?? new ListOptions();
bool iterateBackward = false;

// Backward iterating activates if we have an `EndingBefore`
Expand Down Expand Up @@ -450,7 +450,7 @@ private IEnumerable<T> SearchRequestAutoPagingSync<T>(
options,
requestOptions);

options = options ?? new SearchOptions();
options = ((SearchOptions)options?.Clone()) ?? new SearchOptions();

while (true)
{
Expand Down Expand Up @@ -500,7 +500,7 @@ internal async IAsyncEnumerable<T> SearchRequestAutoPagingAsync<T>(
requestOptions,
cancellationToken).ConfigureAwait(false);

options = options ?? new SearchOptions();
options = ((SearchOptions)options?.Clone()) ?? new SearchOptions();

while (true)
{
Expand Down
3 changes: 3 additions & 0 deletions src/Stripe.net/Services/_base/ServiceNested.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace Stripe
{
using System;

[Obsolete("This class is deprecated and will be removed in a future release. Use StripeClient#RequestAsync or StripeClient#RawRequest instead.")]
public abstract class ServiceNested<TEntityReturned> : Service<TEntityReturned>
where TEntityReturned : IStripeEntity
{
Expand Down
3 changes: 3 additions & 0 deletions src/Stripe.net/Services/_base/Service{TEntityReturned}.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
namespace Stripe
{
using System;

/// <summary>Abstract base class for all services.</summary>
/// <typeparam name="TEntityReturned">
/// The type of <see cref="IStripeEntity"/> that this service returns.
/// </typeparam>
[Obsolete("This class is deprecated and will be removed in a future release. Use StripeClient#RequestAsync or StripeClient#RawRequest instead.")]
public abstract class Service<TEntityReturned> : Service
where TEntityReturned : IStripeEntity
{
Expand Down
3 changes: 0 additions & 3 deletions src/Stripe.net/Services/_common/RawRequestOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ namespace Stripe

public class RawRequestOptions : RequestOptions
{
/// <summary>Gets or sets the API mode to use for the request.</summary>
public ApiMode ApiMode { get; set; }

/// <summary>Gets or sets additional headers for the request.</summary>
public Dictionary<string, string> AdditionalHeaders { get; set; } = new Dictionary<string, string>();

Expand Down
10 changes: 10 additions & 0 deletions src/Stripe.net/Services/_common/RequestOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ internal static T WithClientOptions<T>(this T requestOptions, StripeClientOption
clone.ApiKey = clientOptions.ApiKey;
}

if (string.IsNullOrEmpty(clone.StripeContext))
{
clone.StripeContext = clientOptions.StripeContext;
}

if (string.IsNullOrEmpty(clone.StripeAccount))
{
clone.StripeAccount = clientOptions.StripeAccount;
}

return clone;
}

Expand Down
52 changes: 50 additions & 2 deletions src/StripeTests/Infrastructure/Public/LiveApiRequestorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -476,15 +476,14 @@ public async Task RawRequestAsync_Json()

var rawResponse = await this.apiRequestor.RawRequestAsync(
HttpMethod.Post,
"/v1/charges",
"/v2/charges",
"{\"foo\":\"bar\"}",
new RawRequestOptions
{
AdditionalHeaders =
{
{ "foo", "bar" },
},
ApiMode = ApiMode.V2,
});

var lastRequest = this.httpClient.LastRequest;
Expand Down Expand Up @@ -642,6 +641,55 @@ public void Deserialize()
Assert.Equal(typeof(Foo), deserialized.GetType());
}

[Fact]
public async Task StripeContextHeaderSet()
{
var apiRequestorWithContext = new LiveApiRequestor(new StripeClientOptions
{
ApiKey = "sk_test_123",
HttpClient = this.httpClient,
StripeContext = "ctx_1234",
});
var response = new StripeResponse(HttpStatusCode.OK, null, "{}");
this.httpClient.Response = response;

var service = new CustomerService(apiRequestorWithContext);

await service.CreateAsync(new CustomerCreateOptions() { });
var lastRequest = this.httpClient.LastRequest;
Assert.Equal("ctx_1234", lastRequest.StripeHeaders["Stripe-Context"]);

// If its set in the request options, that takes precendence
await service.CreateAsync(new CustomerCreateOptions() { }, new RequestOptions { StripeContext = "ctx_2345" });
lastRequest = this.httpClient.LastRequest;
Assert.Equal("ctx_2345", lastRequest.StripeHeaders["Stripe-Context"]);
}

[Fact]
public async Task StripeAccountHeaderSet()
{
var apiRequestorWithContext = new LiveApiRequestor(new StripeClientOptions
{
ApiKey = "sk_test_123",
HttpClient = this.httpClient,
StripeAccount = "acct_1234",
});

var response = new StripeResponse(HttpStatusCode.OK, null, "{}");
this.httpClient.Response = response;

var service = new CustomerService(apiRequestorWithContext);

await service.CreateAsync(new CustomerCreateOptions() { });
var lastRequest = this.httpClient.LastRequest;
Assert.Equal("acct_1234", lastRequest.StripeHeaders["Stripe-Account"]);

// If its set in the request options, that takes precendence
await service.CreateAsync(new CustomerCreateOptions() { }, new RequestOptions { StripeAccount = "acct_2345" });
lastRequest = this.httpClient.LastRequest;
Assert.Equal("acct_2345", lastRequest.StripeHeaders["Stripe-Account"]);
}

private class Foo : StripeEntity<Foo>
{
[JsonProperty("bar")]
Expand Down
3 changes: 2 additions & 1 deletion src/StripeTests/Infrastructure/Public/StripeClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public void Ctr_StripeClientOptions()
MeterEventsBase = "localhost:6502",
FilesBase = "localhost:555",
HttpClient = new TestHttpClient(),
StripeAccount = "acct",
StripeContext = "ctx",
};

// This test is designed to ensure StripeClient properly consumes all options. If any are
Expand Down Expand Up @@ -157,7 +159,6 @@ public async Task RawRequestAsync_Form()
{
{ "foo", "bar" },
},
ApiMode = ApiMode.V1,
});

this.MockHttpClientFixture.MockHandler.Protected()
Expand Down
6 changes: 6 additions & 0 deletions src/StripeTests/Services/AutoPagingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ public void ListAutoPaging_StartingAfter()
Assert.Equal("pm_126", models[1].Id);
Assert.Equal("pm_127", models[2].Id);

// Should not mutate its argument
Assert.Equal("pm_124", options.StartingAfter);

// Check invocations
this.MockHttpClientFixture.MockHandler.Protected()
.Verify(
Expand Down Expand Up @@ -247,6 +250,9 @@ public void ListAutoPaging_EndingBefore()
};
var models = service.ListAutoPaging(options).ToList();

// Should not mutate its argument
Assert.Equal("pm_127", options.EndingBefore);

// Check results
Assert.Equal(5, models.Count);
Assert.Equal("pm_126", models[0].Id);
Expand Down
10 changes: 8 additions & 2 deletions src/StripeTests/Services/_base/ServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,17 @@ public void SearchAuto_ReturnsAllPages()
"?page=page2&query=my+query");

var service = new TestService(this.StripeClient);
var options = new SearchOptions() { Query = "my query" };

HashSet<string> ids = new HashSet<string>();
foreach (var testEntity in service.SearchAutoPaging(new SearchOptions() { Query = "my query" }))
foreach (var testEntity in service.SearchAutoPaging(options))
{
Assert.NotNull(testEntity);
ids.Add(testEntity.Id);
}

Assert.Null(options.Page);

Assert.Equal(4, ids.Count);
Assert.Equal(4, ids.Count);
}
Expand All @@ -91,13 +94,16 @@ public async Task SearchAutoAsync_ReturnsAllPages()

var service = new TestService(this.StripeClient);

var options = new SearchOptions() { Query = "my query" };
HashSet<string> ids = new HashSet<string>();
await foreach (var testEntity in service.SearchAutoPagingAsync(new SearchOptions() { Query = "my query" }))
await foreach (var testEntity in service.SearchAutoPagingAsync(options))
{
Assert.NotNull(testEntity);
ids.Add(testEntity.Id);
}

Assert.Null(options.Page);

Assert.Equal(4, ids.Count);
}

Expand Down
Loading
Loading