Dynamic proxy url#20
Open
OliverGesch wants to merge 6 commits intoblaugold:mainfrom
Open
Conversation
Owner
|
@OliverGesch Thanks for taking the time to create a PR. I think you can already achieve what you need without requiring any changes to this package: import 'dart:io';
import 'package:http/http.dart';
import 'package:unsplash_client/unsplash_client.dart';
class ProxyHttpClient extends BaseClient {
ProxyHttpClient({
required this.baseUrl,
required this.proxyBaseUrl,
this.bearerToken,
Client? httpClient,
}) : _httpClient = httpClient ?? Client();
final String baseUrl;
final String proxyBaseUrl;
final String? bearerToken;
final Client _httpClient;
@override
Future<StreamedResponse> send(BaseRequest request) {
// Replace the base URL with the proxy base URL.
final proxyUrl = Uri.parse(
request.url.toString().replaceFirst(baseUrl, proxyBaseUrl),
);
final proxyRequest = _copyRequest(request, url: proxyUrl, headers: {
// Add the headers from the original request, except for the
// authorization header.
...Map.from(request.headers)..remove(HttpHeaders.authorizationHeader),
// If a bearer token was provided, add it to the request.
if (bearerToken != null)
HttpHeaders.authorizationHeader: 'Bearer $bearerToken'
});
return _httpClient.send(proxyRequest);
}
@override
void close() => _httpClient.close();
StreamedRequest _copyRequest(
BaseRequest original, {
required Uri? url,
required Map<String, String>? headers,
}) {
final request = StreamedRequest(original.method, url ?? original.url)
..contentLength = original.contentLength
..followRedirects = original.followRedirects
..headers.addAll(headers ?? original.headers)
..maxRedirects = original.maxRedirects
..persistentConnection = original.persistentConnection;
original.finalize().listen(
request.sink.add,
onError: request.sink.addError,
onDone: request.sink.close,
cancelOnError: true,
);
return request;
}
}
final unsplashClient = UnsplashClient(
settings: ClientSettings(
// Not used, but required.
credentials: AppCredentials(accessKey: ''),
),
httpClient: ProxyHttpClient(
baseUrl: 'https://api.unsplash.com/',
proxyBaseUrl: 'https://unsplash-auth-proxy.example.com/',
bearerToken: '*********',
),
);
|
Author
|
@blaugold Thank you very much for the code! Really appreciate it. I just tried it and it works just as expected. Maybe asking you first on how to solve my challenge would have saved me some time ;) Do you think it might be helpful to keep your example code somewhere in this repo for other people facing the same challenge? Anyway, feel free to close and reject my PR. It is not required anymore. Or let me know if I should close it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hi Blaugold,
I am using your client library for my Flutter app and since Unsplash does not allow storing the secret and access key on the user devices I built a small cloud authentication proxy.
Your client library currently does not allow to change the URL and omit the credentials parameter. I did some minor changes to
I am new to Dart programming so let me know if there are things I could improve in the code.
Best regards
Oliver