Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve expanding responses #43

Draft
wants to merge 1 commit into
base: smf
Choose a base branch
from
Draft
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: 2 additions & 0 deletions lib/messages.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:stripe/src/messages/converters.dart';
import 'package:stripe/src/messages/enums.dart';
import 'package:stripe/src/resources/payment_intent.dart';
import 'package:stripe/src/utils/expandable.dart';

export 'package:stripe/src/messages/enums.dart';

Expand Down
43 changes: 29 additions & 14 deletions lib/messages.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions lib/src/expanded.dart

This file was deleted.

36 changes: 0 additions & 36 deletions lib/src/expanded/invoice_expanded.dart

This file was deleted.

67 changes: 0 additions & 67 deletions lib/src/expanded/subscription_expanded.dart

This file was deleted.

59 changes: 59 additions & 0 deletions lib/src/messages/converters.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:stripe/src/utils/expandable.dart';

/// Converts a [DateTime] to a timestamp int and vice versa.
class TimestampConverter implements JsonConverter<DateTime, int> {
Expand All @@ -11,3 +12,61 @@ class TimestampConverter implements JsonConverter<DateTime, int> {
@override
int toJson(DateTime type) => type.millisecondsSinceEpoch ~/ 1000;
}

class ExpandableJsonConverter<T>
implements JsonConverter<Expandable<T>, Object> {
final T Function(Map<String, dynamic> json) expandedFromJson;

const ExpandableJsonConverter(this.expandedFromJson);

@override
Expandable<T> fromJson(Object json) {
if (json is String) {
return Expandable(id: json);
}

if (json is Map<String, dynamic>) {
return Expandable(
id: json['id'] as String,
expanded: expandedFromJson(json),
);
}

throw ArgumentError('expected an String or a Map<String, dynamic>');
}

@override
Object toJson(Expandable<T> object) {
return object.id;
}
}

class ExpandableListJsonConverter<T>
implements JsonConverter<ExpandableList<T>, List<dynamic>> {
final T Function(Map<String, dynamic> json) expandedFromJson;

const ExpandableListJsonConverter(this.expandedFromJson);

@override
ExpandableList<T> fromJson(List<dynamic> json) {
if (json.isEmpty) {
return ExpandableList(ids: [], expanded: []);
}

if (json.first is String) {
return ExpandableList(ids: json.cast<String>());
}

// Assuming that json is List<Map<String, dynamic>>.
final jsonList = json.cast<Map<String, dynamic>>();
final idsList = jsonList.map((element) => element['id'] as String).toList();
final parsedList = jsonList.map(expandedFromJson).toList();

return ExpandableList(ids: idsList, expanded: parsedList);
}

@override
List<dynamic> toJson(ExpandableList<T> object) {
return object.ids;
}
}
10 changes: 10 additions & 0 deletions lib/src/messages/discount.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,13 @@ class Discount extends Message {
@override
Map<String, dynamic> toJson() => _$DiscountToJson(this);
}

class ExpandableDiscountJsonConverter
extends ExpandableJsonConverter<Discount> {
const ExpandableDiscountJsonConverter() : super(Discount.fromJson);
}

class ExpandableDiscountListJsonConverter
extends ExpandableListJsonConverter<Discount> {
const ExpandableDiscountListJsonConverter() : super(Discount.fromJson);
}
8 changes: 3 additions & 5 deletions lib/src/messages/enums.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
export 'enums/expandable_fields/invoice_expandable_field.dart';
export 'enums/expandable_fields/subscription_expandable_field.dart';
export 'enums/pause_collection_behavior.dart';
export 'enums/payment_behavior.dart';
export 'enums/proration_behavior.dart';
export 'enums/stripe_api_error_type.dart';
export 'enums/pause_collection_behavior.dart';
export 'enums/source_type.dart';
export 'enums/source_status.dart';
export 'enums/source_type.dart';
export 'enums/stripe_api_error_type.dart';

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import 'package:stripe/messages.dart';
import 'package:stripe/src/messages/expandable_objects/expandable_object.dart';

typedef CustomerExpandableObject = SimpleExpandableObject<Customer>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import 'package:stripe/messages.dart';
import 'package:stripe/src/messages/expandable_objects/expandable_object.dart';

typedef DiscountExpandableObject = SimpleExpandableObject<Discount>;
60 changes: 60 additions & 0 deletions lib/src/messages/expandable_objects/expandable_object.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import 'package:stripe/src/utils/map_extension.dart';

/// A class that helps generating queries for expanding API responses.
/// [expandableFields] must be implemented.
///
/// Examples of queries that can be generated by [expandQuery] method:
/// ```
/// ["discounts"]
/// ["latest_invoice.payment_intent", "latest_invoice.payment_intent"]
/// ```
///
/// /// Expanding Stripe API responses: https://docs.stripe.com/expand
abstract class ExpandableObject<T> {
Map<String, ExpandableObject?> get expandableFields;

Map<String, ExpandableObject> get expand =>
expandableFields.whereValueNotNull();

const ExpandableObject();

List<String>? expandQuery() {
final query = _generateExpandQuery().toList();

if (query.isEmpty) return null;

return query.toList();
}

Iterable<String> _generateExpandQuery() sync* {
for (final fieldEntry in expand.entries) {
final fieldName = fieldEntry.key;
final fieldExpandObject = fieldEntry.value;
yield* _generateExpandQueryForField(fieldName, fieldExpandObject);
}
}

Iterable<String> _generateExpandQueryForField(
String name,
ExpandableObject<dynamic> object,
) sync* {
final innerExpandFields = object.expandQuery();

if (innerExpandFields == null || innerExpandFields.isEmpty) {
yield name;
} else {
for (final field in innerExpandFields) {
yield [name, field].join('.');
}
}
}
}

/// An implementation of ExpandableObject<T> for objects that don't have any
/// nested expandable fields.
class SimpleExpandableObject<T> extends ExpandableObject<T> {
@override
Map<String, ExpandableObject?> get expandableFields => const {};

const SimpleExpandableObject();
}
20 changes: 20 additions & 0 deletions lib/src/messages/expandable_objects/invoice_expandable_object.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:stripe/messages.dart';
import 'package:stripe/src/messages/expandable_objects/discount_expandable_object.dart';
import 'package:stripe/src/messages/expandable_objects/expandable_object.dart';
import 'package:stripe/src/messages/expandable_objects/payment_intent_expandable_object.dart';

class InvoiceExpandableObject extends ExpandableObject<Invoice> {
final PaymentIntentExpandableObject? paymentIntent;
final DiscountExpandableObject? discounts;

@override
Map<String, ExpandableObject?> get expandableFields => {
'payment_intent': paymentIntent,
'discounts': discounts,
};

const InvoiceExpandableObject({
this.paymentIntent,
this.discounts,
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import 'package:stripe/messages.dart';
import 'package:stripe/src/messages/expandable_objects/expandable_object.dart';

typedef PaymentIntentExpandableObject = SimpleExpandableObject<Discount>;
Loading