Skip to content

Commit

Permalink
Merge pull request #41 from leoafarias/feature/topics
Browse files Browse the repository at this point in the history
Added topics to search method
  • Loading branch information
leoafarias authored Aug 8, 2023
2 parents 7129518 + 80403fe commit 33c6684
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 2.6.0

- Feature: Added topics parameters to search method
- Chore: Improved models equality and toString methods
- Fix: Hanging on close [#40](https://github.com/leoafarias/pub_api_client/pull/40)

## 2.5.0

- Feature: Ability to customize user-agent
Expand Down
10 changes: 10 additions & 0 deletions lib/src/pub_api_client_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ import 'version.dart';
class PubClient {
final Endpoint endpoint;
final String? pubUrl;
final bool debug;
late http.Client _client;
late Map<String, String> _headers = {};

PubClient({
this.pubUrl,
Credentials? credentials,
http.Client? client,
this.debug = false,
String? userAgent,
}) : endpoint = Endpoint(pubUrl) {
http.Client httpClient;
Expand All @@ -51,6 +53,9 @@ class PubClient {
}

Future<Map<String, dynamic>> _fetch(String url) async {
if (debug) {
print('Fetching: $url');
}
final response = await _client.get(
Uri.parse(url),
headers: _headers,
Expand Down Expand Up @@ -152,11 +157,16 @@ class PubClient {
int page = 1,
SearchOrder sort = SearchOrder.top,
List<String> tags = const [],
List<String> topics = const [],
}) async {
final buffer = StringBuffer(query);
for (final tag in tags) {
buffer.write(' $tag');
}

for (final topic in topics) {
buffer.write(' topic:$topic');
}
final data = await _fetch(endpoint.search(buffer.toString(), page, sort));
return SearchResults.fromMap(data);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/version.dart

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

2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: pub_api_client
description: An API Client for Pub to interact with public package information.
version: 2.5.0
version: 2.6.0
homepage: https://github.com/leoafarias/pub_api_client

environment:
Expand Down
22 changes: 17 additions & 5 deletions test/pubdev_api_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:pub_api_client/pub_api_client.dart';
import 'package:test/test.dart';

const packageName = 'fvm';
final client = PubClient();
final client = PubClient(debug: true);

void main() {
group('PubDev Client', () {
Expand Down Expand Up @@ -40,8 +40,10 @@ void main() {

test('Get package version info', () async {
final package = await client.packageInfo(packageName);
final version =
await client.packageVersionInfo(packageName, package.version);
final version = await client.packageVersionInfo(
packageName,
package.version,
);

expect(package.latest.archiveUrl, version.archiveUrl);
expect(package.version, version.version);
Expand Down Expand Up @@ -111,8 +113,10 @@ void main() {
test('Search for packages of a publisher', () async {
final payload =
await client.search('', tags: [PackageTag.publisher('fvm.app')]);
final nextPagePayload = await client
.search('', tags: [PackageTag.dependency('pub_api_client')]);
final nextPagePayload = await client.search(
'',
tags: [PackageTag.dependency('pub_api_client')],
);
expect(payload.packages.length, greaterThan(0));
expect(nextPagePayload.packages.length, greaterThan(0));
});
Expand All @@ -127,6 +131,14 @@ void main() {
expect(packages.length, greaterThan(20000));
});

test('Get package topics', () async {
final results = await client.search('', topics: ['ffi']);
final zeroResults = await client.search('', topics: ['h8haisd091']);

expect(results.packages.length, greaterThan(0));
expect(zeroResults.packages.length, 0);
});

// test('Can like, unlike, and view liked packages', () async {
// if (pubCredentials == null) {
// print('Skipping test. No credentials found.');
Expand Down

0 comments on commit 33c6684

Please sign in to comment.