|
| 1 | +import 'dart:convert'; |
| 2 | + |
| 3 | +import 'package:http/http.dart' as http; |
| 4 | +import 'package:tbdex/src/http_client/models/create_exchange_request.dart'; |
| 5 | +import 'package:tbdex/src/http_client/models/exchange.dart'; |
| 6 | +import 'package:tbdex/src/http_client/models/get_offerings_filter.dart'; |
| 7 | +import 'package:tbdex/src/protocol/models/close.dart'; |
| 8 | +import 'package:tbdex/src/protocol/models/offering.dart'; |
| 9 | +import 'package:tbdex/src/protocol/models/order.dart'; |
| 10 | +import 'package:tbdex/src/protocol/models/rfq.dart'; |
| 11 | +import 'package:tbdex/src/protocol/parser.dart'; |
| 12 | +import 'package:tbdex/src/protocol/validator.dart'; |
| 13 | +import 'package:typeid/typeid.dart'; |
| 14 | +import 'package:web5/web5.dart'; |
| 15 | + |
| 16 | +class TbdexHttpClient { |
| 17 | + TbdexHttpClient._(); |
| 18 | + |
| 19 | + static const _jsonHeader = 'application/json'; |
| 20 | + static const _expirationDuration = Duration(minutes: 5); |
| 21 | + |
| 22 | + static http.Client _client = http.Client(); |
| 23 | + |
| 24 | + // ignore: avoid_setters_without_getters |
| 25 | + static set client(http.Client client) { |
| 26 | + _client = client; |
| 27 | + } |
| 28 | + |
| 29 | + static Future<Exchange> getExchange( |
| 30 | + BearerDid did, |
| 31 | + String pfiDid, |
| 32 | + String exchangeId, |
| 33 | + ) async { |
| 34 | + final requestToken = await _generateRequestToken(did, pfiDid); |
| 35 | + final pfiServiceEndpoint = await _getPfiServiceEndpoint(pfiDid); |
| 36 | + final url = Uri.parse('$pfiServiceEndpoint/exchanges/$exchangeId'); |
| 37 | + |
| 38 | + final response = await _client.get( |
| 39 | + url, |
| 40 | + headers: { |
| 41 | + 'Authorization': 'Bearer $requestToken', |
| 42 | + }, |
| 43 | + ); |
| 44 | + |
| 45 | + if (response.statusCode != 200) { |
| 46 | + throw Exception('failed to fetch exchange: ${response.body}'); |
| 47 | + } |
| 48 | + |
| 49 | + return Parser.parseExchange(response.body); |
| 50 | + } |
| 51 | + |
| 52 | + static Future<List<Exchange>> getExchanges( |
| 53 | + BearerDid did, |
| 54 | + String pfiDid, |
| 55 | + ) async { |
| 56 | + final requestToken = await _generateRequestToken(did, pfiDid); |
| 57 | + final pfiServiceEndpoint = await _getPfiServiceEndpoint(pfiDid); |
| 58 | + final url = Uri.parse('$pfiServiceEndpoint/exchanges/'); |
| 59 | + |
| 60 | + final response = await _client.get( |
| 61 | + url, |
| 62 | + headers: { |
| 63 | + 'Authorization': 'Bearer $requestToken', |
| 64 | + }, |
| 65 | + ); |
| 66 | + |
| 67 | + if (response.statusCode != 200) { |
| 68 | + throw Exception('failed to fetch exchanges: ${response.body}'); |
| 69 | + } |
| 70 | + |
| 71 | + return Parser.parseExchanges(response.body); |
| 72 | + } |
| 73 | + |
| 74 | + static Future<List<Offering>> getOfferings( |
| 75 | + String pfiDid, { |
| 76 | + GetOfferingsFilter? filter, |
| 77 | + }) async { |
| 78 | + final pfiServiceEndpoint = await _getPfiServiceEndpoint(pfiDid); |
| 79 | + final url = Uri.parse('$pfiServiceEndpoint/offerings/').replace( |
| 80 | + queryParameters: filter?.toJson(), |
| 81 | + ); |
| 82 | + |
| 83 | + final response = await _client.get(url); |
| 84 | + |
| 85 | + if (response.statusCode != 200) { |
| 86 | + throw Exception(response); |
| 87 | + } |
| 88 | + |
| 89 | + return Parser.parseOfferings(response.body); |
| 90 | + } |
| 91 | + |
| 92 | + static Future<void> createExchange( |
| 93 | + Rfq rfq, { |
| 94 | + String? replyTo, |
| 95 | + }) async { |
| 96 | + Validator.validateMessage(rfq); |
| 97 | + final pfiDid = rfq.metadata.to; |
| 98 | + final body = jsonEncode( |
| 99 | + CreateExchangeRequest(rfq: rfq, replyTo: replyTo), |
| 100 | + ); |
| 101 | + |
| 102 | + await _submitMessage(pfiDid, body); |
| 103 | + } |
| 104 | + |
| 105 | + static Future<void> submitOrder(Order order) async { |
| 106 | + Validator.validateMessage(order); |
| 107 | + final pfiDid = order.metadata.to; |
| 108 | + final exchangeId = order.metadata.exchangeId; |
| 109 | + final body = jsonEncode(order.toJson()); |
| 110 | + |
| 111 | + await _submitMessage(pfiDid, body, exchangeId: exchangeId); |
| 112 | + } |
| 113 | + |
| 114 | + static Future<void> submitClose(Close close) async { |
| 115 | + Validator.validateMessage(close); |
| 116 | + final pfiDid = close.metadata.to; |
| 117 | + final exchangeId = close.metadata.exchangeId; |
| 118 | + final body = jsonEncode(close.toJson()); |
| 119 | + |
| 120 | + await _submitMessage(pfiDid, body, exchangeId: exchangeId); |
| 121 | + } |
| 122 | + |
| 123 | + static Future<void> _submitMessage( |
| 124 | + String pfiDid, |
| 125 | + String requestBody, { |
| 126 | + String? exchangeId, |
| 127 | + }) async { |
| 128 | + final pfiServiceEndpoint = await _getPfiServiceEndpoint(pfiDid); |
| 129 | + final path = '/exchanges${exchangeId != null ? '/$exchangeId' : ''}'; |
| 130 | + final url = Uri.parse(pfiServiceEndpoint + path); |
| 131 | + |
| 132 | + final response = await _client.post( |
| 133 | + url, |
| 134 | + headers: {'Content-Type': _jsonHeader}, |
| 135 | + body: requestBody, |
| 136 | + ); |
| 137 | + |
| 138 | + if (response.statusCode != 201) { |
| 139 | + throw Exception(response); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + static Future<String> _getPfiServiceEndpoint(String pfiDid) async { |
| 144 | + final didResolutionResult = await DidResolver.resolve(pfiDid); |
| 145 | + |
| 146 | + if (didResolutionResult.didDocument == null) { |
| 147 | + throw Exception('did resolution failed'); |
| 148 | + } |
| 149 | + |
| 150 | + final service = didResolutionResult.didDocument?.service?.firstWhere( |
| 151 | + (service) => service.type == 'PFI', |
| 152 | + orElse: () => throw Exception('did does not have service of type PFI'), |
| 153 | + ); |
| 154 | + |
| 155 | + return service?.serviceEndpoint ?? ''; |
| 156 | + } |
| 157 | + |
| 158 | + static Future<String> _generateRequestToken( |
| 159 | + BearerDid did, |
| 160 | + String pfiDid, |
| 161 | + ) async { |
| 162 | + final nowEpochSeconds = DateTime.now().millisecondsSinceEpoch ~/ 1000; |
| 163 | + final exp = nowEpochSeconds + _expirationDuration.inSeconds; |
| 164 | + |
| 165 | + return Jwt.sign( |
| 166 | + did: did, |
| 167 | + payload: JwtClaims( |
| 168 | + aud: pfiDid, |
| 169 | + iss: did.uri, |
| 170 | + exp: exp, |
| 171 | + iat: nowEpochSeconds, |
| 172 | + jti: TypeId.generate(''), |
| 173 | + ), |
| 174 | + ); |
| 175 | + } |
| 176 | +} |
0 commit comments