Skip to content

Commit

Permalink
* Code cleanup: removed commented code in client library, better erro…
Browse files Browse the repository at this point in the history
…r handling in default http client library,

removed code for UrlProcess request from PubNubCore class,
fixed issues with generate file url
  • Loading branch information
mohitpubnub committed Oct 21, 2024
1 parent fba60eb commit 8c5bb00
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 2,706 deletions.
40 changes: 28 additions & 12 deletions src/Api/PubnubApi/Builder/UriUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
using System.Globalization;
using System.Linq;
using System.Text;
using PubnubApi.EndPoint;

namespace PubnubApi
{
public class UriUtil
public static class UriUtil
{
public static string EncodeUriComponent(string s, PNOperationType type, bool ignoreComma, bool ignoreColon, bool ignorePercent2fEncode)
{
Expand Down Expand Up @@ -77,13 +78,7 @@ public static string EncodeUriComponent(string s, PNOperationType type, bool ign
}
else
{
string escapeChar = System.Uri.EscapeDataString(ch.ToString());
#if NET35 || NET40
if (escapeChar == ch.ToString() && IsUnsafeToEncode(ch, ignoreComma, ignoreColon))
{
escapeChar = string.Format(CultureInfo.InvariantCulture, "%{0}{1}", ToHex(ch / 16), ToHex(ch % 16));
}
#endif
string escapeChar = Uri.EscapeDataString(ch.ToString());
o.Append(escapeChar);
}
}
Expand Down Expand Up @@ -162,7 +157,7 @@ private static int ConvertToUtf32(string s, int index)
}

// Check if the character at index is a high surrogate.
int temp1 = (int)s[index] - HighSurrogateStart;
int temp1 = s[index] - HighSurrogateStart;
if (temp1 >= 0 && temp1 <= 0x7ff)
{
// Found a surrogate char.
Expand All @@ -171,7 +166,7 @@ private static int ConvertToUtf32(string s, int index)
// Found a high surrogate.
if (index < s.Length - 1)
{
int temp2 = (int)s[index + 1] - LowSurrogateStart;
int temp2 = s[index + 1] - LowSurrogateStart;
if (temp2 >= 0 && temp2 <= 0x3ff)
{
// Found a low surrogate.
Expand All @@ -196,12 +191,33 @@ private static int ConvertToUtf32(string s, int index)
}

// Not a high-surrogate or low-surrogate. Genereate the UTF32 value for the BMP characters.
return (int)s[index];
return s[index];
}

public static string BuildQueryString(Dictionary<string, string> queryStringParamMap)
{
return string.Join("&", queryStringParamMap?.OrderBy(kvp => kvp.Key, StringComparer.Ordinal).Select(kvp => string.Format(CultureInfo.InvariantCulture, "{0}={1}", kvp.Key, kvp.Value)).ToArray());
return string.Join("&", queryStringParamMap?.OrderBy(kvp => kvp.Key, StringComparer.Ordinal).Select(kvp => string.Format(CultureInfo.InvariantCulture, "{0}={1}", kvp.Key, kvp.Value)).ToArray() ?? Array.Empty<string>());
}

public static string GetFileUrl(string fileId, string fileName, string channel, PNConfiguration pnConfiguration, Pubnub pubnub, TokenManager tokenmanager)
{
var requestParameters = new RequestParameter()
{
RequestType = Constants.GET,
PathSegment = [
"v1",
"files",
pnConfiguration.SubscribeKey,
"channels",
channel,
"files",
fileId,
fileName
],
Query = new Dictionary<string, string>()
};
var transportRequest = pubnub.transportMiddleware.PreapareTransportRequest(requestParameter:requestParameters, PNOperationType.PNFileUrlOperation);
return transportRequest.RequestUrl;
}
}
}
2,283 changes: 0 additions & 2,283 deletions src/Api/PubnubApi/Builder/UrlRequestBuilder.cs

This file was deleted.

21 changes: 11 additions & 10 deletions src/Api/PubnubApi/EndPoint/Files/GenerateFileUploadUrlOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ public void Execute(PNCallback<PNGenerateFileUploadUrlResult> callback)
throw new ArgumentException("Missing File Name");
}

GenerateFileUploadUrl(this.queryParam, callback);
GenerateFileUploadUrl(callback);
}

public async Task<PNResult<PNGenerateFileUploadUrlResult>> ExecuteAsync()
{
return await GenerateFileUploadUrl(this.queryParam).ConfigureAwait(false);
return await GenerateFileUploadUrl();
}

