Skip to content
This repository has been archived by the owner on May 12, 2023. It is now read-only.

Commit

Permalink
Return location link on create/update
Browse files Browse the repository at this point in the history
For #17
  • Loading branch information
pardahlman committed May 15, 2017
1 parent 6438ed7 commit 37f7be1
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 6 deletions.
28 changes: 28 additions & 0 deletions Akeneo.IntegrationTests/MediaFileTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Threading.Tasks;
using Akeneo.Model;
using Xunit;

namespace Akeneo.IntegrationTests
{
public class MediaFileTests : IntegrationTestBase
{
[Fact]
public async Task Shoud_Upload_Image()
{
/* Setup */
var fileUpload = new MediaUpload
{
Product =
{
Identifier = "tyfon-bb-1000-1000-3m-3m",
Attribute = "Product_Image_Medium"
},
FilePath = "C:\\tmp\\product_logo.png"
};

/* Test */
var response = await Client.UploadAsync(fileUpload);
/* Assert */
}
}
}
7 changes: 4 additions & 3 deletions Akeneo/AkeneoClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Akeneo.Authentication;
using Akeneo.Client;
using Akeneo.Common;
using Akeneo.Consts;
using Akeneo.Exceptions;
using Akeneo.Http;
using Akeneo.Model;
Expand Down Expand Up @@ -67,7 +68,7 @@ public AkeneoClient(Uri apiEndPoint, IAuthenticationClient authClient) : base(ap
var endpoint = _endpointResolver.ForResourceType<TModel>(option?.Attribute ?? string.Empty);
var response = await PostAsync(endpoint, model, ct);
return response.IsSuccessStatusCode
? AkeneoResponse.Success(response.StatusCode)
? AkeneoResponse.Success(response.StatusCode, new KeyValuePair<string, PaginationLink>(PaginationLinks.Location, new PaginationLink { Href = response.Headers?.Location?.ToString() }))
: await response.Content.ReadAsJsonAsync<AkeneoResponse>();
}

Expand All @@ -76,7 +77,7 @@ public AkeneoClient(Uri apiEndPoint, IAuthenticationClient authClient) : base(ap
var endpoint = _endpointResolver.ForResource(model);
var response = await PatchAsJsonAsync(endpoint, model, ct);
return response.IsSuccessStatusCode
? AkeneoResponse.Success(response.StatusCode)
? AkeneoResponse.Success(response.StatusCode, new KeyValuePair<string, PaginationLink>(PaginationLinks.Location, new PaginationLink { Href = response.Headers?.Location?.ToString() }))
: await response.Content.ReadAsJsonAsync<AkeneoResponse>();
}

Expand Down Expand Up @@ -123,7 +124,7 @@ public AkeneoClient(Uri apiEndPoint, IAuthenticationClient authClient) : base(ap
}

return response.IsSuccessStatusCode
? AkeneoResponse.Success(response.StatusCode)
? AkeneoResponse.Success(response.StatusCode, new KeyValuePair<string, PaginationLink>(PaginationLinks.Location, new PaginationLink{ Href = response.Headers?.Location?.ToString()}))
: await response.Content.ReadAsJsonAsync<AkeneoResponse>();
}

Expand Down
6 changes: 4 additions & 2 deletions Akeneo/Client/AkeneoResponse.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Newtonsoft.Json;

Expand All @@ -13,11 +14,12 @@ public class AkeneoResponse
public Dictionary<string, PaginationLink> Links { get; set; }
public List<ValidationError> Errors { get; set; }

public static AkeneoResponse Success(HttpStatusCode code)
public static AkeneoResponse Success(HttpStatusCode code, params KeyValuePair<string, PaginationLink>[] links)
{
return new AkeneoResponse
{
Code = code
Code = code,
Links = links?.ToDictionary(kvp => kvp.Key, kvp => kvp.Value)
};
}
}
Expand Down
12 changes: 12 additions & 0 deletions Akeneo/Consts/PaginationLinks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Akeneo.Consts
{
public class PaginationLinks
{
public const string Location = "location";
public const string Next = "next";
public const string Previous = "previous";
public const string Self = "self";
public const string First = "first";
public const string Download = "download";
}
}
3 changes: 2 additions & 1 deletion Akeneo/Extensions/AkeneoClientExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading;
using System.Threading.Tasks;
using Akeneo.Client;
using Akeneo.Consts;
using Akeneo.Model;

namespace Akeneo.Extensions
Expand All @@ -24,7 +25,7 @@ public static class AkeneoClientExtensions
{
var pagination = await client.GetManyAsync<TModel>(parentCode, page, limit, ct: ct);
result.AddRange(pagination.GetItems());
hasMore = pagination.Links.ContainsKey("next");
hasMore = pagination.Links.ContainsKey(PaginationLinks.Next);
page++;
} while (hasMore);
return result;
Expand Down

0 comments on commit 37f7be1

Please sign in to comment.