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

Fixes issue when closing client #40

Merged
merged 2 commits into from
Aug 8, 2023
Merged
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
1 change: 1 addition & 0 deletions example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ void main() async {
final client = PubClient();
final packageScore = await client.packageScore('fvm');
print('Package Score: $packageScore');
client.close();
}
18 changes: 0 additions & 18 deletions lib/src/helpers/http_client.dart

This file was deleted.

4 changes: 4 additions & 0 deletions lib/src/models/latest_version_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ class LatestVersion {
@override
int get hashCode =>
needUpdate.hashCode ^ latestVersion.hashCode ^ packageInfo.hashCode;

@override
String toString() =>
'LatestVersion(needUpdate: $needUpdate, latestVersion: $latestVersion, packageInfo: $packageInfo)';
}
7 changes: 7 additions & 0 deletions lib/src/models/package_documentation_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class PackageDocumentation {

@override
int get hashCode => name.hashCode ^ versions.hashCode;

@override
String toString() => 'PackageDocumentation(name: $name, versions: $versions)';
}

/// Package Documentation Version Model
Expand Down Expand Up @@ -92,4 +95,8 @@ class PackageDocumentationVersion {
@override
int get hashCode =>
version.hashCode ^ status.hashCode ^ hasDocumentation.hashCode;

@override
String toString() =>
'PackageDocumentationVersion(version: $version, status: $status, hasDocumentation: $hasDocumentation)';
}
15 changes: 15 additions & 0 deletions lib/src/models/package_like_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,19 @@ class PackageLike {
package: map['package'] as String? ?? '',
liked: map['liked'] as bool? ?? false,
);

@override
bool operator ==(Object other) {
if (identical(this, other)) return true;

return other is PackageLike &&
other.package == package &&
other.liked == liked;
}

@override
int get hashCode => package.hashCode ^ liked.hashCode;

@override
String toString() => 'PackageLike(package: $package, liked: $liked)';
}
15 changes: 15 additions & 0 deletions lib/src/models/package_metrics_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,19 @@ class PackageMetrics {
scorecard:
PackageScoreCard.fromMap(map['scorecard'] as Map<String, dynamic>),
);

@override
String toString() => 'PackageMetrics(score: $score, scorecard: $scorecard)';

@override
bool operator ==(Object other) {
if (identical(this, other)) return true;

return other is PackageMetrics &&
other.score == score &&
other.scorecard == scorecard;
}

@override
int get hashCode => score.hashCode ^ scorecard.hashCode;
}
18 changes: 18 additions & 0 deletions lib/src/models/package_options_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,22 @@ class PackageOptions {
isUnlisted: map['isUnlisted'] as bool? ?? false,
replacedBy: map['replacedBy'] as String?,
);

@override
String toString() =>
'PackageOptions(isDiscontinued: $isDiscontinued, isUnlisted: $isUnlisted, replacedBy: $replacedBy)';

@override
bool operator ==(Object other) {
if (identical(this, other)) return true;

return other is PackageOptions &&
other.isDiscontinued == isDiscontinued &&
other.isUnlisted == isUnlisted &&
other.replacedBy == replacedBy;
}

@override
int get hashCode =>
isDiscontinued.hashCode ^ isUnlisted.hashCode ^ replacedBy.hashCode;
}
13 changes: 13 additions & 0 deletions lib/src/models/package_publisher_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,17 @@ class PackagePublisher {
PackagePublisher(
publisherId: map['publisherId'] as String?,
);

@override
String toString() => 'PackagePublisher(publisherId: $publisherId)';

@override
bool operator ==(Object other) {
if (identical(this, other)) return true;

return other is PackagePublisher && other.publisherId == publisherId;
}

@override
int get hashCode => publisherId.hashCode;
}
10 changes: 10 additions & 0 deletions lib/src/models/package_score_model.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:collection/collection.dart';

// ignore_for_file: lines_longer_than_80_chars

/// Package Score Model
class PackageScore {
final int? grantedPoints;
Expand Down Expand Up @@ -31,6 +33,10 @@ class PackageScore {
lastUpdated: DateTime.parse(map['lastUpdated'] as String? ?? ''),
);

@override
String toString() =>
'PackageScore(grantedPoints: $grantedPoints, maxPoints: $maxPoints, likeCount: $likeCount, popularityScore: $popularityScore, lastUpdated: $lastUpdated)';

@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
Expand Down Expand Up @@ -103,6 +109,10 @@ class PackageScoreCard {
List<String>.from(map['reportTypes'] as List<dynamic>? ?? []),
);

@override
String toString() =>
'PackageScoreCard(packageName: $packageName, packageVersion: $packageVersion, runtimeVersion: $runtimeVersion, updated: $updated, packageCreated: $packageCreated, packageVersionCreated: $packageVersionCreated, derivedTags: $derivedTags, flags: $flags, reportTypes: $reportTypes)';

@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
Expand Down
41 changes: 41 additions & 0 deletions lib/src/models/pub_package_model.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:collection/collection.dart';
import 'package:pubspec/pubspec.dart';

/// Package Model
Expand Down Expand Up @@ -36,6 +37,24 @@ class PubPackage {
),
);
}

