Skip to content

Commit

Permalink
feat: add optional HttpClient to allow mocking (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
ethan-tbd authored Apr 23, 2024
1 parent f4d2ed0 commit 3b7c7c3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
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 {
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);
}
}

0 comments on commit 3b7c7c3

Please sign in to comment.