Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
cleftheris committed Mar 29, 2024
1 parent fbbaf40 commit 5700af8
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 12 deletions.
13 changes: 11 additions & 2 deletions src/Incontrl.Sdk/Abstractions/IOrganisationApi.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Incontrl.Sdk.Models;
Expand Down Expand Up @@ -39,6 +40,14 @@ public interface IOrganisationApi
/// <param name="fileName">The name of the file.</param>
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <returns>Returns the task object representing the asynchronous operation.</returns>
Task LogoUploadAsync(Stream fileContent, string fileName, CancellationToken cancellationToken = default);
Task<Link> LogoUploadAsync(Stream fileContent, string fileName, CancellationToken cancellationToken = default);

/// <summary>
/// Updates the organization logo url with an external absolute link.
/// </summary>
/// <param name="absoluteUri">The absolute link</param>
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <returns>Returns the task object representing the asynchronous operation.</returns>
Task<Link> LogoSetUriAsync(Uri absoluteUri, CancellationToken cancellationToken = default);
}
}
13 changes: 11 additions & 2 deletions src/Incontrl.Sdk/Abstractions/ISubscriptionCompanyApi.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Incontrl.Sdk.Models;
Expand Down Expand Up @@ -32,6 +33,14 @@ public interface ISubscriptionCompanyApi
/// <param name="fileName">The name of the file.</param>
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <returns>Returns the task object representing the asynchronous operation.</returns>
Task LogoUploadAsync(Stream fileContent, string fileName, CancellationToken cancellationToken = default);
Task<Link> LogoUploadAsync(Stream fileContent, string fileName, CancellationToken cancellationToken = default);

/// <summary>
/// Updates the company logo url with an external absolute link.
/// </summary>
/// <param name="absoluteUri">The absolute link</param>
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <returns>Returns the task object representing the asynchronous operation.</returns>
Task<Link> LogoSetUriAsync(Uri absoluteUri, CancellationToken cancellationToken = default);
}
}
2 changes: 1 addition & 1 deletion src/Incontrl.Sdk/Incontrl.Sdk.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Description>Incontrl.io .Net SDK</Description>
<Title>Incontrl.Net Class Library</Title>
<Copyright>Copyright (c) 2017 Indice</Copyright>
<VersionPrefix>3.0.7</VersionPrefix>
<VersionPrefix>3.0.8</VersionPrefix>
<!--<VersionSuffix>beta1</VersionSuffix>-->
<Authors>Constantinos Leftheris, Giorgos Manoltzas</Authors>
<TargetFrameworks>net461;netstandard2.0</TargetFrameworks>
Expand Down
14 changes: 14 additions & 0 deletions src/Incontrl.Sdk/Models/Link.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Incontrl.Sdk.Models
{
public class Link
{
public string Name { get; set; }
public string Uri { get; set; }
}
}
32 changes: 32 additions & 0 deletions src/Incontrl.Sdk/Services/ClientBase.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using Incontrl.Sdk.Http;
using Incontrl.Sdk.Models;
using Indice.Services;
using Indice.Types;
using Polly;

namespace Incontrl.Sdk.Services
{
Expand Down Expand Up @@ -88,6 +92,34 @@ public async Task PostFileAsync(string requestUri, Stream fileContent, string fi
await _httpClient.PostAsync(requestUri, formDataContent, cancellationToken);
}
}
public async Task<TResponse> PostFileAsync<TResponse>(HttpMethod method, string requestUri, Stream fileContent, string fileName, NameValueCollection formData = null, CancellationToken cancellationToken = default) {
using var formDataContent = new MultipartFormDataContent("upload-" + Guid.NewGuid().ToString().ToLower());
if (fileContent != null) {
var streamContent = new StreamContent(fileContent);
var fileExtension = Path.GetExtension(fileName);
streamContent.Headers.ContentType = new MediaTypeHeaderValue(GetMimeTypeFromExtension(fileExtension));
formDataContent.Add(streamContent, "file", fileName);
}
if (formData?.Count > 0) {
var items = formData.AllKeys.SelectMany(formData.GetValues, (k, v) => new { key = k, value = v });
foreach (var item in items) {
formDataContent.Add(new StringContent(item.value), item.key);
}
}
var httpRequest = new HttpRequestMessage(method, requestUri);
httpRequest.Content = formDataContent;
httpRequest.Headers.Add("Accept", "application/json");
var httpMessage = await _httpClient.SendAsync(httpRequest, cancellationToken);
var content = await httpMessage.Content.ReadAsStringAsync();
JsonResponse<TResponse> response;
if (httpMessage.IsSuccessStatusCode) {
response = new JsonResponse<TResponse>(content);
} else {
response = new JsonResponse<TResponse>(content, httpMessage.StatusCode, httpMessage.ReasonPhrase);
httpMessage.HandleHttpError(response);
}
return response.Data;
}

