-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integration of TransportLayer with all endpoints except File and Subs…
…cribe operations
- Loading branch information
1 parent
082829e
commit 976d9b3
Showing
72 changed files
with
11,815 additions
and
11,093 deletions.
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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
929 changes: 408 additions & 521 deletions
929
src/Api/PubnubApi/EndPoint/Access/GrantTokenOperation.cs
Large diffs are not rendered by default.
Oops, something went wrong.
302 changes: 158 additions & 144 deletions
302
src/Api/PubnubApi/EndPoint/Access/RevokeTokenOperation.cs
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,151 +1,165 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
#if !NET35 && !NET40 | ||
using System.Collections.Concurrent; | ||
#endif | ||
using System.Text; | ||
|
||
namespace PubnubApi.EndPoint | ||
{ | ||
public class RevokeTokenOperation : PubnubCoreBase | ||
{ | ||
private readonly PNConfiguration config; | ||
private readonly IJsonPluggableLibrary jsonLibrary; | ||
private readonly IPubnubUnitTest unit; | ||
private readonly IPubnubLog pubnubLog; | ||
|
||
private string pnToken = string.Empty; | ||
private PNCallback<PNAccessManagerRevokeTokenResult> savedCallbackRevokeToken; | ||
private Dictionary<string, object> queryParam; | ||
|
||
public RevokeTokenOperation(PNConfiguration pubnubConfig, IJsonPluggableLibrary jsonPluggableLibrary, IPubnubUnitTest pubnubUnit, IPubnubLog log, EndPoint.TokenManager tokenManager, Pubnub instance) : base(pubnubConfig, jsonPluggableLibrary, pubnubUnit, log, tokenManager, instance) | ||
{ | ||
config = pubnubConfig; | ||
jsonLibrary = jsonPluggableLibrary; | ||
unit = pubnubUnit; | ||
pubnubLog = log; | ||
PubnubInstance = instance; | ||
|
||
InitializeDefaultVariableObjectStates(); | ||
} | ||
|
||
public RevokeTokenOperation Token(string tokenToBeRevoked) | ||
{ | ||
this.pnToken = tokenToBeRevoked; | ||
return this; | ||
} | ||
|
||
public RevokeTokenOperation QueryParam(Dictionary<string, object> customQueryParam) | ||
{ | ||
this.queryParam = customQueryParam; | ||
return this; | ||
} | ||
|
||
public void Execute(PNCallback<PNAccessManagerRevokeTokenResult> callback) | ||
{ | ||
#if NETFX_CORE || WINDOWS_UWP || UAP || NETSTANDARD10 || NETSTANDARD11 || NETSTANDARD12 | ||
Task.Factory.StartNew(() => | ||
{ | ||
this.savedCallbackRevokeToken = callback; | ||
RevokeAccess(callback); | ||
}, CancellationToken.None, TaskCreationOptions.PreferFairness, TaskScheduler.Default).ConfigureAwait(false); | ||
#else | ||
new Thread(() => | ||
{ | ||
this.savedCallbackRevokeToken = callback; | ||
RevokeAccess(callback); | ||
}) | ||
{ IsBackground = true }.Start(); | ||
#endif | ||
} | ||
|
||
public async Task<PNResult<PNAccessManagerRevokeTokenResult>> ExecuteAsync() | ||
{ | ||
return await RevokeAccess().ConfigureAwait(false); | ||
} | ||
|
||
internal void Retry() | ||
{ | ||
#if NETFX_CORE || WINDOWS_UWP || UAP || NETSTANDARD10 || NETSTANDARD11 || NETSTANDARD12 | ||
Task.Factory.StartNew(() => | ||
{ | ||
RevokeAccess(savedCallbackRevokeToken); | ||
}, CancellationToken.None, TaskCreationOptions.PreferFairness, TaskScheduler.Default).ConfigureAwait(false); | ||
#else | ||
new Thread(() => | ||
{ | ||
RevokeAccess(savedCallbackRevokeToken); | ||
}) | ||
{ IsBackground = true }.Start(); | ||
#endif | ||
} | ||
|
||
internal void RevokeAccess(PNCallback<PNAccessManagerRevokeTokenResult> callback) | ||
{ | ||
if (string.IsNullOrEmpty(config.SecretKey) || string.IsNullOrEmpty(config.SecretKey.Trim()) || config.SecretKey.Length <= 0) | ||
{ | ||
throw new MissingMemberException("Invalid secret key"); | ||
} | ||
|
||
RequestState<PNAccessManagerRevokeTokenResult> requestState = new RequestState<PNAccessManagerRevokeTokenResult>(); | ||
requestState.ResponseType = PNOperationType.PNAccessManagerRevokeToken; | ||
requestState.PubnubCallback = callback; | ||
requestState.Reconnect = false; | ||
requestState.EndPointOperation = this; | ||
|
||
string requestMethodName = "DELETE"; | ||
IUrlRequestBuilder urlBuilder = new UrlRequestBuilder(config, jsonLibrary, unit, pubnubLog, null, (PubnubInstance != null) ? PubnubInstance.InstanceId : ""); | ||
Uri request = urlBuilder.BuildRevokeV3AccessRequest(requestMethodName, null, pnToken, this.queryParam); | ||
|
||
UrlProcessRequest(request, requestState, false).ContinueWith(r => | ||
{ | ||
string json = r.Result.Item1; | ||
if (!string.IsNullOrEmpty(json)) | ||
{ | ||
List<object> result = ProcessJsonResponse(requestState, json); | ||
ProcessResponseCallbacks(result, requestState); | ||
} | ||
}, TaskContinuationOptions.ExecuteSynchronously).Wait(); | ||
} | ||
|
||
internal async Task<PNResult<PNAccessManagerRevokeTokenResult>> RevokeAccess() | ||
{ | ||
if (string.IsNullOrEmpty(config.SecretKey) || string.IsNullOrEmpty(config.SecretKey.Trim()) || config.SecretKey.Length <= 0) | ||
{ | ||
throw new MissingMemberException("Invalid secret key"); | ||
} | ||
|
||
PNResult<PNAccessManagerRevokeTokenResult> ret = new PNResult<PNAccessManagerRevokeTokenResult>(); | ||
|
||
string requestMethodName = "DELETE"; | ||
IUrlRequestBuilder urlBuilder = new UrlRequestBuilder(config, jsonLibrary, unit, pubnubLog, null, (PubnubInstance != null) ? PubnubInstance.InstanceId : ""); | ||
Uri request = urlBuilder.BuildRevokeV3AccessRequest(requestMethodName, null, pnToken, queryParam); | ||
|
||
RequestState<PNAccessManagerRevokeTokenResult> requestState = new RequestState<PNAccessManagerRevokeTokenResult>(); | ||
requestState.ResponseType = PNOperationType.PNAccessManagerRevokeToken; | ||
requestState.Reconnect = false; | ||
requestState.EndPointOperation = this; | ||
|
||
Tuple<string, PNStatus> JsonAndStatusTuple = await UrlProcessRequest(request, requestState, false).ConfigureAwait(false); | ||
ret.Status = JsonAndStatusTuple.Item2; | ||
string json = JsonAndStatusTuple.Item1; | ||
if (!string.IsNullOrEmpty(json)) | ||
{ | ||
List<object> resultList = ProcessJsonResponse(requestState, json); | ||
if (resultList != null && resultList.Count > 0) | ||
{ | ||
ResponseBuilder responseBuilder = new ResponseBuilder(config, jsonLibrary, pubnubLog); | ||
PNAccessManagerRevokeTokenResult responseResult = responseBuilder.JsonToObject<PNAccessManagerRevokeTokenResult>(resultList, true); | ||
if (responseResult != null) | ||
{ | ||
ret.Result = responseResult; | ||
} | ||
} | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
} | ||
public class RevokeTokenOperation : PubnubCoreBase | ||
{ | ||
private readonly PNConfiguration config; | ||
private readonly IJsonPluggableLibrary jsonLibrary; | ||
private readonly IPubnubUnitTest unit; | ||
private readonly IPubnubLog pubnubLog; | ||
|
||
private string pnToken = string.Empty; | ||
private PNCallback<PNAccessManagerRevokeTokenResult> savedCallbackRevokeToken; | ||
private Dictionary<string, object> queryParam; | ||
|
||
public RevokeTokenOperation(PNConfiguration pubnubConfig, IJsonPluggableLibrary jsonPluggableLibrary, IPubnubUnitTest pubnubUnit, IPubnubLog log, EndPoint.TokenManager tokenManager, Pubnub instance) : base(pubnubConfig, jsonPluggableLibrary, pubnubUnit, log, tokenManager, instance) | ||
{ | ||
config = pubnubConfig; | ||
jsonLibrary = jsonPluggableLibrary; | ||
unit = pubnubUnit; | ||
pubnubLog = log; | ||
PubnubInstance = instance; | ||
|
||
InitializeDefaultVariableObjectStates(); | ||
} | ||
|
||
public RevokeTokenOperation Token(string tokenToBeRevoked) | ||
{ | ||
this.pnToken = tokenToBeRevoked; | ||
return this; | ||
} | ||
|
||
public RevokeTokenOperation QueryParam(Dictionary<string, object> customQueryParam) | ||
{ | ||
this.queryParam = customQueryParam; | ||
return this; | ||
} | ||
|
||
public void Execute(PNCallback<PNAccessManagerRevokeTokenResult> callback) | ||
{ | ||
this.savedCallbackRevokeToken = callback; | ||
RevokeAccess(callback); | ||
} | ||
|
||
public async Task<PNResult<PNAccessManagerRevokeTokenResult>> ExecuteAsync() | ||
{ | ||
return await RevokeAccess(); | ||
} | ||
|
||
internal void Retry() | ||
{ | ||
RevokeAccess(savedCallbackRevokeToken); | ||
} | ||
|
||
internal void RevokeAccess(PNCallback<PNAccessManagerRevokeTokenResult> callback) | ||
{ | ||
if (string.IsNullOrEmpty(config.SecretKey) || string.IsNullOrEmpty(config.SecretKey.Trim()) || config.SecretKey.Length <= 0) { | ||
throw new MissingMemberException("Invalid secret key"); | ||
} | ||
|
||
RequestState<PNAccessManagerRevokeTokenResult> requestState = new RequestState<PNAccessManagerRevokeTokenResult>(); | ||
requestState.ResponseType = PNOperationType.PNAccessManagerRevokeToken; | ||
requestState.PubnubCallback = callback; | ||
requestState.Reconnect = false; | ||
requestState.EndPointOperation = this; | ||
|
||
var requestParameter = CreateRequestParameter(); | ||
var transportRequest = PubnubInstance.transportMiddleware.PreapareTransportRequest(requestParameter: requestParameter, operationType: PNOperationType.PNAccessManagerRevokeToken); | ||
PubnubInstance.transportMiddleware.Send(transportRequest: transportRequest).ContinueWith(t => { | ||
var transportResponse = t.Result; | ||
if (transportResponse.Error == null) { | ||
var responseString = Encoding.UTF8.GetString(t.Result.Content); | ||
if (!string.IsNullOrEmpty(responseString)) { | ||
List<object> result = ProcessJsonResponse(requestState, responseString); | ||
ProcessResponseCallbacks(result, requestState); | ||
} | ||
} else { | ||
int statusCode = PNStatusCodeHelper.GetHttpStatusCode(t.Result.Error.Message); | ||
PNStatusCategory category = PNStatusCategoryHelper.GetPNStatusCategory(statusCode, t.Result.Error.Message); | ||
PNStatus status = new StatusBuilder(config, jsonLibrary).CreateStatusResponse(PNOperationType.PNAccessManagerRevokeToken, category, requestState, statusCode, new PNException(t.Result.Error.Message, t.Result.Error)); | ||
requestState.PubnubCallback.OnResponse(default(PNAccessManagerRevokeTokenResult), status); | ||
} | ||
}); | ||
} | ||
|
||
internal async Task<PNResult<PNAccessManagerRevokeTokenResult>> RevokeAccess() | ||
{ | ||
if (string.IsNullOrEmpty(config.SecretKey) || string.IsNullOrEmpty(config.SecretKey.Trim()) || config.SecretKey.Length <= 0) { | ||
throw new MissingMemberException("Invalid secret key"); | ||
} | ||
PNResult<PNAccessManagerRevokeTokenResult> returnValue = new PNResult<PNAccessManagerRevokeTokenResult>(); | ||
RequestState<PNAccessManagerRevokeTokenResult> requestState = new RequestState<PNAccessManagerRevokeTokenResult>(); | ||
requestState.ResponseType = PNOperationType.PNAccessManagerRevokeToken; | ||
requestState.Reconnect = false; | ||
requestState.EndPointOperation = this; | ||
Tuple<string, PNStatus> JsonAndStatusTuple; | ||
|
||
var requestParameter = CreateRequestParameter(); | ||
var transportRequest = PubnubInstance.transportMiddleware.PreapareTransportRequest(requestParameter: requestParameter, operationType: PNOperationType.PNAccessManagerRevokeToken); | ||
var transportResponse = await PubnubInstance.transportMiddleware.Send(transportRequest: transportRequest); | ||
if (transportResponse.Error == null) { | ||
var responseString = Encoding.UTF8.GetString(transportResponse.Content); | ||
PNStatus errorStatus = GetStatusIfError(requestState, responseString); | ||
if (errorStatus == null) { | ||
PNStatus status = new StatusBuilder(config, jsonLibrary).CreateStatusResponse(requestState.ResponseType, PNStatusCategory.PNAcknowledgmentCategory, requestState, transportResponse.StatusCode, null); | ||
JsonAndStatusTuple = new Tuple<string, PNStatus>(responseString, status); | ||
} else { | ||
JsonAndStatusTuple = new Tuple<string, PNStatus>("", errorStatus); | ||
} | ||
returnValue.Status = JsonAndStatusTuple.Item2; | ||
string json = JsonAndStatusTuple.Item1; | ||
if (!string.IsNullOrEmpty(json)) { | ||
List<object> resultList = ProcessJsonResponse(requestState, json); | ||
if (resultList != null && resultList.Count > 0) { | ||
ResponseBuilder responseBuilder = new ResponseBuilder(config, jsonLibrary, pubnubLog); | ||
PNAccessManagerRevokeTokenResult responseResult = responseBuilder.JsonToObject<PNAccessManagerRevokeTokenResult>(resultList, true); | ||
if (responseResult != null) { | ||
returnValue.Result = responseResult; | ||
} | ||
} | ||
} | ||
} else { | ||
int statusCode = PNStatusCodeHelper.GetHttpStatusCode(transportResponse.Error.Message); | ||
PNStatusCategory category = PNStatusCategoryHelper.GetPNStatusCategory(statusCode, transportResponse.Error.Message); | ||
PNStatus status = new StatusBuilder(config, jsonLibrary).CreateStatusResponse(PNOperationType.PNAccessManagerRevokeToken, category, requestState, statusCode, new PNException(transportResponse.Error.Message, transportResponse.Error)); | ||
returnValue.Status = status; | ||
} | ||
return returnValue; | ||
} | ||
|
||
private RequestParameter CreateRequestParameter() | ||
{ | ||
List<string> pathSegments = new List<string>() { | ||
"v3", | ||
"pam", | ||
config.SubscribeKey, | ||
"grant", | ||
pnToken | ||
}; | ||
|
||
Dictionary<string, string> requestQueryStringParams = new Dictionary<string, string>(); | ||
|
||
if (queryParam != null && queryParam.Count > 0) { | ||
foreach (KeyValuePair<string, object> kvp in queryParam) { | ||
if (!requestQueryStringParams.ContainsKey(kvp.Key)) { | ||
requestQueryStringParams.Add(kvp.Key, UriUtil.EncodeUriComponent(kvp.Value.ToString(), PNOperationType.ChannelGroupRevokeAccess, false, false, false)); | ||
} | ||
} | ||
} | ||
|
||
string queryString = UriUtil.BuildQueryString(requestQueryStringParams); | ||
|
||
var requestParameter = new RequestParameter() { | ||
RequestType = Constants.DELETE, | ||
PathSegment = pathSegments, | ||
Query = requestQueryStringParams | ||
}; | ||
|
||
return requestParameter; | ||
} | ||
} | ||
} |
Oops, something went wrong.