Skip to content

Commit

Permalink
Extend catalog service with create
Browse files Browse the repository at this point in the history
This commit contains a number of fixes, e.g. a way to create
catalogs programmatically (for admins only, for now).
  • Loading branch information
iavorJ committed Apr 2, 2020
1 parent d94ef85 commit 30ac503
Show file tree
Hide file tree
Showing 7 changed files with 282 additions and 15 deletions.
58 changes: 58 additions & 0 deletions src/Meplato.Store2.Tests/Catalogs/CreateTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#region Copyright and terms of services

// Copyright (c) 2013-present Meplato GmbH.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
// or implied. See the License for the specific language governing permissions and limitations under
// the License.

#endregion

using System.Threading.Tasks;
using Meplato.Store2.Catalogs;
using NUnit.Framework;

namespace Meplato.Store2.Tests.Catalogs
{
[TestFixture]
public class CreateTests : TestCase
{
[Test]
public async Task TestCreate()
{
MockFromFile("catalogs.create.success");

var service = GetCatalogsService();
Assert.NotNull(service);

var create = new CreateCatalog()
{
MerchantId = 1,
Name = "test2",
// Description = "",
// ProjectID = 0,
ProjectMpcc = "meplato",
ValidFrom = null,
ValidUntil = null,
Country = "DE",
Currency = "EUR",
Language = "de",
Target = "mall",
SageNumber = "",
SageContract = ""
};

var response = await service.Create().Catalog(create).Do();
Assert.NotNull(response);
Assert.AreEqual("store#catalog", response.Kind);
Assert.AreEqual(81, response.Id);
Assert.AreEqual("48F31F33AD", response.Pin);
}
}
}
2 changes: 2 additions & 0 deletions src/Meplato.Store2.Tests/Meplato.Store2.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<Otherwise />
</Choose>
<ItemGroup>
<Compile Include="Catalogs\CreateTests.cs" />
<Compile Include="Catalogs\GetTests.cs" />
<Compile Include="Catalogs\PublishTests.cs" />
<Compile Include="Catalogs\PurgeTests.cs" />
Expand Down Expand Up @@ -103,6 +104,7 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Content Include="TestData\catalogs.create.success" />
<Content Include="TestData\catalogs.get.not_found">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
Expand Down
11 changes: 11 additions & 0 deletions src/Meplato.Store2.Tests/TestData/catalogs.create.success
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
HTTP/1.1 201 Created
Content-Type: application/json; charset=utf-8
P3p: CP="This is not a P3P policy!"
Vary: Cookie
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Ua-Compatible: IE=edge
X-Xss-Protection: 1; mode=block
Date: Wed, 01 Apr 2015 13:53:54 GMT