public async Task<TResponse> PutAsync<TRequest, TResponse>(string requestUri, TRequest model, CancellationToken cancellationToken = default) {
var httpMessage = await _httpClient.PutAsync(requestUri, JsonRequest.For(model), cancellationToken);
Expand Down
14 changes: 11 additions & 3 deletions src/Incontrl.Sdk/Services/OrganisationApi.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.IO;
using System;
using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Incontrl.Sdk.Abstractions;
Expand All @@ -17,9 +19,15 @@ public Task<Organisation> GetAsync(CancellationToken cancellationToken = default
public Task<Organisation> UpdateAsync(UpdateOrganisationRequest request, CancellationToken cancellationToken = default) =>
clientBase.PutAsync<UpdateOrganisationRequest, Organisation>($"subscriptions/{SubscriptionId}/organisations/{OrganisationId}", request, cancellationToken);

public Task LogoUploadAsync(Stream fileContent, string fileName, CancellationToken cancellationToken = default) =>
clientBase.PostFileAsync($"subscriptions/{SubscriptionId}/organisations/{OrganisationId}/logo", fileContent, fileName, cancellationToken);
public Task<Link> LogoUploadAsync(Stream fileContent, string fileName, CancellationToken cancellationToken = default) =>
clientBase.PostFileAsync<Link>(HttpMethod.Put, $"subscriptions/{SubscriptionId}/organisations/{OrganisationId}/logo", fileContent, fileName, formData:null, cancellationToken);

public Task<Link> LogoSetUriAsync(Uri absoluteUri, CancellationToken cancellationToken = default) {
var form = new System.Collections.Specialized.NameValueCollection {
{ "imageUrl", absoluteUri.ToString() }
};
return clientBase.PostFileAsync<Link>(HttpMethod.Put, $"subscriptions/{SubscriptionId}/company/logo", fileContent: null, fileName: null, formData: form, cancellationToken);
}
public Task DeleteAsync(CancellationToken cancellationToken = default) =>
clientBase.DeleteAsync($"subscriptions/{SubscriptionId}/organisations/{OrganisationId}", cancellationToken);
}
Expand Down
14 changes: 11 additions & 3 deletions src/Incontrl.Sdk/Services/SubscriptionCompanyApi.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Incontrl.Sdk.Abstractions;
Expand All @@ -17,8 +18,15 @@ public Task<Organisation> GetAsync(CancellationToken cancellationToken = default
public Task<Organisation> UpdateAsync(UpdateCompanyRequest request, CancellationToken cancellationToken = default) =>
clientBase.PutAsync<UpdateCompanyRequest, Organisation>($"subscriptions/{SubscriptionId}/company", request, cancellationToken);

public Task LogoUploadAsync(Stream fileContent, string fileName, CancellationToken cancellationToken = default) =>
clientBase.PostFileAsync($"subscriptions/{SubscriptionId}/company/logo", fileContent, fileName, cancellationToken);

public Task<Link> LogoUploadAsync(Stream fileContent, string fileName, CancellationToken cancellationToken = default) {
return clientBase.PostFileAsync<Link>(HttpMethod.Put, $"subscriptions/{SubscriptionId}/company/logo", fileContent, fileName, formData: null, cancellationToken);
}
public Task<Link> LogoSetUriAsync(Uri absoluteUri, CancellationToken cancellationToken = default) {
var form = new System.Collections.Specialized.NameValueCollection {
{ "imageUrl", absoluteUri.ToString() }
};
return clientBase.PostFileAsync<Link>(HttpMethod.Put, $"subscriptions/{SubscriptionId}/company/logo", fileContent:null, fileName: null, formData: form, cancellationToken);
}

}
}
2 changes: 1 addition & 1 deletion test/Incontrl.Sdk.Tests/IncontrlApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public async Task CanUploadImage(string subscriptionId) {
var image = Image.Load(Convert.FromBase64String(base64), out var format);
image.Save(stream, format);
stream.Position = 0;
await _api.Subscriptions(subscriptionId).Company().LogoUploadAsync(stream, "test.png");
var link = await _api.Subscriptions(subscriptionId).Company().LogoUploadAsync(stream, "test.png");
Assert.True(true);
}

Expand Down

0 comments on commit 5700af8

Please sign in to comment.