Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
leoafarias committed Sep 26, 2024
1 parent a3ca809 commit 7548368
Show file tree
Hide file tree
Showing 8 changed files with 63,513 additions and 31 deletions.
4 changes: 3 additions & 1 deletion lib/src/helpers/recursive_paging.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import '../models/search_results_model.dart';
import '../pub_api_client_base.dart';

Future<List<PackageResult>> recursivePaging(
PubClient client, SearchResults prevResults) async {
PubClient client,
SearchResults prevResults,
) async {
final packages = [...prevResults.packages];
final nextPage = prevResults.next;
if (nextPage != null) {
Expand Down
3 changes: 2 additions & 1 deletion lib/src/pub_api_client_base.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:convert';
import 'dart:developer';

import 'package:http/http.dart' as http;
import 'package:oauth2/oauth2.dart';
Expand Down Expand Up @@ -54,7 +55,7 @@ class PubClient {

Future<Map<String, dynamic>> _fetch(String url) async {
if (debug) {
print('Fetching: $url');
log('Fetching: $url');
}
final response = await _client.get(
Uri.parse(url),
Expand Down
94 changes: 94 additions & 0 deletions test/endpoints_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import 'package:pub_api_client/src/endpoints.dart';
import 'package:pub_api_client/src/models/search_order.dart';
import 'package:test/test.dart';

void main() {
group('Endpoint', () {
late Endpoint endpoint;

setUp(() {
endpoint = Endpoint(null);
});

test('constructor uses default base URL when null is provided', () {
expect(endpoint.baseUrl, equals(Endpoint.defaultBaseUrl));
});

test('constructor uses custom base URL when provided', () {
final customEndpoint = Endpoint('https://custom.pub.dev');
expect(customEndpoint.baseUrl, equals('https://custom.pub.dev'));
});

test('packageInfo returns correct URL', () {
expect(endpoint.packageInfo('flutter'),
equals('${Endpoint.defaultBaseUrl}/api/packages/flutter'));
});

test('packageScore returns correct URL', () {
expect(endpoint.packageScore('dart'),
equals('${Endpoint.defaultBaseUrl}/api/packages/dart/score'));
});

test('packageMetrics returns correct URL', () {
expect(endpoint.packageMetrics('http'),
equals('${Endpoint.defaultBaseUrl}/api/packages/http/metrics'));
});

test('packageOptions returns correct URL', () {
expect(endpoint.packageOptions('test'),
equals('${Endpoint.defaultBaseUrl}/api/packages/test/options'));
});

test('packagePublisher returns correct URL', () {
expect(endpoint.packagePublisher('bloc'),
equals('${Endpoint.defaultBaseUrl}/api/packages/bloc/publisher'));
});

test('packageDocumentation returns correct URL', () {
expect(endpoint.packageDocumentation('provider'),
equals('${Endpoint.defaultBaseUrl}/api/documentation/provider'));
});

test('packageVersions returns correct URL', () {
expect(endpoint.packageVersions('rxdart'),
equals('${Endpoint.defaultBaseUrl}/packages/rxdart.json'));
});

test('packageVersionInfo returns correct URL', () {
expect(
endpoint.packageVersionInfo('get_it', '1.0.0'),
equals(
'${Endpoint.defaultBaseUrl}/api/packages/get_it/versions/1.0.0'));
});

test('packageNames returns correct URL', () {
expect(endpoint.packageNames,
equals('${Endpoint.defaultBaseUrl}/api/package-names'));
});

test('likePackage returns correct URL', () {
expect(endpoint.likePackage('dio'),
equals('${Endpoint.defaultBaseUrl}/api/account/likes/dio'));
});

test('likedPackages returns correct URL', () {
expect(endpoint.likedPackages,
equals('${Endpoint.defaultBaseUrl}/api/account/likes'));
});

test('search returns correct URL with parameters', () {
expect(
endpoint.search('flutter', 1, SearchOrder.top),
equals(
'${Endpoint.defaultBaseUrl}/api/search?q=flutter&page=1&sort=top'));
});

test('nextPage returns correct URL with custom base URL', () {
final customEndpoint = Endpoint('https://custom.pub.dev');
expect(
customEndpoint
.nextPage('https://pub.dev/api/search?q=flutter&page=2'),
equals('https://custom.pub.dev/api/search?q=flutter&page=2'));
});
});
}
Loading

0 comments on commit 7548368

Please sign in to comment.