Skip to content

Commit 35f4ea9

Browse files
committed
fix: cakepay countries endpoint
1 parent bc6c63f commit 35f4ea9

2 files changed

Lines changed: 50 additions & 66 deletions

File tree

lib/pages/cakepay/cakepay_vendors_view.dart

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,27 @@ class _CakePayVendorsViewState extends State<CakePayVendorsView> {
6060

6161
/// Derive a country list from the loaded vendors so we don't need the
6262
/// broken /marketplace/countries/ endpoint.
63-
void _deriveCountries() {
64-
final seen = <String>{};
65-
_countryNames =
66-
_vendors
67-
.map((v) => v.country)
68-
.whereType<String>()
69-
.where((c) => c.isNotEmpty && seen.add(c))
70-
.toList()
71-
..sort();
63+
Future<void> _deriveCountries() async {
64+
// naive caching
65+
if (_countryNames.isNotEmpty) return;
66+
67+
final response = await CakePayService.instance.client.getAllCountries();
68+
69+
if (response.hasError || response.value == null) {
70+
if (mounted) {
71+
setState(() {
72+
_error = response.exception?.message ?? "Failed to load countries";
73+
});
74+
}
75+
} else {
76+
_countryNames =
77+
response.value!
78+
.where((e) => e.available)
79+
.map((e) => e.name)
80+
.toSet()
81+
.toList(growable: false)
82+
..sort();
83+
}
7284
}
7385

7486
Future<void> _loadVendors() async {
@@ -86,15 +98,16 @@ class _CakePayVendorsViewState extends State<CakePayVendorsView> {
8698

8799
if (!mounted) return;
88100

89-
setState(() {
90-
_loading = false;
91-
if (!resp.hasError && resp.value != null) {
92-
_vendors = resp.value!;
93-
_deriveCountries();
94-
} else {
101+
if (resp.hasError || resp.value == null) {
102+
setState(() {
95103
_error = resp.exception?.message ?? "Failed to load gift cards";
96-
}
97-
});
104+
});
105+
} else {
106+
_vendors = resp.value!;
107+
await _deriveCountries();
108+
}
109+
110+
if (mounted) setState(() => _loading = false);
98111
}
99112

100113
Future<void> _onCardTapped(CakePayCard card) async {

lib/services/cakepay/src/client.dart

Lines changed: 20 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class CakePayClient {
2727
HTTP? httpClient,
2828
}) : _httpClient = httpClient ?? const HTTP();
2929

30-
Map<String, String> _headers() => {
30+
late final _authHeaders = {
3131
'Authorization': 'Bearer $apiToken',
3232
'Content-Type': 'application/json',
3333
};
@@ -175,44 +175,8 @@ class CakePayClient {
175175
);
176176
}
177177

178-
Future<ApiResponse<List<CakePayCountry>>> getCountries({
179-
int? page,
180-
int? pageSize,
181-
}) async {
182-
final query = <String, String>{};
183-
if (page != null) query['page'] = page.toString();
184-
if (pageSize != null) query['page_size'] = pageSize.toString();
185-
186-
return _requestRaw(
187-
'GET',
188-
'/marketplace/countries/',
189-
query: query,
190-
parse: (body) {
191-
final decoded = jsonDecode(body);
192-
if (decoded is List) {
193-
return decoded
194-
.whereType<Map<String, dynamic>>()
195-
.map(CakePayCountry.fromJson)
196-
.toList();
197-
}
198-
if (decoded is Map<String, dynamic>) {
199-
final results = decoded['results'];
200-
if (results is List) {
201-
return results
202-
.whereType<Map<String, dynamic>>()
203-
.map(CakePayCountry.fromJson)
204-
.toList();
205-
}
206-
}
207-
return [];
208-
},
209-
);
210-
}
211-
212178
/// Fetches all countries by following pagination til last page.
213-
Future<ApiResponse<List<CakePayCountry>>> getAllCountries({
214-
int pageSize = 250,
215-
}) async {
179+
Future<ApiResponse<List<CakePayCountry>>> getAllCountries() async {
216180
try {
217181
final allCountries = <CakePayCountry>[];
218182
int page = 1;
@@ -221,7 +185,8 @@ class CakePayClient {
221185
final response = await _send(
222186
'GET',
223187
'/marketplace/countries/',
224-
query: {'page': page.toString(), 'page_size': pageSize.toString()},
188+
query: {'page': page.toString()},
189+
overrideHeaders: {}, // Auth here leads to 403. Why? Who knows?
225190
);
226191

227192
if (response.code < 200 || response.code >= 300) {
@@ -236,15 +201,16 @@ class CakePayClient {
236201

237202
final decoded = jsonDecode(response.body);
238203

204+
// This never gets hit according to docs
239205
// Handle non-paginated response (plain list).
240-
if (decoded is List) {
241-
return ApiResponse(
242-
value: decoded
243-
.whereType<Map<String, dynamic>>()
244-
.map(CakePayCountry.fromJson)
245-
.toList(),
246-
);
247-
}
206+
// if (decoded is List) {
207+
// return ApiResponse(
208+
// value: decoded
209+
// .whereType<Map<String, dynamic>>()
210+
// .map(CakePayCountry.fromJson)
211+
// .toList(),
212+
// );
213+
// }
248214

249215
if (decoded is Map<String, dynamic>) {
250216
final results = decoded['results'];
@@ -466,15 +432,20 @@ class CakePayClient {
466432
String path, {
467433
Map<String, dynamic>? body,
468434
Map<String, String>? query,
435+
Map<String, String>? overrideHeaders,
469436
}) async {
470437
var uri = Uri.parse('$baseUrl$path');
471438
if (query != null && query.isNotEmpty) {
472439
uri = uri.replace(queryParameters: query);
473440
}
474-
final headers = _headers();
441+
final headers = overrideHeaders ?? _authHeaders;
475442
final proxy = _proxyInfo;
476443

477-
Logging.instance.t("$_kTag $method $uri");
444+
try {
445+
throw Exception(path);
446+
} catch (e, s) {
447+
Logging.instance.f("$_kTag $method $uri", error: e, stackTrace: s);
448+
}
478449

479450
switch (method) {
480451
case 'GET':

0 commit comments

Comments
 (0)