@override
String toString() =>
'PubPackage(name: $name, latest: $latest, versions: $versions)';

@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
final listEquals = const DeepCollectionEquality().equals;

return other is PubPackage &&
other.name == name &&
other.latest == latest &&
listEquals(other.versions, versions);
}

@override
int get hashCode => name.hashCode ^ latest.hashCode ^ versions.hashCode;
}

/// Package Version Model
Expand Down Expand Up @@ -65,4 +84,26 @@ class PackageVersion {
archiveUrl: map['archiveUrl'] as String? ?? '',
published: DateTime.parse(map['published'] as String? ?? ''),
);

@override
String toString() =>
'PackageVersion(version: $version, pubspec: $pubspec, archiveUrl: $archiveUrl, published: $published)';

@override
bool operator ==(Object other) {
if (identical(this, other)) return true;

return other is PackageVersion &&
other.version == version &&
other.pubspec == pubspec &&
other.archiveUrl == archiveUrl &&
other.published == published;
}

@override
int get hashCode =>
version.hashCode ^
pubspec.hashCode ^
archiveUrl.hashCode ^
published.hashCode;
}
6 changes: 6 additions & 0 deletions lib/src/models/search_results_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class SearchResults {
);
}

@override
String toString() => 'SearchResults(packages: $packages, next: $next)';

@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
Expand Down Expand Up @@ -53,6 +56,9 @@ class PackageResult {
package: map['package'] as String? ?? '',
);

@override
String toString() => 'PackageResult(package: $package)';

@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
Expand Down
51 changes: 31 additions & 20 deletions lib/src/pub_api_client_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'package:oauth2/oauth2.dart';
import 'constants.dart';
import 'endpoints.dart';
import 'helpers/exceptions.dart';
import 'helpers/http_client.dart';
import 'helpers/recursive_paging.dart';
import 'models/package_documentation_model.dart';
import 'models/package_like_model.dart';
Expand All @@ -17,54 +16,67 @@ import 'models/package_score_model.dart';
import 'models/pub_package_model.dart';
import 'models/search_order.dart';
import 'models/search_results_model.dart';

typedef FetchFunction = Future<Map<String, dynamic>> Function(String url);
import 'version.dart';

/// Pub API Client
class PubClient {
final Endpoint endpoint;
final String? pubUrl;
final http.Client? client;
final String? userAgent;
final Credentials? credentials;
late final PubApiHttpClient _client;
late http.Client _client;
late Map<String, String> _headers = {};

PubClient({
this.pubUrl,
this.credentials,
this.client,
this.userAgent,
Credentials? credentials,
http.Client? client,
String? userAgent,
}) : endpoint = Endpoint(pubUrl) {
http.Client httpClient;
if (credentials == null) {
httpClient = http.Client();
} else {
httpClient = Client(
credentials!,
credentials,
identifier: PubAuth.identifier,
secret: PubAuth.secret,
);
}

_client = PubApiHttpClient(
client ?? httpClient,
userAgent: userAgent,
);
userAgent ??= 'default';

_headers = {
'user-agent': 'pub_api_client/$packageVersion ($userAgent)',
};
_client = client ?? httpClient;
}

Future<Map<String, dynamic>> _fetch(String url) async {
final response = await _client.get(Uri.parse(url));
final response = await _client.get(
Uri.parse(url),
headers: _headers,
);

responseValidOrThrow(response);

return jsonDecode(response.body) as Map<String, dynamic>;
}

Future<Map<String, dynamic>> _put(String url) async {
final response = await _client.put(Uri.parse(url));
final response = await _client.put(
Uri.parse(url),
headers: _headers,
);

responseValidOrThrow(response);
return jsonDecode(response.body) as Map<String, dynamic>;
}

Future<void> _delete(String url) async {
final response = await _client.delete(Uri.parse(url));
final response = await _client.delete(
Uri.parse(url),
headers: _headers,
);

responseValidOrThrow(response);
}

Expand Down Expand Up @@ -94,7 +106,6 @@ class PubClient {
/// Returns the `PackageOptions` for package [packageName]
Future<PackageOptions> packageOptions(String packageName) async {
final data = await _fetch(endpoint.packageOptions(packageName));

return PackageOptions.fromMap(data);
}

Expand Down Expand Up @@ -235,6 +246,6 @@ class PubClient {
}

void close() {
client?.close();
_client.close();
}
}