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

feat: add optional HttpClient to allow mocking #64

Merged
merged 1 commit into from
Apr 23, 2024
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
4 changes: 3 additions & 1 deletion packages/web5/lib/src/dids/did_jwk/did_jwk.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:convert';
import 'dart:io';

import 'package:web5/src/crypto.dart';
import 'package:web5/src/dids/did.dart';
Expand Down Expand Up @@ -60,7 +61,8 @@ class DidJwk {
/// an invalid [DidResolutionResult].
///
/// Throws [FormatException] if the JWK parsing fails.
static Future<DidResolutionResult> resolve(Did did) async {
static Future<DidResolutionResult> resolve(Did did,
{HttpClient? client}) async {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm feels odd here because not all resolvers need http clients

if (did.method != methodName) {
return DidResolutionResult.withError(DidResolutionError.invalidDid);
}
Expand Down
11 changes: 8 additions & 3 deletions packages/web5/lib/src/dids/did_method_resolver.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:io';

import 'package:web5/src/dids/did.dart';
import 'package:web5/src/dids/did_core.dart';

Expand All @@ -7,13 +9,16 @@ class DidMethodResolver {
String name;

/// The function to resolve a DID URI using this method.
Future<DidResolutionResult> Function(Did) resolve;
Future<DidResolutionResult> Function(Did, {HttpClient? client}) resolve;

/// Constructs a [DidMethodResolver] with a given [name] and [resolve] function.
DidMethodResolver({required this.name, required this.resolve});

Future<DidDereferenceResult> dereference(Did did) async {
final didResolutionResult = await resolve(did);
Future<DidDereferenceResult> dereference(
Did did, {
HttpClient? client,
}) async {
final didResolutionResult = await resolve(did, client: client);

if (didResolutionResult.hasError()) {
return DidDereferenceResult.withError(
Expand Down
24 changes: 16 additions & 8 deletions packages/web5/lib/src/dids/did_resolver.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:io';

import 'package:web5/src/dids/did.dart';
import 'package:web5/src/dids/did_core.dart';
import 'package:web5/src/dids/did_dht/did_dht.dart';
Expand Down Expand Up @@ -27,12 +29,15 @@ class DidResolver {
// Static field to hold the instance
static final DidResolver _instance = DidResolver._default();

static Future<DidResolutionResult> resolve(String uri) {
return _instance.resolveDid(uri);
static Future<DidResolutionResult> resolve(String uri, {HttpClient? client}) {
return _instance.resolveDid(uri, client: client);
}

static Future<DidDereferenceResult> dereference(String url) {
return _instance.dereferenceDid(url);
static Future<DidDereferenceResult> dereference(
String url, {
HttpClient? client,
}) {
return _instance.dereferenceDid(url, client: client);
}

/// Constructs a [DidResolver] with a list of [DidMethodResolver]s.
Expand All @@ -47,7 +52,7 @@ class DidResolver {
/// Resolves a DID URI into a [DidResolutionResult].
///
/// Throws an [Exception] if no resolver is available for the given method.
Future<DidResolutionResult> resolveDid(String uri) {
Future<DidResolutionResult> resolveDid(String uri, {HttpClient? client}) {
final Did did;
try {
did = Did.parse(uri);
Expand All @@ -63,20 +68,23 @@ class DidResolver {
throw Exception('no resolver available for did:${did.method}');
}

return resolver.resolve(did);
return resolver.resolve(did, client: client);
}

/// Resolves a DID URI into a [DidResolutionResult].
///
/// Throws an [Exception] if no resolver is available for the given method.
Future<DidDereferenceResult> dereferenceDid(String url) {
Future<DidDereferenceResult> dereferenceDid(
String url, {
HttpClient? client,
}) {
final did = Did.parse(url);
final resolver = methodResolvers[did.method];

if (resolver == null) {
throw Exception('no resolver available for did:${did.method}');
}

return resolver.dereference(did);
return resolver.dereference(did, client: client);
}
}
Loading