{"kind":"store#catalog","selfLink":"https://store3.go/api/v2/catalogs/48F31F33AD","id":81,"type":"corporate","merchantId":1,"merchantMpcc":"meplato","merchantMpsc":"meplato-sc","merchantName":"Meplato GmbH","projectId":1,"projectMpcc":"meplato","projectMpbc":"meplato","projectName":"Meplato","project":{"id":1,"type":"corporate","mpcc":"meplato","mpbc":"meplato","name":"Meplato","profile":{"nameExists":{"policy":1},"categoriesExists":{"policy":1},"eclassesValid":{"policy":0,"version":"5.1"},"orderUnitValues":{"policy":0},"contentUnitValues":{"policy":0}},"customization":{"hooks":[{"kind":"publish_initial","via":"smtp","target":"oe+dev-initial-publish@meplato.de"}]},"script":"","visible":true,"country":"DE","language":"de","locale":"de_DE","timeZone":"Europe/Berlin","currency":"EUR","ou":"PCE","catalogPriceInitial":0,"catalogPriceRecurring":0,"catalogPriceCurrency":"EUR","catalogPriceInterval":"yearly","target":"mall","companyGroup":"","created":"2017-10-09T14:29:35Z","updated":"2020-01-02T08:47:32Z"},"slug":"test2","name":"test2","pin":"48F31F33AD","validFrom":"2020-03-13","validUntil":"2099-12-31","currency":"EUR","country":"DE","language":"de","state":"idle","created":"2020-03-13T15:39:52.004116159Z","updated":"2020-03-13T15:39:52.004116159Z","lockedForDownload":false,"supportsOciDetail":false,"supportsOciDetailadd":false,"supportsOciValidate":false,"supportsOciSourcing":false,"supportsOciBackgroundsearch":false,"supportsOciQuantitycheck":false,"supportsOciDownloadjson":false,"keepOriginalBlobs":false,"target":"","showImportFailure":false}
176 changes: 172 additions & 4 deletions src/Meplato.Store2/Catalogs/Service.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#region Copyright and terms of services
// Copyright (c) 2015-2016 Meplato GmbH.
// Copyright (c) 2013-present Meplato GmbH.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
Expand All @@ -17,8 +17,8 @@
// The file implements the Meplato Store API.
//
// Author: Meplato API Team <support@meplato.com>
// Version: 2.1.6
// License: Copyright (c) 2015-2018 Meplato GmbH. All rights reserved.
// Version: 2.1.7
// License: Copyright (c) 2015-2020 Meplato GmbH. All rights reserved.
// See <a href="https://developer.meplato.com/store2/#terms">Terms of Service</a>
// See <a href="https://developer.meplato.com/store2/">External documentation</a>

Expand All @@ -40,7 +40,7 @@ public class Service
{
#region Service
public const string Title = "Meplato Store API";
public const string Version = "2.1.6";
public const string Version = "2.1.7";
public const string UserAgent = "meplato-csharp-client/2.0";
public const string DefaultBaseURL = "https://store.meplato.com/api/v2";

Expand Down Expand Up @@ -100,6 +100,13 @@ public string GetAuthorizationHeader()
return null;
}

/// <summary>
/// Create a new catalog (admin only).
/// </summary>
public CreateService Create() {
return new CreateService(this);
}

/// <summary>
/// Get a single catalog.
/// </summary>
Expand Down Expand Up @@ -452,6 +459,13 @@ public class Catalog
[JsonProperty("supportsOciValidate")]
public bool SupportsOciValidate { get; set; }

/// <summary>
/// Target represents the target system which can be either an
/// empty string, "catscout" or "mall".
/// </summary>
[JsonProperty("target")]
public string Target { get; set; }

/// <summary>
/// Type of catalog, e.g. corporate or basic.
/// </summary>
Expand Down Expand Up @@ -480,6 +494,101 @@ public class Catalog
#endregion // Catalog
}

/// <summary>
/// CreateCatalog holds the properties of a new catalog.
/// </summary>
public class CreateCatalog
{
#region CreateCatalog

/// <summary>
/// Country is the ISO-3166 alpha-2 code for the country that the
/// catalog is destined for (e.g. DE or US).
/// </summary>
[JsonProperty("country")]
public string Country { get; set; }

/// <summary>
/// Currency is the ISO-4217 currency code that is used for all
/// products in the catalog (e.g. EUR or USD).
/// </summary>
[JsonProperty("currency")]
public string Currency { get; set; }

/// <summary>
/// Description of the catalog.
/// </summary>
[JsonProperty("description")]
public string Description { get; set; }

/// <summary>
/// Language is the IETF language tag of the language of all
/// products in the catalog (e.g. de or pt-BR).
/// </summary>
[JsonProperty("language")]
public string Language { get; set; }

/// <summary>
/// ID of the merchant.
/// </summary>
[JsonProperty("merchantId")]
public long MerchantId { get; set; }

/// <summary>
/// Name of the catalog.
/// </summary>
[JsonProperty("name")]
public string Name { get; set; }

/// <summary>
/// ID of the project.
/// </summary>
[JsonProperty("projectId")]
public long ProjectId { get; set; }

/// <summary>
/// MPCC of the project.
/// </summary>
[JsonProperty("projectMpcc")]
public string ProjectMpcc { get; set; }

/// <summary>
/// SageContract represents the internal identifier at Meplato for
/// the contract of this catalog.
/// </summary>
[JsonProperty("sageContract")]
public string SageContract { get; set; }

/// <summary>
/// SageNumber represents the internal identifier at Meplato for
/// the merchant of this catalog.
/// </summary>
[JsonProperty("sageNumber")]
public string SageNumber { get; set; }

/// <summary>
/// Target represents the target system which can be either an
/// empty string, "catscout" or "mall".
/// </summary>
[JsonProperty("target")]
public string Target { get; set; }

/// <summary>
/// ValidFrom is the date the catalog becomes effective
/// (YYYY-MM-DD).
/// </summary>
[JsonProperty("validFrom")]
public string ValidFrom { get; set; }

/// <summary>
/// ValidUntil is the date the catalog expires (YYYY-MM-DD).
/// </summary>
[JsonProperty("validUntil")]
public string ValidUntil { get; set; }

#endregion // CreateCatalog
}