private void GenerateFileUploadUrl(Dictionary<string, object> externalQueryParam, PNCallback<PNGenerateFileUploadUrlResult> callback)
private void GenerateFileUploadUrl(PNCallback<PNGenerateFileUploadUrlResult> callback)
{
RequestState<PNGenerateFileUploadUrlResult> requestState = new RequestState<PNGenerateFileUploadUrlResult>();
requestState.ResponseType = PNOperationType.PNGenerateFileUploadUrlOperation;
Expand All @@ -77,6 +77,7 @@ private void GenerateFileUploadUrl(Dictionary<string, object> externalQueryParam
var transportResponse = t.Result;
if (transportResponse.Error == null) {
var responseString = Encoding.UTF8.GetString(transportResponse.Content);
requestState.GotJsonResponse = true;
if (!string.IsNullOrEmpty(responseString)) {
List<object> result = ProcessJsonResponse(requestState, responseString);
ProcessResponseCallbacks(result, requestState);
Expand All @@ -93,7 +94,7 @@ private void GenerateFileUploadUrl(Dictionary<string, object> externalQueryParam
});
}

private async Task<PNResult<PNGenerateFileUploadUrlResult>> GenerateFileUploadUrl(Dictionary<string, object> externalQueryParam)
private async Task<PNResult<PNGenerateFileUploadUrlResult>> GenerateFileUploadUrl()
{
PNResult<PNGenerateFileUploadUrlResult> returnValue = new PNResult<PNGenerateFileUploadUrlResult>();
if (string.IsNullOrEmpty(sendFileName)) {
Expand All @@ -110,20 +111,21 @@ private async Task<PNResult<PNGenerateFileUploadUrlResult>> GenerateFileUploadUr
requestState.EndPointOperation = this;

var requestParameter = CreateRequestParameter();
Tuple<string, PNStatus> JsonAndStatusTuple;
var transportRequest = PubnubInstance.transportMiddleware.PreapareTransportRequest(requestParameter: requestParameter, operationType: PNOperationType.PNGenerateFileUploadUrlOperation);
var transportResponse = await PubnubInstance.transportMiddleware.Send(transportRequest: transportRequest);
if (transportResponse.Error == null) {
var responseString = Encoding.UTF8.GetString(transportResponse.Content);
PNStatus errorStatus = GetStatusIfError(requestState, responseString);
Tuple<string, PNStatus> jsonAndStatusTuple;
if (errorStatus == null) {
requestState.GotJsonResponse = true;
PNStatus status = new StatusBuilder(config, jsonLibrary).CreateStatusResponse(requestState.ResponseType, PNStatusCategory.PNAcknowledgmentCategory, requestState, transportResponse.StatusCode, null);
JsonAndStatusTuple = new Tuple<string, PNStatus>(responseString, status);
jsonAndStatusTuple = new Tuple<string, PNStatus>(responseString, status);
} else {
JsonAndStatusTuple = new Tuple<string, PNStatus>(string.Empty, errorStatus);
jsonAndStatusTuple = new Tuple<string, PNStatus>(string.Empty, errorStatus);
}
returnValue.Status = JsonAndStatusTuple.Item2;
string json = JsonAndStatusTuple.Item1;
returnValue.Status = jsonAndStatusTuple.Item2;
string json = jsonAndStatusTuple.Item1;
if (!string.IsNullOrEmpty(json)) {
List<object> resultList = ProcessJsonResponse(requestState, json);
ResponseBuilder responseBuilder = new ResponseBuilder(config, jsonLibrary, pubnubLog);
Expand Down Expand Up @@ -162,7 +164,6 @@ private RequestParameter CreateRequestParameter()
}
}
}

Dictionary<string, object> messageEnvelope = new Dictionary<string, object>();
if (!string.IsNullOrEmpty(sendFileName)) {
messageEnvelope.Add("name", sendFileName);
Expand Down
6 changes: 2 additions & 4 deletions src/Api/PubnubApi/EventEngine/Common/EventEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,8 @@ public void EmitEvent<T>(object e)
Dictionary<string, object> fileObjDic = jsonLibrary.ConvertToDictionaryObject(pnMsgObjDic["file"]);
if (fileObjDic != null && fileObjDic.ContainsKey("id") && fileObjDic.ContainsKey("name")) {
fileMessage.File = new PNFile { Id = fileObjDic["id"].ToString(), Name = fileObjDic["name"].ToString() };

IUrlRequestBuilder urlBuilder = new UrlRequestBuilder(configuration, jsonLibrary, null, log, tokenManager, (instance != null) ? instance.InstanceId : "");
Uri fileUrlRequest = urlBuilder.BuildGetFileUrlOrDeleteReqest("GET", "", fileMessage.Channel, fileMessage.File.Id, fileMessage.File.Name, null, PNOperationType.PNGenerateFileUploadUrlOperation);
fileMessage.File.Url = fileUrlRequest.ToString();
fileMessage.File.Url = UriUtil.GetFileUrl(fileName: fileMessage.File.Name, fileId: fileMessage.File.Id, channel:fileMessage.Channel,
pnConfiguration:configuration, pubnub:instance, tokenmanager:tokenManager);
}
}
} else {
Expand Down
Loading

0 comments on commit 8c5bb00

Please sign in to comment.