Skip to content

Commit

Permalink
Easier way to handle http client.
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianStehle committed Jan 24, 2019
1 parent f848649 commit 0e2564b
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// All rights reserved. Licensed under the MIT license.
// ==========================================================================

using System;
using System.Collections.Generic;

namespace Squidex.ClientLibrary
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
// All rights reserved. Licensed under the MIT license.
// ==========================================================================

using System.Collections.Generic;

namespace Squidex.ClientLibrary
{
public sealed class AssetEntities : EntitiesBase<Asset>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<PackageIconUrl>https://raw.githubusercontent.com/Squidex/squidex/master/media/logo-squared.png</PackageIconUrl>
<PackageLicense>https://github.com/Squidex/squidex/blob/master/LICENSE.txt</PackageLicense>
<PackageProjectUrl>https://github.com/Squidex/squidex/</PackageProjectUrl>
<Version>2.1</Version>
<Version>2.2</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// All rights reserved. Licensed under the MIT license.
// ==========================================================================

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
Expand All @@ -19,8 +18,8 @@ namespace Squidex.ClientLibrary
{
public sealed class SquidexAssetClient : SquidexClientBase
{
public SquidexAssetClient(Uri serviceUrl, string applicationName, IAuthenticator authenticator)
: base(serviceUrl, applicationName, authenticator)
public SquidexAssetClient(string applicationName, HttpClient httpClient)
: base(applicationName, httpClient)
{
}

Expand Down Expand Up @@ -157,12 +156,12 @@ public async Task DeleteAssetAsync(string id)

private string BuildAssetsUrl(string path = "")
{
return $"api/assets/{path}";
return $"assets/{path}";
}

private string BuildAppAssetsUrl(string path = "")
{
return $"api/apps/{ApplicationName}/assets/{path}";
return $"apps/{ApplicationName}/assets/{path}";
}

private static MultipartFormDataContent BuildRequest(string contentName, string contentMimeType, Stream stream)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// All rights reserved. Licensed under the MIT license.
// ==========================================================================

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
Expand All @@ -20,8 +19,8 @@ public sealed class SquidexClient<TEntity, TData> : SquidexClientBase
{
public string SchemaName { get; }

public SquidexClient(Uri serviceUrl, string applicationName, string schemaName, IAuthenticator authenticator)
: base(serviceUrl, applicationName, authenticator)
public SquidexClient(string applicationName, string schemaName, HttpClient httpClient)
: base(applicationName, httpClient)
{
Guard.NotNullOrEmpty(schemaName, nameof(schemaName));

Expand Down Expand Up @@ -73,6 +72,7 @@ public async Task<TEntity> GetAsync(string id, QueryContext context = null)
Guard.NotNullOrEmpty(id, nameof(id));

var response = await RequestAsync(HttpMethod.Get, BuildContentUrl($"{id}/"), context: context);

return await response.Content.ReadAsJsonAsync<TEntity>();
}

Expand Down Expand Up @@ -184,7 +184,7 @@ public async Task DeleteAsync(TEntity entity)

private string BuildContentUrl(string path = "")
{
return $"api/content/{ApplicationName}/{SchemaName}/{path}";
return $"content/{ApplicationName}/{SchemaName}/{path}";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,39 @@
using System.Net.Http;
using System.Threading.Tasks;

using Squidex.ClientLibrary.Utils;

namespace Squidex.ClientLibrary
{
public abstract class SquidexClientBase : IDisposable
{
protected HttpClient HttpClient { get; }

protected Uri ServiceUrl { get; }
private readonly HttpClient httpClient;

protected string ApplicationName { get; }

protected SquidexClientBase(Uri serviceUrl, string applicationName, IAuthenticator authenticator)
protected SquidexClientBase(string applicationName, HttpClient httpClient)
{
Guard.NotNull(serviceUrl, nameof(serviceUrl));
Guard.NotNullOrEmpty(applicationName, nameof(applicationName));
Guard.NotNull(httpClient, nameof(httpClient));

HttpClient = new HttpClient(new AuthenticatingHttpClientHandler(authenticator), true);
ApplicationName = applicationName;
ServiceUrl = serviceUrl;

this.httpClient = httpClient;
}

protected async Task<HttpResponseMessage> RequestAsync(HttpMethod method, string path, HttpContent content = null, QueryContext context = null)
{
var uri = new Uri(ServiceUrl, path);

using (var request = BuildRequest(method, uri, content, context))
using (var request = BuildRequest(method, path, content, context))
{
var response = await HttpClient.SendAsync(request);
var response = await httpClient.SendAsync(request);

await EnsureResponseIsValidAsync(response);

return response;
}
}

protected static HttpRequestMessage BuildRequest(HttpMethod method, Uri uri, HttpContent content, QueryContext context = null)
protected static HttpRequestMessage BuildRequest(HttpMethod method, string path, HttpContent content, QueryContext context = null)
{
var request = new HttpRequestMessage(method, uri);
var request = new HttpRequestMessage(method, path);

if (content != null)
{
Expand Down Expand Up @@ -94,7 +88,7 @@ protected async Task EnsureResponseIsValidAsync(HttpResponseMessage response)

public void Dispose()
{
this.HttpClient?.Dispose();
httpClient?.Dispose();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public IUsersClient CreateUsersClient()

public SquidexAssetClient GetAssetClient()
{
return new SquidexAssetClient(serviceUrl, applicationName, authenticator);
return new SquidexAssetClient(applicationName, CreateHttpClient());
}

public SquidexClient<TEntity, TData> GetClient<TEntity, TData>(string schemaName)
Expand All @@ -133,14 +133,14 @@ public SquidexClient<TEntity, TData> GetClient<TEntity, TData>(string schemaName
{
Guard.NotNullOrEmpty(schemaName, nameof(schemaName));

return new SquidexClient<TEntity, TData>(serviceUrl, applicationName, schemaName, authenticator);
return new SquidexClient<TEntity, TData>(applicationName, schemaName, CreateHttpClient());
}

private HttpClient CreateHttpClient()
{
var url = new Uri(serviceUrl, "/api/");

return new HttpClient(new AuthenticatingHttpClientHandler(authenticator)) { BaseAddress = url };
return new HttpClient(new AuthenticatingHttpClientHandler(authenticator), false) { BaseAddress = url };
}
}
}

0 comments on commit 0e2564b

Please sign in to comment.