Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Dart] Allow setting an accessToken for OAuth #7528

Merged
merged 2 commits into from
Jan 30, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
dart - allow setting an accessToken for oauth
  • Loading branch information
Jörn Ahrens committed Jan 29, 2018
commit f661f18f79a7cbacc5745c013087ee53d4122d13
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ class ApiClient {

Map<String, String> _defaultHeaderMap = {};
Map<String, Authentication> _authentications = {};
String accessToken;

final _RegList = new RegExp(r'^List<(.*)>$');
final _RegMap = new RegExp(r'^Map<String,(.*)>$');

ApiClient({this.basePath: "{{{basePath}}}"}) {
ApiClient({this.basePath: "{{{basePath}}}", this.accessToken}) {
// Setup authentications (key: authentication name, value: authentication).{{#authMethods}}{{#isBasic}}
_authentications['{{name}}'] = new HttpBasicAuth();{{/isBasic}}{{#isApiKey}}
_authentications['{{name}}'] = new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}");{{/isApiKey}}{{#isOAuth}}
_authentications['{{name}}'] = new OAuth();{{/isOAuth}}{{/authMethods}}
_authentications['{{name}}'] = new OAuth(accessToken: this.accessToken);{{/isOAuth}}{{/authMethods}}
}

void addDefaultHeader(String key, String value) {
Expand Down Expand Up @@ -153,4 +154,13 @@ class ApiClient {
auth.applyToParams(queryParams, headerParams);
});
}

void setAccessToken(String accessToken) {
this.accessToken = accessToken;
_authentications.forEach((key, auth) {
if (auth is OAuth) {
auth.setAccessToken(accessToken);
}
});
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
part of {{pubName}}.api;

class OAuth implements Authentication {
String accessToken;

OAuth({this.accessToken}) {
}

@override
void applyToParams(List<QueryParam> queryParams, Map<String, String> headerParams) {
// TODO: support oauth
if (accessToken != null) {
headerParams["Authorization"] = "Bearer " + accessToken;
}
}

void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
}