Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

fix: update http client #27

Merged
merged 6 commits into from
May 6, 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
17 changes: 7 additions & 10 deletions lib/src/http_client/tbdex_http_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class TbdexHttpClient {
return Parser.parseExchange(response.body);
}

static Future<List<Exchange>> getExchanges(
static Future<List<String>> listExchanges(
BearerDid did,
String pfiDid,
) async {
Expand All @@ -71,7 +71,7 @@ class TbdexHttpClient {
return Parser.parseExchanges(response.body);
}

static Future<List<Offering>> getOfferings(
static Future<List<Offering>> listOfferings(
String pfiDid, {
GetOfferingsFilter? filter,
}) async {
Expand Down Expand Up @@ -128,16 +128,13 @@ class TbdexHttpClient {
final pfiServiceEndpoint = await _getPfiServiceEndpoint(pfiDid);
final path = '/exchanges${exchangeId != null ? '/$exchangeId' : ''}';
final url = Uri.parse(pfiServiceEndpoint + path);
final headers = {'Content-Type': _jsonHeader};

final response = await _client.post(
url,
headers: {
'Content-Type': _jsonHeader,
},
body: requestBody,
);
final response = await (exchangeId == null
? _client.post(url, headers: headers, body: requestBody)
: _client.put(url, headers: headers, body: requestBody));

if (response.statusCode != 201) {
if (response.statusCode != 202) {
throw Exception(response);
}
}
Expand Down
36 changes: 20 additions & 16 deletions lib/src/protocol/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ import 'package:tbdex/src/protocol/validator.dart';

abstract class Parser {
static Message parseMessage(String rawMessage) {

final jsonObject = jsonDecode(rawMessage) as Map<String, dynamic>?;
if (jsonObject is! Map<String, dynamic>) {
throw TbdexParseException(TbdexExceptionCode.parserMessageJsonNotObject, 'Message JSON must be an object');
throw TbdexParseException(
TbdexExceptionCode.parserMessageJsonNotObject,
'Message JSON must be an object',
);
}
Validator.validate(jsonObject, 'message');

Expand Down Expand Up @@ -56,7 +58,7 @@ abstract class Parser {
return parsedMessages;
}

static List<Exchange> parseExchanges(String rawExchanges) {
static List<String> parseExchanges(String rawExchanges) {
final jsonObject = jsonDecode(rawExchanges);

if (jsonObject is! Map<String, dynamic>) {
Expand All @@ -69,18 +71,11 @@ abstract class Parser {
throw Exception('exchanges data is malformed or empty');
}

final parsedExchanges = <Exchange>[];

for (final exchangeJson in exchanges) {
final parsedMessages = <Message>[];
for (final messageJson in exchangeJson) {
final message = _parseMessageJson(messageJson);
parsedMessages.add(message);
}
parsedExchanges.add(parsedMessages);
if (exchanges.any((element) => element is! String)) {
throw Exception('all exchange ids should be strings');
}

return parsedExchanges;
return List<String>.from(exchanges);
}

static List<Offering> parseOfferings(String rawOfferings) {
Expand Down Expand Up @@ -109,7 +104,10 @@ abstract class Parser {
final messageKind = _getKindFromJson(jsonObject);
final matchedKind = MessageKind.values.firstWhere(
(kind) => kind.name == messageKind,
orElse: () => throw TbdexParseException(TbdexExceptionCode.parserUnknownMessageKind, 'unknown message kind: $messageKind'),
orElse: () => throw TbdexParseException(
TbdexExceptionCode.parserUnknownMessageKind,
'unknown message kind: $messageKind',
),
);

switch (matchedKind) {
Expand All @@ -130,7 +128,10 @@ abstract class Parser {
final resourceKind = _getKindFromJson(jsonObject);
final matchedKind = ResourceKind.values.firstWhere(
(kind) => kind.name == resourceKind,
orElse: () => throw TbdexParseException(TbdexExceptionCode.parserUnknownResourceKind, 'unknown resource kind: $resourceKind'),
orElse: () => throw TbdexParseException(
TbdexExceptionCode.parserUnknownResourceKind,
'unknown resource kind: $resourceKind',
),
);

switch (matchedKind) {
Expand All @@ -146,7 +147,10 @@ abstract class Parser {
final metadata = jsonObject['metadata'];

if (metadata is! Map<String, dynamic> || metadata.isEmpty) {
throw TbdexParseException(TbdexExceptionCode.parserMetadataMalformed, 'metadata is malformed or empty');
throw TbdexParseException(
TbdexExceptionCode.parserMetadataMalformed,
'metadata is malformed or empty',
);
}

final kind = metadata['kind'];
Expand Down
26 changes: 4 additions & 22 deletions test/helpers/test_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -175,28 +175,10 @@ class TestData {
return jsonEncode({'data': jsonData});
}

static String getExchangesResponse() {
final offering = TestData.getOffering();
final rfq = TestData.getRfq(offeringId: offering.metadata.id);
final quote = TestData.getQuote();

final mockExchanges = [
[rfq, quote],
];

final jsonData = mockExchanges
.map(
(exchangeList) => exchangeList.map((message) {
if (message is Rfq) {
return message.toJson();
} else if (message is Quote) {
return message.toJson();
}
}).toList(),
)
.toList();

return jsonEncode({'data': jsonData});
static String listExchangesResponse() {
return jsonEncode({
'data': ['123', '456', '789'],
});
}

static String getCreateExchangeRequest(Rfq rfq, {String? replyTo}) =>
Expand Down
26 changes: 13 additions & 13 deletions test/http_client/tbdex_http_client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,22 @@ void main() async {
).called(1);
});

test('can get exchanges', () async {
test('can list exchanges', () async {
when(
() => mockHttpClient.get(
Uri.parse('$pfiServiceEndpoint/exchanges/'),
headers: any(named: 'headers'),
),
).thenAnswer(
(_) async => http.Response(TestData.getExchangesResponse(), 200),
(_) async => http.Response(TestData.listExchangesResponse(), 200),
);

final exchanges = await TbdexHttpClient.getExchanges(
final exchanges = await TbdexHttpClient.listExchanges(
TestData.aliceDid,
pfiDid,
);

expect(exchanges.length, 1);
expect(exchanges.length, 3);

verify(
() => mockHttpClient.get(
Expand All @@ -80,14 +80,14 @@ void main() async {
).called(1);
});

test('can get offerings', () async {
test('can list offerings', () async {
when(
() => mockHttpClient.get(Uri.parse('$pfiServiceEndpoint/offerings/')),
).thenAnswer(
(_) async => http.Response(TestData.getOfferingResponse(), 200),
);

final offerings = await TbdexHttpClient.getOfferings(pfiDid);
final offerings = await TbdexHttpClient.listOfferings(pfiDid);

expect(offerings.length, 1);

Expand All @@ -107,7 +107,7 @@ void main() async {
body: TestData.getCreateExchangeRequest(rfq, replyTo: 'reply_to'),
),
).thenAnswer(
(_) async => http.Response('', 201),
(_) async => http.Response('', 202),
);

await TbdexHttpClient.createExchange(rfq, replyTo: 'reply_to');
Expand All @@ -127,19 +127,19 @@ void main() async {
await order.sign(TestData.aliceDid);

when(
() => mockHttpClient.post(
() => mockHttpClient.put(
Uri.parse('$pfiServiceEndpoint/exchanges/$exchangeId'),
headers: any(named: 'headers'),
body: TestData.getSubmitOrderRequest(order),
),
).thenAnswer(
(_) async => http.Response('', 201),
(_) async => http.Response('', 202),
);

await TbdexHttpClient.submitOrder(order);

verify(
() => mockHttpClient.post(
() => mockHttpClient.put(
Uri.parse('$pfiServiceEndpoint/exchanges/$exchangeId'),
headers: any(named: 'headers'),
body: TestData.getSubmitOrderRequest(order),
Expand All @@ -153,19 +153,19 @@ void main() async {
await close.sign(TestData.aliceDid);

when(
() => mockHttpClient.post(
() => mockHttpClient.put(
Uri.parse('$pfiServiceEndpoint/exchanges/$exchangeId'),
headers: any(named: 'headers'),
body: TestData.getSubmitCloseRequest(close),
),
).thenAnswer(
(_) async => http.Response('', 201),
(_) async => http.Response('', 202),
);

await TbdexHttpClient.submitClose(close);

verify(
() => mockHttpClient.post(
() => mockHttpClient.put(
Uri.parse('$pfiServiceEndpoint/exchanges/$exchangeId'),
headers: any(named: 'headers'),
body: TestData.getSubmitCloseRequest(close),
Expand Down
22 changes: 5 additions & 17 deletions test/protocol/parser_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dart:convert';

import 'package:tbdex/src/http_client/models/exchange.dart';
import 'package:tbdex/src/protocol/models/message.dart';
import 'package:tbdex/src/protocol/models/offering.dart';
import 'package:tbdex/src/protocol/models/quote.dart';
Expand Down Expand Up @@ -55,27 +54,16 @@ void main() async {
expect(exchange.last.toString(), equals(quoteJson));
});

test('can parse a list of exchanges', () async {
final rfq = TestData.getRfq();
final quote = TestData.getQuote();
final rfqJson = jsonEncode(rfq.toJson());
final quoteJson = jsonEncode(quote.toJson());

test('can parse a list of exchange ids', () async {
final exchanges = Parser.parseExchanges(
jsonEncode({
'data': [
[rfq, quote],
[rfq, quote],
],
'data': ['123', '456'],
}),
);

expect(exchanges, isA<List<Exchange>>());
expect(exchanges.first, isA<Exchange>());
expect(exchanges.first.first, isA<Rfq>());
expect(exchanges[1].last, isA<Quote>());
expect(exchanges.first.first.toString(), equals(rfqJson));
expect(exchanges[1].last.toString(), equals(quoteJson));
expect(exchanges, isA<List<String>>());
expect(exchanges.first, equals('123'));
expect(exchanges.last, equals('456'));
});

test('can parse a list of offerings', () async {
Expand Down
Loading