Skip to content

Commit

Permalink
Replace ExternalAccount object by IExternalAccount interface
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe committed Oct 2, 2018
1 parent 3b71dec commit 1e48083
Show file tree
Hide file tree
Showing 32 changed files with 106 additions and 114 deletions.
2 changes: 1 addition & 1 deletion src/Stripe.net/Entities/Accounts/Account.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ internal object InternalBusinessLogo
public string Email { get; set; }

[JsonProperty("external_accounts")]
public StripeList<ExternalAccount> ExternalAccounts { get; set; }
public StripeList<IExternalAccount> ExternalAccounts { get; set; }

[JsonProperty("keys")]
public CustomAccountKeys Keys { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion src/Stripe.net/Entities/BankAccounts/BankAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Stripe
using Newtonsoft.Json;
using Stripe.Infrastructure;

public class BankAccount : StripeEntityWithId, ISupportMetadata
public class BankAccount : StripeEntityWithId, ISupportMetadata, IExternalAccount
{
[JsonProperty("object")]
public string Object { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion src/Stripe.net/Entities/Cards/Card.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Stripe
using Newtonsoft.Json;
using Stripe.Infrastructure;

public class Card : StripeEntityWithId, ISupportMetadata
public class Card : StripeEntityWithId, ISupportMetadata, IExternalAccount
{
[JsonProperty("object")]
public string Object { get; set; }
Expand Down
21 changes: 0 additions & 21 deletions src/Stripe.net/Entities/ExternalAccounts/ExternalAccount.cs

This file was deleted.

4 changes: 2 additions & 2 deletions src/Stripe.net/Entities/Payouts/Payout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ internal object InternalBalanceTransaction
public string DestinationId { get; set; }

[JsonIgnore]
public ExternalAccount Destination { get; set; }
public IExternalAccount Destination { get; set; }

[JsonProperty("destination")]
internal object InternalDestination
{
set
{
StringOrObject<ExternalAccount>.Map(value, s => this.DestinationId = s, o => this.Destination = o);
StringOrObject<IExternalAccount>.Map(value, s => this.DestinationId = s, o => this.Destination = o);
}
}
#endregion
Expand Down
2 changes: 1 addition & 1 deletion src/Stripe.net/Entities/_base/StripeEntity.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Stripe
{
public abstract class StripeEntity
public abstract class StripeEntity : IStripeEntity
{
public StripeResponse StripeResponse { get; set; }
}
Expand Down
2 changes: 1 addition & 1 deletion src/Stripe.net/Entities/_base/StripeEntityWithId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Stripe
{
using Newtonsoft.Json;

public abstract class StripeEntityWithId : StripeEntity
public abstract class StripeEntityWithId : StripeEntity, IStripeEntityWithId
{
[JsonProperty("id")]
public string Id { get; set; }
Expand Down
6 changes: 6 additions & 0 deletions src/Stripe.net/Entities/_interfaces/IExternalAccount.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Stripe
{
public interface IExternalAccount : IStripeEntityWithId
{
}
}
7 changes: 7 additions & 0 deletions src/Stripe.net/Entities/_interfaces/IStripeEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Stripe
{
public interface IStripeEntity
{
StripeResponse StripeResponse { get; set; }
}
}
7 changes: 7 additions & 0 deletions src/Stripe.net/Entities/_interfaces/IStripeEntityWithId.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Stripe
{
public interface IStripeEntityWithId : IStripeEntity
{
string Id { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ namespace Stripe.Infrastructure

internal class ExternalAccountConverter : JsonConverter
{
public override bool CanWrite => false;

public override bool CanConvert(Type objectType)
{
throw new NotImplementedException();
return objectType == typeof(IExternalAccount);
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
Expand All @@ -24,21 +22,17 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
{
var incoming = JObject.Load(reader);

var externalAccount = new ExternalAccount
{
Id = incoming.SelectToken("id").ToString()
};
var externalAccount = default(IExternalAccount);

if (incoming.SelectToken("object")?.ToString() == "bank_account")
var objectName = incoming.SelectToken("object")?.ToString();

if (objectName == "bank_account")
{
externalAccount.Type = ExternalAccountType.BankAccount;
externalAccount.BankAccount = Mapper<BankAccount>.MapFromJson(incoming.ToString());
externalAccount = Mapper<BankAccount>.MapFromJson(incoming.ToString());
}

if (incoming.SelectToken("object")?.ToString() == "card")
else if (objectName == "card")
{
externalAccount.Type = ExternalAccountType.Card;
externalAccount.Card = Mapper<Card>.MapFromJson(incoming.ToString());
externalAccount = Mapper<Card>.MapFromJson(incoming.ToString());
}

return externalAccount;
Expand Down
2 changes: 1 addition & 1 deletion src/Stripe.net/Infrastructure/ParameterBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Stripe.Infrastructure
internal static class ParameterBuilder
{
public static string ApplyAllParameters<T>(this Service<T> service, BaseOptions obj, string url, bool isListMethod = false)
where T : StripeEntity
where T : IStripeEntity
{
// store the original url from the service call into requestString (e.g. https://api.stripe.com/v1/accounts/account_id)
// before the stripe attributes get applied. all of the attributes that will get passed to stripe will be applied to this string,
Expand Down
7 changes: 6 additions & 1 deletion src/Stripe.net/Infrastructure/Public/Mapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ namespace Stripe
using System.Reflection;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Stripe.Infrastructure;

public static class Mapper<T>
{
private static JsonConverter[] converters = {
new ExternalAccountConverter(),
};

public static List<T> MapCollectionFromJson(string json, string token = "data", StripeResponse stripeResponse = null)
{
var jObject = JObject.Parse(json);
Expand All @@ -29,7 +34,7 @@ public static T MapFromJson(string json, string parentToken = null, StripeRespon
{
var jsonToParse = string.IsNullOrEmpty(parentToken) ? json : JObject.Parse(json).SelectToken(parentToken).ToString();

var result = JsonConvert.DeserializeObject<T>(jsonToParse);
var result = JsonConvert.DeserializeObject<T>(jsonToParse, converters);

// if necessary, we might need to apply the stripe response to nested properties for StripeList<T>
ApplyStripeResponse(json, stripeResponse, result);
Expand Down
7 changes: 4 additions & 3 deletions src/Stripe.net/Infrastructure/StringOrObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@ namespace Stripe.Infrastructure
using Newtonsoft.Json.Linq;

internal static class StringOrObject<T>
where T : StripeEntityWithId
where T : IStripeEntityWithId
{
public static void Map(object value, Action<string> updateId, Action<T> updateObject)
{
if (value is JObject)
{
T item = ((JToken)value).ToObject<T>();
// T item = ((JToken)value).ToObject<T>();
T item = Mapper<T>.MapFromJson(value.ToString());
updateId(item.Id);
updateObject(item);
}
else if (value is string)
{
updateId((string)value);
updateObject(null);
updateObject(default(T));
}
}
}
Expand Down
32 changes: 16 additions & 16 deletions src/Stripe.net/Services/ExternalAccounts/ExternalAccountService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ namespace Stripe
using System.Threading.Tasks;
using Stripe.Infrastructure;

public class ExternalAccountService : ServiceNested<ExternalAccount>,
INestedCreatable<ExternalAccount, ExternalAccountCreateOptions>,
INestedDeletable<ExternalAccount>,
INestedListable<ExternalAccount, ExternalAccountListOptions>,
INestedRetrievable<ExternalAccount>,
INestedUpdatable<ExternalAccount, ExternalAccountUpdateOptions>
public class ExternalAccountService : ServiceNested<IExternalAccount>,
INestedCreatable<IExternalAccount, ExternalAccountCreateOptions>,
INestedDeletable<IExternalAccount>,
INestedListable<IExternalAccount, ExternalAccountListOptions>,
INestedRetrievable<IExternalAccount>,
INestedUpdatable<IExternalAccount, ExternalAccountUpdateOptions>
{
public ExternalAccountService()
: base(null)
Expand All @@ -24,52 +24,52 @@ public ExternalAccountService(string apiKey)

public override string BasePath => "/accounts/{PARENT_ID}/external_accounts";

public virtual ExternalAccount Create(string accountId, ExternalAccountCreateOptions options, RequestOptions requestOptions = null)
public virtual IExternalAccount Create(string accountId, ExternalAccountCreateOptions options, RequestOptions requestOptions = null)
{
return this.CreateNestedEntity(accountId, options, requestOptions);
}

public virtual Task<ExternalAccount> CreateAsync(string accountId, ExternalAccountCreateOptions options, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken))
public virtual Task<IExternalAccount> CreateAsync(string accountId, ExternalAccountCreateOptions options, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken))
{
return this.CreateNestedEntityAsync(accountId, options, requestOptions, cancellationToken);
}

public virtual ExternalAccount Delete(string accountId, string externalAccountId, RequestOptions requestOptions = null)
public virtual IExternalAccount Delete(string accountId, string externalAccountId, RequestOptions requestOptions = null)
{
return this.DeleteNestedEntity(accountId, externalAccountId, null, requestOptions);
}

public virtual Task<ExternalAccount> DeleteAsync(string accountId, string externalAccountId, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken))
public virtual Task<IExternalAccount> DeleteAsync(string accountId, string externalAccountId, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken))
{
return this.DeleteNestedEntityAsync(accountId, externalAccountId, null, requestOptions, cancellationToken);
}

public virtual ExternalAccount Get(string accountId, string externalAccountId, RequestOptions requestOptions = null)
public virtual IExternalAccount Get(string accountId, string externalAccountId, RequestOptions requestOptions = null)
{
return this.GetNestedEntity(accountId, externalAccountId, null, requestOptions);
}

public virtual Task<ExternalAccount> GetAsync(string accountId, string externalAccountId, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken))
public virtual Task<IExternalAccount> GetAsync(string accountId, string externalAccountId, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken))
{
return this.GetNestedEntityAsync(accountId, externalAccountId, null, requestOptions, cancellationToken);
}

public virtual StripeList<ExternalAccount> List(string accountId, ExternalAccountListOptions options = null, RequestOptions requestOptions = null)
public virtual StripeList<IExternalAccount> List(string accountId, ExternalAccountListOptions options = null, RequestOptions requestOptions = null)
{
return this.ListNestedEntities(accountId, options, requestOptions);
}

public virtual Task<StripeList<ExternalAccount>> ListAsync(string accountId, ExternalAccountListOptions options = null, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken))
public virtual Task<StripeList<IExternalAccount>> ListAsync(string accountId, ExternalAccountListOptions options = null, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken))
{
return this.ListNestedEntitiesAsync(accountId, options, requestOptions, cancellationToken);
}

public virtual ExternalAccount Update(string accountId, string externalAccountId, ExternalAccountUpdateOptions options, RequestOptions requestOptions = null)
public virtual IExternalAccount Update(string accountId, string externalAccountId, ExternalAccountUpdateOptions options, RequestOptions requestOptions = null)
{
return this.UpdateNestedEntity(accountId, externalAccountId, options, requestOptions);
}

public virtual Task<ExternalAccount> UpdateAsync(string accountId, string externalAccountId, ExternalAccountUpdateOptions options, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken))
public virtual Task<IExternalAccount> UpdateAsync(string accountId, string externalAccountId, ExternalAccountUpdateOptions options, RequestOptions requestOptions = null, CancellationToken cancellationToken = default(CancellationToken))
{
return this.UpdateNestedEntityAsync(accountId, externalAccountId, options, requestOptions, cancellationToken);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Stripe.net/Services/Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Stripe
using Stripe.Infrastructure;

public abstract class Service<EntityReturned>
where EntityReturned : StripeEntity
where EntityReturned : IStripeEntity
{
protected Service()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Stripe.net/Services/ServiceNested.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Stripe
using Stripe.Infrastructure;

public abstract class ServiceNested<EntityReturned> : Service<EntityReturned>
where EntityReturned : StripeEntity
where EntityReturned : IStripeEntity
{
protected ServiceNested()
: base(null)
Expand Down
2 changes: 1 addition & 1 deletion src/Stripe.net/Services/_interfaces/ICreatable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Stripe
using System.Threading.Tasks;

public interface ICreatable<T, O>
where T : StripeEntity
where T : IStripeEntity
where O : BaseOptions
{
T Create(O createOptions, RequestOptions requestOptions = null);
Expand Down
2 changes: 1 addition & 1 deletion src/Stripe.net/Services/_interfaces/IDeletable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Stripe
using System.Threading.Tasks;

public interface IDeletable<T>
where T : StripeEntity
where T : IStripeEntity
{
T Delete(string id, RequestOptions requestOptions = null);

Expand Down
2 changes: 1 addition & 1 deletion src/Stripe.net/Services/_interfaces/IListable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Stripe
using System.Threading.Tasks;

public interface IListable<T, O>
where T : StripeEntity
where T : IStripeEntity
where O : ListOptions
{
StripeList<T> List(O listOptions = null, RequestOptions requestOptions = null);
Expand Down
2 changes: 1 addition & 1 deletion src/Stripe.net/Services/_interfaces/INestedCreatable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Stripe
using System.Threading.Tasks;

public interface INestedCreatable<T, O>
where T : StripeEntity
where T : IStripeEntity
where O : BaseOptions
{
T Create(string parentId, O createOptions, RequestOptions requestOptions = null);
Expand Down
2 changes: 1 addition & 1 deletion src/Stripe.net/Services/_interfaces/INestedDeletable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Stripe
using System.Threading.Tasks;

public interface INestedDeletable<T>
where T : StripeEntity
where T : IStripeEntity
{
T Delete(string parentId, string id, RequestOptions requestOptions = null);

Expand Down
2 changes: 1 addition & 1 deletion src/Stripe.net/Services/_interfaces/INestedListable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Stripe
using System.Threading.Tasks;

public interface INestedListable<T, O>
where T : StripeEntity
where T : IStripeEntity
where O : ListOptions
{
StripeList<T> List(string parentId, O listOptions = null, RequestOptions requestOptions = null);
Expand Down
2 changes: 1 addition & 1 deletion src/Stripe.net/Services/_interfaces/INestedRetrievable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Stripe
using System.Threading.Tasks;

public interface INestedRetrievable<T>
where T : StripeEntity
where T : IStripeEntity
{
T Get(string parentId, string id, RequestOptions requestOptions = null);

Expand Down
2 changes: 1 addition & 1 deletion src/Stripe.net/Services/_interfaces/INestedUpdatable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Stripe
using System.Threading.Tasks;

public interface INestedUpdatable<T, O>
where T : StripeEntity
where T : IStripeEntity
where O : BaseOptions
{
T Update(string parentId, string id, O updateOptions, RequestOptions requestOptions = null);
Expand Down
2 changes: 1 addition & 1 deletion src/Stripe.net/Services/_interfaces/IRetrievable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Stripe
using System.Threading.Tasks;

public interface IRetrievable<T>
where T : StripeEntity
where T : IStripeEntity
{
T Get(string id, RequestOptions requestOptions = null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Stripe
using System.Threading.Tasks;

public interface ISingletonRetrievable<T>
where T : StripeEntity
where T : IStripeEntity
{
T Get(RequestOptions requestOptions = null);

Expand Down
Loading

0 comments on commit 1e48083

Please sign in to comment.