Skip to content

Commit

Permalink
allow custom request headers in sep requests
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-rogobete committed Jul 21, 2024
1 parent c8d91a8 commit 79bc528
Show file tree
Hide file tree
Showing 10 changed files with 474 additions and 293 deletions.
17 changes: 7 additions & 10 deletions lib/src/sep/0001/stellar_toml.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import 'dart:developer';
import 'package:http/http.dart' as http;
import 'package:toml/toml.dart';

import '../../requests/request_builder.dart';

/// Parses the stellar toml data from a given string or from a given domain.
/// See <a href="https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0001.md" target="_blank">Stellar Toml</a>
/// Supported Version: 2.5.0
Expand Down Expand Up @@ -176,12 +174,10 @@ class StellarToml {
}

static Future<StellarToml> fromDomain(String domain,
{http.Client? httpClient}) async {
{http.Client? httpClient, Map<String, String>? httpRequestHeaders}) async {
Uri uri = Uri.parse("https://" + domain + "/.well-known/stellar.toml");
http.Client client = httpClient == null ? http.Client() : httpClient;
return await client
.get(uri, headers: RequestBuilder.headers)
.then((response) {
return await client.get(uri, headers: httpRequestHeaders).then((response) {
if (response.statusCode != 200) {
throw Exception(
"Stellar toml not found, response status code ${response.statusCode}");
Expand All @@ -192,12 +188,13 @@ class StellarToml {

/// Alternately to specifying a currency in its content, stellar.toml can link out to a separate TOML file for the currency by specifying toml="https://DOMAIN/.well-known/CURRENCY.toml" as the currency's only field.
/// In this case you can use this method to load the currency data from the received link (Currency.toml).
static Future<Currency> currencyFromUrl(String toml) async {
static Future<Currency> currencyFromUrl(String toml,
{http.Client? httpClient, Map<String, String>? httpRequestHeaders}) async {
Uri uri = Uri.parse(toml);

return await http.Client()
.get(uri, headers: RequestBuilder.headers)
.then((response) {
http.Client client = httpClient == null ? http.Client() : httpClient;

return await client.get(uri, headers: httpRequestHeaders).then((response) {
if (response.statusCode != 200) {
throw Exception(
"Currency toml not found, response status code ${response.statusCode}");
Expand Down
125 changes: 81 additions & 44 deletions lib/src/sep/0002/federation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,53 +13,67 @@ import '../../requests/request_builder.dart';
class Federation {
/// Resolves a stellar address such as bob*soneso.com.
/// Returns a [FederationResponse] object.
static Future<FederationResponse> resolveStellarAddress(String address) async {
String addr = address;
if (!addr.contains("*")) {
throw Exception("invalid federation address: $addr");
static Future<FederationResponse> resolveStellarAddress(String address,
{http.Client? httpClient,
Map<String, String>? httpRequestHeaders}) async {
if (!address.contains("*")) {
throw Exception("invalid federation address: $address");
}

String domain = addr.split("*").last;
StellarToml toml = await StellarToml.fromDomain(domain);
String domain = address.split("*").last;
StellarToml toml = await StellarToml.fromDomain(domain,
httpClient: httpClient, httpRequestHeaders: httpRequestHeaders);
String? federationServer = toml.generalInformation.federationServer;
if (federationServer == null) {
throw Exception("no federation server found for domain $domain");
}

Uri serverURI = Uri.parse(federationServer);
http.Client httpClient = http.Client();

_FederationRequestBuilder requestBuilder = _FederationRequestBuilder(httpClient, serverURI);
FederationResponse response =
await requestBuilder.forStringToLookUp(addr).forType("name").execute();
http.Client client = httpClient == null ? http.Client() : httpClient;

_FederationRequestBuilder requestBuilder = _FederationRequestBuilder(
client, serverURI,
httpRequestHeaders: httpRequestHeaders);
FederationResponse response = await requestBuilder
.forStringToLookUp(address)
.forType("name")
.execute();
return response;
}

/// Resolves a stellar account id such as GBVPKXWMAB3FIUJB6T7LF66DABKKA2ZHRHDOQZ25GBAEFZVHTBPJNOJI
/// The url of the federation server has to be provided.
/// Returns a [FederationResponse] object.
static Future<FederationResponse> resolveStellarAccountId(
String accountId, String federationServerUrl) async {

String accountId, String federationServerUrl,
{http.Client? httpClient,
Map<String, String>? httpRequestHeaders}) async {
Uri serverURI = Uri.parse(federationServerUrl);
http.Client httpClient = http.Client();

_FederationRequestBuilder requestBuilder = _FederationRequestBuilder(httpClient, serverURI);
FederationResponse response =
await requestBuilder.forStringToLookUp(accountId).forType("id").execute();
http.Client client = httpClient == null ? http.Client() : httpClient;

_FederationRequestBuilder requestBuilder = _FederationRequestBuilder(
client, serverURI,
httpRequestHeaders: httpRequestHeaders);
FederationResponse response = await requestBuilder
.forStringToLookUp(accountId)
.forType("id")
.execute();
return response;
}

/// Resolves a stellar account transaction id such as c1b368c00e9852351361e07cc58c54277e7a6366580044ab152b8db9cd8ec52a
/// The url of the federation server has to be provided.
/// Returns a [FederationResponse] object.
static Future<FederationResponse> resolveStellarTransactionId(
String txId, String federationServerUrl) async {

String txId, String federationServerUrl,
{http.Client? httpClient,
Map<String, String>? httpRequestHeaders}) async {
Uri serverURI = Uri.parse(federationServerUrl);
http.Client httpClient = http.Client();
http.Client client = httpClient == null ? http.Client() : httpClient;

_FederationRequestBuilder requestBuilder = _FederationRequestBuilder(httpClient, serverURI);
_FederationRequestBuilder requestBuilder = _FederationRequestBuilder(
client, serverURI,
httpRequestHeaders: httpRequestHeaders);
FederationResponse response =
await requestBuilder.forStringToLookUp(txId).forType("txid").execute();
return response;
Expand All @@ -69,14 +83,19 @@ class Federation {
/// The url of the federation server and the forward query parameters have to be provided.
/// Returns a [FederationResponse] object.
static Future<FederationResponse> resolveForward(
Map<String, String> forwardQueryParameters, String federationServerUrl) async {

Map<String, String> forwardQueryParameters, String federationServerUrl,
{http.Client? httpClient,
Map<String, String>? httpRequestHeaders}) async {
Uri serverURI = Uri.parse(federationServerUrl);
http.Client httpClient = http.Client();

_FederationRequestBuilder requestBuilder = _FederationRequestBuilder(httpClient, serverURI);
FederationResponse response =
await requestBuilder.forType("forward").forQueryParameters(forwardQueryParameters).execute();
http.Client client = httpClient == null ? http.Client() : httpClient;

_FederationRequestBuilder requestBuilder = _FederationRequestBuilder(
client, serverURI,
httpRequestHeaders: httpRequestHeaders);
FederationResponse response = await requestBuilder
.forType("forward")
.forQueryParameters(forwardQueryParameters)
.execute();
return response;
}
}
Expand All @@ -89,25 +108,33 @@ class FederationResponse extends Response {
String? memoType;
String? memo;

FederationResponse(this.stellarAddress, this.accountId, this.memoType, this.memo);
FederationResponse(
this.stellarAddress, this.accountId, this.memoType, this.memo);

factory FederationResponse.fromJson(Map<String, dynamic> json) => FederationResponse(
json['stellar_address'],
json['account_id'],
json['memo_type'] == null ? null : json['memo_type'],
json['memo'] == null ? null : json['memo']);
factory FederationResponse.fromJson(Map<String, dynamic> json) =>
FederationResponse(
json['stellar_address'],
json['account_id'],
json['memo_type'] == null ? null : json['memo_type'],
json['memo'] == null ? null : json['memo']);
}

// Requests the federation data.
class _FederationRequestBuilder extends RequestBuilder {
_FederationRequestBuilder(http.Client httpClient, Uri serverURI)
Map<String, String>? httpRequestHeaders;

_FederationRequestBuilder(http.Client httpClient, Uri serverURI,
{this.httpRequestHeaders})
: super(httpClient, serverURI, null);

Future<FederationResponse> federationURI(Uri uri) async {
TypeToken<FederationResponse> type = TypeToken<FederationResponse>();
ResponseHandler<FederationResponse> responseHandler = ResponseHandler<FederationResponse>(type);
ResponseHandler<FederationResponse> responseHandler =
ResponseHandler<FederationResponse>(type);

return await httpClient.get(uri, headers: RequestBuilder.headers).then((response) {
return await httpClient
.get(uri, headers: httpRequestHeaders)
.then((response) {
return responseHandler.handleResponse(response);
});
}
Expand All @@ -122,21 +149,31 @@ class _FederationRequestBuilder extends RequestBuilder {
return this;
}

_FederationRequestBuilder forQueryParameters(Map<String, String> queryParams) {
_FederationRequestBuilder forQueryParameters(
Map<String, String> queryParams) {
queryParameters.addAll(queryParams);
return this;
}

static Future<FederationResponse> requestExecute(http.Client httpClient, Uri uri) async {
static Future<FederationResponse> requestExecute(
http.Client httpClient, Uri uri,
{Map<String, String>? httpRequestHeaders}) async {
TypeToken<FederationResponse> type = TypeToken<FederationResponse>();
ResponseHandler<FederationResponse> responseHandler = ResponseHandler<FederationResponse>(type);

return await httpClient.get(uri, headers: RequestBuilder.headers).then((response) {
ResponseHandler<FederationResponse> responseHandler =
ResponseHandler<FederationResponse>(type);

return await httpClient
.get(
uri,
headers: httpRequestHeaders,
)
.then((response) {
return responseHandler.handleResponse(response);
});
}

Future<FederationResponse> execute() {
return _FederationRequestBuilder.requestExecute(this.httpClient, this.buildUri());
return _FederationRequestBuilder.requestExecute(
this.httpClient, this.buildUri());
}
}
Loading

0 comments on commit 79bc528

Please sign in to comment.