/// <summary>
/// CustField describes a generic name/value pair. Its purpose is
/// to provide a mechanism for customer-specific fields.
Expand Down Expand Up @@ -819,6 +928,65 @@ public class SearchResponse
#endregion // SearchResponse
}

/// <summary>
/// CreateService: Create a new catalog (admin only).
/// </summary>
public class CreateService
{
#region CreateService

private readonly Service _service;
private readonly IDictionary<string, object> _opt = new Dictionary<string, object>();
private readonly IDictionary<string, string> _hdr = new Dictionary<string, string>();

private CreateCatalog _catalog;

/// <summary>
/// Creates a new instance of CreateService.
/// </summary>
public CreateService(Service service)
{
_service = service;
}

/// <summary>
/// Catalog properties of the new catalog.
/// </summary>
public CreateService Catalog(CreateCatalog catalog)
{
_catalog = catalog;
return this;
}

/// <summary>
/// Execute the operation.
/// </summary>
public async Task<Catalog> Do()
{
// Make a copy of the parameters and add the path parameters to it
var parameters = new Dictionary<string, object>();

// Make a copy of the header parameters and set UA
var headers = new Dictionary<string, string>();
string authorization = _service.GetAuthorizationHeader();
if (!string.IsNullOrEmpty(authorization))
{
headers["Authorization"] = authorization;
}

var uriTemplate = _service.BaseURL + "/catalogs";
var response = await _service.Client.Execute(
HttpMethod.Post,
uriTemplate,
parameters,
headers,
_catalog);
return response.GetBodyJSON<Catalog>();
}

#endregion // CreateService
}

/// <summary>
/// GetService: Get a single catalog.
/// </summary>
Expand Down
8 changes: 4 additions & 4 deletions src/Meplato.Store2/Jobs/Service.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#region Copyright and terms of services
// Copyright (c) 2015-2016 Meplato GmbH.
// Copyright (c) 2013-present Meplato GmbH.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
Expand All @@ -17,8 +17,8 @@
// The file implements the Meplato Store API.
//
// Author: Meplato API Team <support@meplato.com>
// Version: 2.1.6
// License: Copyright (c) 2015-2018 Meplato GmbH. All rights reserved.
// Version: 2.1.7
// License: Copyright (c) 2015-2020 Meplato GmbH. All rights reserved.
// See <a href="https://developer.meplato.com/store2/#terms">Terms of Service</a>
// See <a href="https://developer.meplato.com/store2/">External documentation</a>

Expand All @@ -40,7 +40,7 @@ public class Service
{
#region Service
public const string Title = "Meplato Store API";
public const string Version = "2.1.6";
public const string Version = "2.1.7";
public const string UserAgent = "meplato-csharp-client/2.0";
public const string DefaultBaseURL = "https://store.meplato.com/api/v2";

Expand Down
Loading

0 comments on commit 30ac503

Please sign in to comment.