-
Notifications
You must be signed in to change notification settings - Fork 20
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
feat: Add Authenticated Datafile Support #222
Merged
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c6bddf7
adf implementation
msohailhussain 1530007
fixed unit tests
msohailhussain 8be280a
injecting httpclient
msohailhussain ab0ed53
fixed unit tests
msohailhussain 9b9f3f2
fixed unit tests
msohailhussain e13fd20
added callback
msohailhussain d1f41a3
added callback
msohailhussain 080a390
revised names
msohailhussain 3b239fb
added in changelog
msohailhussain 854490f
renamed variable
msohailhussain bd9b366
renamed to DatafileAccessToken
msohailhussain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright 2019, Optimizely | ||
* Copyright 2019-2020, Optimizely | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
|
@@ -28,34 +28,64 @@ public class HttpProjectConfigManager : PollingProjectConfigManager | |
{ | ||
private string Url; | ||
private string LastModifiedSince = string.Empty; | ||
|
||
private string AuthenticatedDatafileToken = string.Empty; | ||
private HttpProjectConfigManager(TimeSpan period, string url, TimeSpan blockingTimeout, bool autoUpdate, ILogger logger, IErrorHandler errorHandler) | ||
: base(period, blockingTimeout, autoUpdate, logger, errorHandler) | ||
{ | ||
Url = url; | ||
} | ||
|
||
private HttpProjectConfigManager(TimeSpan period, string url, TimeSpan blockingTimeout, bool autoUpdate, ILogger logger, IErrorHandler errorHandler, string authDatafileToken) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
: this(period, url, blockingTimeout, autoUpdate, logger, errorHandler) | ||
{ | ||
AuthenticatedDatafileToken = authDatafileToken; | ||
} | ||
|
||
public Task OnReady() | ||
{ | ||
return CompletableConfigManager.Task; | ||
} | ||
|
||
#if !NET40 && !NET35 | ||
private static System.Net.Http.HttpClient Client; | ||
static HttpProjectConfigManager() | ||
// HttpClient wrapper class which can be used to mock HttpClient for unit testing. | ||
public class HttpClient | ||
{ | ||
Client = new System.Net.Http.HttpClient(GetHttpClientHandler()); | ||
} | ||
private System.Net.Http.HttpClient Client; | ||
|
||
public static System.Net.Http.HttpClientHandler GetHttpClientHandler() | ||
{ | ||
var handler = new System.Net.Http.HttpClientHandler() { | ||
AutomaticDecompression = System.Net.DecompressionMethods.GZip | System.Net.DecompressionMethods.Deflate | ||
}; | ||
public HttpClient() | ||
{ | ||
Client = new System.Net.Http.HttpClient(GetHttpClientHandler()); | ||
} | ||
|
||
return handler; | ||
public HttpClient(System.Net.Http.HttpClient httpClient) : this() | ||
{ | ||
if (httpClient != null) { | ||
Client = httpClient; | ||
} | ||
} | ||
|
||
public static System.Net.Http.HttpClientHandler GetHttpClientHandler() | ||
{ | ||
var handler = new System.Net.Http.HttpClientHandler() { | ||
AutomaticDecompression = System.Net.DecompressionMethods.GZip | System.Net.DecompressionMethods.Deflate | ||
}; | ||
|
||
return handler; | ||
} | ||
|
||
public virtual Task<System.Net.Http.HttpResponseMessage> SendAsync(System.Net.Http.HttpRequestMessage httpRequestMessage) | ||
{ | ||
return Client.SendAsync(httpRequestMessage); | ||
} | ||
} | ||
|
||
private static HttpClient Client; | ||
|
||
static HttpProjectConfigManager() | ||
{ | ||
Client = new HttpClient(); | ||
} | ||
|
||
private string GetRemoteDatafileResponse() | ||
{ | ||
var request = new System.Net.Http.HttpRequestMessage { | ||
|
@@ -67,6 +97,10 @@ private string GetRemoteDatafileResponse() | |
if (!string.IsNullOrEmpty(LastModifiedSince)) | ||
request.Headers.Add("If-Modified-Since", LastModifiedSince); | ||
|
||
if (!string.IsNullOrEmpty(AuthenticatedDatafileToken)) { | ||
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", AuthenticatedDatafileToken); | ||
} | ||
|
||
var httpResponse = Client.SendAsync(request); | ||
httpResponse.Wait(); | ||
|
||
|
@@ -136,11 +170,12 @@ public class Builder | |
private readonly TimeSpan DEFAULT_PERIOD = TimeSpan.FromMinutes(5); | ||
private readonly TimeSpan DEFAULT_BLOCKINGOUT_PERIOD = TimeSpan.FromSeconds(15); | ||
private readonly string DEFAULT_FORMAT = "https://cdn.optimizely.com/datafiles/{0}.json"; | ||
|
||
private readonly string DEFAULT_AUTHENTICATED_DATAFILE_FORMAT = "https://config.optimizely.com/datafiles/auth/{0}.json"; | ||
private string Datafile; | ||
private string AuthenticatedDatafileToken; | ||
private string SdkKey; | ||
private string Url; | ||
private string Format; | ||
private string Format; | ||
private ILogger Logger; | ||
private IErrorHandler ErrorHandler; | ||
private TimeSpan Period; | ||
|
@@ -174,6 +209,14 @@ public Builder WithSdkKey(string sdkKey) | |
|
||
return this; | ||
} | ||
#if !NET40 && !NET35 | ||
public Builder WithAuthToken(string authToken) | ||
{ | ||
this.AuthenticatedDatafileToken = authToken; | ||
|
||
return this; | ||
} | ||
#endif | ||
|
||
public Builder WithUrl(string url) | ||
{ | ||
|
@@ -260,15 +303,18 @@ public HttpProjectConfigManager Build(bool defer) | |
ErrorHandler = new DefaultErrorHandler(); | ||
|
||
if (string.IsNullOrEmpty(Format)) { | ||
Format = DEFAULT_FORMAT; | ||
} | ||
|
||
if (string.IsNullOrEmpty(Url) && string.IsNullOrEmpty(SdkKey)) | ||
{ | ||
ErrorHandler.HandleError(new Exception("SdkKey cannot be null")); | ||
if (string.IsNullOrEmpty(AuthenticatedDatafileToken)) { | ||
Format = DEFAULT_FORMAT; | ||
} else { | ||
Format = DEFAULT_AUTHENTICATED_DATAFILE_FORMAT; | ||
} | ||
} | ||
else if (!string.IsNullOrEmpty(SdkKey)) | ||
{ | ||
|
||
if (string.IsNullOrEmpty(Url)) { | ||
if (string.IsNullOrEmpty(SdkKey)) { | ||
ErrorHandler.HandleError(new Exception("SdkKey cannot be null")); | ||
} | ||
Url = string.Format(Format, SdkKey); | ||
} | ||
|
||
|
@@ -290,7 +336,7 @@ public HttpProjectConfigManager Build(bool defer) | |
} | ||
|
||
|
||
configManager = new HttpProjectConfigManager(Period, Url, BlockingTimeoutSpan, AutoUpdate, Logger, ErrorHandler); | ||
configManager = new HttpProjectConfigManager(Period, Url, BlockingTimeoutSpan, AutoUpdate, Logger, ErrorHandler, AuthenticatedDatafileToken); | ||
|
||
if (Datafile != null) | ||
{ | ||
|
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Call it datafileAuthToken