Skip to content

Commit

Permalink
MTM-35043 : Fixed Regex and used forceinitalhost to use path
Browse files Browse the repository at this point in the history
  • Loading branch information
Pallav committed Nov 18, 2020
1 parent 5e7531b commit d3b3dcd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
4 changes: 2 additions & 2 deletions REST-SDK/src/Cumulocity.SDK.Client/CumulocityHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Cumulocity.SDK.Client
#pragma warning disable CS0169
public class CumulocityHttpClient : HttpClient
{
private readonly Regex hostPattern = new Regex(@"((http|https):\\/\\/.+?)(\\/|\\?|$)");
private readonly Regex hostPattern = new Regex("((http|https):\\/\\/.+?)(\\/|\\?|$)");

private PlatformParameters platformParameters;
private readonly SerializerSettings _serializerSettings;
Expand Down Expand Up @@ -52,7 +52,7 @@ public CumulocityHttpClient resource(string path)
return this;
}

protected internal virtual string resolvePath(string path)
public string resolvePath(string path)
{
if (path.StartsWith("/", StringComparison.Ordinal)) path = InitialHost + path;
return platformParameters.ForceInitialHost ? insertInitialHost(path) : path;
Expand Down
3 changes: 1 addition & 2 deletions REST-SDK/src/Cumulocity.SDK.Client/PlatformParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public PlatformParameters()
{
proxyPort = -1;
requireResponseBody_Renamed = true;
forceInitialHost = false;
PageSize = 5;
}

Expand All @@ -44,7 +43,6 @@ public PlatformParameters()
{
proxyPort = -1;
requireResponseBody_Renamed = true;
forceInitialHost = false;
PageSize = 5;
PageSize = pageSize;
this.clientConfiguration = clientConfiguration;
Expand Down Expand Up @@ -119,6 +117,7 @@ public virtual string RequestOrigin
private void setMandatoryFields(string host, CumulocityCredentials credentials)
{
if (host[host.Length - 1] != '/') host = $"{host}/";
if (host.Equals("http://cumulocity:8111/")) forceInitialHost = true;

this.host = host;
tenantId = credentials.TenantId;
Expand Down
13 changes: 13 additions & 0 deletions REST-SDK/src/Cumulocity.SDK.Client/RestConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public T Get<T>(string path, MediaType mediaType, Type responseType)

public async Task<Stream> GetStream(string path, MediaType aPPLICATION_OCTET_STREAM_TYPE)
{
path = Client.resolvePath(path);
return await client.GetStreamAsync(path);
}

Expand Down Expand Up @@ -132,6 +133,7 @@ public async Task<T> PutAsync<T>(string path, CumulocityMediaType mediaType, T r
private async Task<T> sendAsyncRequest<T>(String method, String path, CumulocityMediaType mediaType,T representation)
where T : IResourceRepresentation
{
path = Client.resolvePath(path);
var bufferRequestService = PlatformParameters.BufferRequestService;
var request = BufferedRequest.create(method, path, mediaType, representation);
return (T) await bufferRequestService.Create(request);
Expand All @@ -151,6 +153,7 @@ public void Delete(string path)

private Task<HttpResponseMessage> getClientResponse(string path, MediaType mediaType)
{
path = Client.resolvePath(path);
var request = new HttpRequestMessage
{
Method = HttpMethod.Get,
Expand All @@ -167,6 +170,7 @@ private Task<HttpResponseMessage> getClientResponse(string path, MediaType media

private Task<HttpResponseMessage> getClientResponse(string path, CumulocityMediaType mediaType)
{
path = Client.resolvePath(path);
var request = new HttpRequestMessage
{
Method = HttpMethod.Get,
Expand All @@ -184,6 +188,7 @@ private Task<HttpResponseMessage> getClientResponse(string path, CumulocityMedia
private Task<HttpResponseMessage> httpStreamPost<T>(string path, CumulocityMediaType contentType,
CumulocityMediaType accept, Stream content, T representation)
{
path = Client.resolvePath(path);
MultipartFormDataContent multipartContent = new MultipartFormDataContent();
ByteArrayContent byteContent;

Expand Down Expand Up @@ -214,6 +219,7 @@ private Task<HttpResponseMessage> getClientResponse(string path, CumulocityMedia

private Task<HttpResponseMessage> httpPostText<T>(string path, String content, Type representation)
{
path = Client.resolvePath(path);
var stringContent = new StringContent(content, Encoding.UTF8).Replace(MediaType.TEXT_PLAIN);

var request = new HttpRequestMessage
Expand All @@ -234,6 +240,7 @@ private Task<HttpResponseMessage> httpPostText<T>(string path, String content, T

private Task<HttpResponseMessage> httpPutText<T>(string path, String content, Type representation)
{
path = Client.resolvePath(path);
var stringContent = new StringContent(content, Encoding.UTF8).Replace(MediaType.TEXT_PLAIN);

var request = new HttpRequestMessage
Expand All @@ -254,6 +261,7 @@ private Task<HttpResponseMessage> httpPutText<T>(string path, String content, Ty

private Task<HttpResponseMessage> httpPutStream<T>(string path, Stream content, Type representation)
{
path = Client.resolvePath(path);
var streamContent = new StreamContent(content);

MultipartFormDataContent multipartContent = new MultipartFormDataContent();
Expand All @@ -278,6 +286,7 @@ private Task<HttpResponseMessage> httpPutStream<T>(string path, Stream content,
private Task<HttpResponseMessage> httpPost<T>(string path, MediaType contentType,
T representation)
{
path = Client.resolvePath(path);
var json = JsonConvert.SerializeObject(representation,
new JsonSerializerSettings
{
Expand All @@ -302,6 +311,7 @@ private Task<HttpResponseMessage> httpPutStream<T>(string path, Stream content,
private Task<HttpResponseMessage> httpPost<T>(string path, CumulocityMediaType contentType,
CumulocityMediaType accept, T representation)
{
path = Client.resolvePath(path);
var json = JsonConvert.SerializeObject(representation,
new JsonSerializerSettings
{
Expand Down Expand Up @@ -329,6 +339,7 @@ private Task<HttpResponseMessage> httpPutStream<T>(string path, Stream content,

private Task<HttpResponseMessage> deleteClientResponse(string path)
{
path = Client.resolvePath(path);
var request = new HttpRequestMessage
{
Method = HttpMethod.Delete,
Expand All @@ -344,6 +355,7 @@ private Task<HttpResponseMessage> deleteClientResponse(string path)

private Task<HttpResponseMessage> putClientResponse<T>(string path, CumulocityMediaType mediaType, T representation)
{
path = Client.resolvePath(path);
var json = JsonConvert.SerializeObject(representation,
new JsonSerializerSettings
{
Expand Down Expand Up @@ -440,6 +452,7 @@ public T postFile<T>(string path, ManagedObjectRepresentation container, byte[]

private Task<HttpResponseMessage> httpPostFile<T>(string path, byte[] bytes, ManagedObjectRepresentation container)
{
path = Client.resolvePath(path);
var stringContentObject = new StringContent(JsonConvert.SerializeObject(container, new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
Expand Down

0 comments on commit d3b3dcd

Please sign in to comment.