Skip to content

Commit

Permalink
Version 0.8.6
Browse files Browse the repository at this point in the history
  • Loading branch information
XiaoFaye committed Mar 29, 2023
1 parent 2a59cd8 commit 9def407
Show file tree
Hide file tree
Showing 15 changed files with 100 additions and 48 deletions.
14 changes: 8 additions & 6 deletions Base/BaseObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using WooCommerce.NET.WordPress.v2;

namespace WooCommerceNET.Base
{
[DataContract]
public class JsonObject
{
[IgnoreDataMember]
public static CultureInfo Culture { get; set; }

[OnSerializing]
void OnSerializing(StreamingContext ctx)
{
Expand All @@ -28,13 +30,13 @@ void OnSerializing(StreamingContext ctx)
GetType().GetTypeInfo().BaseType.FullName.StartsWith("WooCommerceNET.WooCommerce.v1") ||
GetType().GetTypeInfo().BaseType.FullName.StartsWith("WooCommerceNET.WooCommerce.v2") ||
GetType().GetTypeInfo().BaseType.FullName.StartsWith("WooCommerceNET.WooCommerce.v3"))
objValue.SetValue(this, (pi.GetValue(this) as decimal?).Value.ToString(CultureInfo.InvariantCulture));
objValue.SetValue(this, (pi.GetValue(this) as decimal?).Value.ToString(Culture));
else
objValue.SetValue(this, decimal.Parse(pi.GetValue(this).ToString(), CultureInfo.InvariantCulture));
objValue.SetValue(this, decimal.Parse(pi.GetValue(this).ToString(), Culture));
}
else if (pi.PropertyType == typeof(int?))
{
objValue.SetValue(this, int.Parse(pi.GetValue(this).ToString(), CultureInfo.InvariantCulture));
objValue.SetValue(this, int.Parse(pi.GetValue(this).ToString(), Culture));
}
else if (pi.PropertyType == typeof(DateTime?))
{
Expand All @@ -58,14 +60,14 @@ void OnDeserialized(StreamingContext ctx)
object value = objValue.GetValue(this);

if (!(value == null || value.ToString() == string.Empty))
pi.SetValue(this, decimal.Parse(value.ToString(), CultureInfo.InvariantCulture));
pi.SetValue(this, decimal.Parse(value.ToString(), Culture));
}
else if (pi.PropertyType == typeof(int?))
{
object value = objValue.GetValue(this);

if (!(value == null || value.ToString() == string.Empty))
pi.SetValue(this, int.Parse(value.ToString(), CultureInfo.InvariantCulture));
pi.SetValue(this, int.Parse(value.ToString(), Culture));
}
else if (pi.PropertyType == typeof(DateTime?))
{
Expand Down
4 changes: 0 additions & 4 deletions Base/HMAC-SHA256.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WooCommerceNET.Base
{
Expand Down
6 changes: 1 addition & 5 deletions Base/SHA1.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text;

namespace WooCommerceNET.Base
{
Expand Down
4 changes: 4 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

Version History
-------------------
* v0.8.6 update
1. Fix manage_stock property deserializing issue in Project object. #722
2. Add Culture object in WCObject constructor to resolve format issue. #731
3. Allow accessing WordPress plugin REST API with WooCommerce secret and key.
* v0.8.5 update
1. Change all id field to 64bit integer (unsigned long) to prevent overflow. #560
2. Add WCCustomerItem for get customer by email endpoint.
Expand Down
12 changes: 12 additions & 0 deletions Properties/PublishProfiles/FolderProfile.pubxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<PublishDir>bin\Release\netstandard2.0\publish\</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
</PropertyGroup>
</Project>
9 changes: 9 additions & 0 deletions Properties/PublishProfiles/FolderProfile.pubxml.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<History>True|2021-11-20T01:40:11.9243673Z;True|2021-03-17T21:17:35.0713079+13:00;</History>
</PropertyGroup>
</Project>
5 changes: 5 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ WooCommerce.NET is a .NET library for calling WooCommerce/WordPress REST API wit

If this project has been helpful for you and you want to support it, please consider [Buying me a coffee](https://www.buymeacoffee.com/YU0SqVyrR):coffee:

**For priority paid support/consulting service, customized REST API implementation and plugins REST API implementation, please email to [James (me:sunglasses:)](mailto:[email protected])**

Usage (WooCommerce REST API)
-------------------
* [How to use JSON.NET in WooCommerce.NET](https://github.com/XiaoFaye/WooCommerce.NET/wiki/How-to-use-JSON.NET-in-WooCommerce.NET)
Expand All @@ -32,6 +34,9 @@ using WooCommerceNET.WooCommerce.v3.Extension;
RestAPI rest = new RestAPI("http://www.yourstore.co.nz/wp-json/wc/v3/", "<WooCommerce Key>", "<WooCommerce Secret");
WCObject wc = new WCObject(rest);

//Use below code for WCObject only if you would like to have different CultureInfo
WCObject wc = new WCObject(rest, CultureInfo.GetCultureInfo("de-DE"));

//Get all products
var products = await wc.Product.GetAll();

Expand Down
39 changes: 21 additions & 18 deletions RestAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
Expand Down Expand Up @@ -127,7 +125,6 @@ public RestAPI(string url, string key, string secret, bool authorizedHeader = tr
}



public bool IsLegacy
{
get
Expand Down Expand Up @@ -189,15 +186,7 @@ public virtual async Task<string> SendHttpClientRequest<T>(string endpoint, Requ

if (wc_url.StartsWith("https", StringComparison.OrdinalIgnoreCase) && Version != APIVersion.WordPressAPI && Version != APIVersion.WordPressAPIJWT)
{
if (AuthorizedHeader == true)
{
httpWebRequest = (HttpWebRequest)WebRequest.Create(wc_url + GetOAuthEndPoint(method.ToString(), endpoint, parms));
if (WCAuthWithJWT && JWT_Object != null)
httpWebRequest.Headers["Authorization"] = "Bearer " + JWT_Object.token;
else
httpWebRequest.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(wc_key + ":" + wc_secret));
}
else
if (AuthorizedHeader == false)
{
if (parms == null)
parms = new Dictionary<string, string>();
Expand All @@ -206,8 +195,22 @@ public virtual async Task<string> SendHttpClientRequest<T>(string endpoint, Requ
parms.Add("consumer_key", wc_key);
if (!parms.ContainsKey("consumer_secret"))
parms.Add("consumer_secret", wc_secret);
}

//Allow accessing WordPress plugin REST API with WooCommerce secret and key.
//Url should be passed to RestAPI as WooCommerce Rest API url, e.g.: https://mystore.com/wp-json/wc/v3
//Endpoint should be starting with wp-json
if (endpoint.StartsWith("wp-json"))
httpWebRequest = (HttpWebRequest)WebRequest.Create(new Uri(new Uri($"https://{new Uri(wc_url).Host}"), GetOAuthEndPoint(method.ToString(), endpoint, parms)));
else
httpWebRequest = (HttpWebRequest)WebRequest.Create(wc_url + GetOAuthEndPoint(method.ToString(), endpoint, parms));

if (AuthorizedHeader == true)
{
if (WCAuthWithJWT && JWT_Object != null)
httpWebRequest.Headers["Authorization"] = "Bearer " + JWT_Object.token;
else
httpWebRequest.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(wc_key + ":" + wc_secret));
}
}
else
Expand All @@ -220,7 +223,7 @@ public virtual async Task<string> SendHttpClientRequest<T>(string endpoint, Requ
// start the stream immediately
httpWebRequest.Method = method.ToString();
httpWebRequest.AllowReadStreamBuffering = false;

if (webRequestFilter != null)
webRequestFilter.Invoke(httpWebRequest);

Expand Down Expand Up @@ -298,27 +301,27 @@ public virtual async Task<string> SendHttpClientRequest<T>(string endpoint, Requ

public async Task<string> GetRestful(string endpoint, Dictionary<string, string> parms = null)
{
return await SendHttpClientRequest(endpoint, RequestMethod.GET, string.Empty, parms).ConfigureAwait(false);
return await SendHttpClientRequest(endpoint.ToLower(), RequestMethod.GET, string.Empty, parms).ConfigureAwait(false);
}

public async Task<string> PostRestful(string endpoint, object jsonObject, Dictionary<string, string> parms = null)
{
return await SendHttpClientRequest(endpoint, RequestMethod.POST, jsonObject, parms).ConfigureAwait(false);
return await SendHttpClientRequest(endpoint.ToLower(), RequestMethod.POST, jsonObject, parms).ConfigureAwait(false);
}

public async Task<string> PutRestful(string endpoint, object jsonObject, Dictionary<string, string> parms = null)
{
return await SendHttpClientRequest(endpoint, RequestMethod.PUT, jsonObject, parms).ConfigureAwait(false);
return await SendHttpClientRequest(endpoint.ToLower(), RequestMethod.PUT, jsonObject, parms).ConfigureAwait(false);
}

public async Task<string> DeleteRestful(string endpoint, Dictionary<string, string> parms = null)
{
return await SendHttpClientRequest(endpoint, RequestMethod.DELETE, string.Empty, parms).ConfigureAwait(false);
return await SendHttpClientRequest(endpoint.ToLower(), RequestMethod.DELETE, string.Empty, parms).ConfigureAwait(false);
}

public async Task<string> DeleteRestful(string endpoint, object jsonObject, Dictionary<string, string> parms = null)
{
return await SendHttpClientRequest(endpoint, RequestMethod.DELETE, jsonObject, parms).ConfigureAwait(false);
return await SendHttpClientRequest(endpoint.ToLower(), RequestMethod.DELETE, jsonObject, parms).ConfigureAwait(false);
}

protected string GetOAuthEndPoint(string method, string endpoint, Dictionary<string, string> parms = null)
Expand Down
22 changes: 15 additions & 7 deletions WooCommerce.NET.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageId>WooCommerceNET</PackageId>
<Version>0.8.5</Version>
<Version>0.8.6</Version>
<Authors>JamesYang@NZ</Authors>
<Company>JamesYang@NZ</Company>
<Description>A .NET Wrapper for WooCommerce/WordPress REST API</Description>
Expand All @@ -15,18 +15,19 @@
GitHub: https://github.com/XiaoFaye/WooCommerce.NET
Changes Doc: https://github.com/XiaoFaye/WooCommerce.NET/blob/master/Changes.md

* v0.8.5 update
1. Change all id field to 64bit integer (unsigned long) to prevent overflow. #560
2. Add WCCustomerItem for get customer by email endpoint.
3. Escape all querystrings.</PackageReleaseNotes>
* v0.8.6 update
1. Fix manage_stock property deserializing issue in Project object. #722
2. Add Culture object in WCObject constructor to resolve format issue. #731
3. Allow accessing WordPress plugin REST API with WooCommerce secret and key.</PackageReleaseNotes>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<AssemblyVersion>0.8.5.0</AssemblyVersion>
<AssemblyVersion>0.8.6.0</AssemblyVersion>
<SignAssembly>false</SignAssembly>
<AssemblyOriginatorKeyFile>sn.key.snk</AssemblyOriginatorKeyFile>
<PackageLicenseFile>License.md</PackageLicenseFile>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<FileVersion>0.8.5.0</FileVersion>
<FileVersion>0.8.6.0</FileVersion>
<PackageTags>WooCommerce Wordpress Restful API</PackageTags>
<PackageReadmeFile>Readme.md</PackageReadmeFile>
</PropertyGroup>

<ItemGroup>
Expand All @@ -40,4 +41,11 @@ Changes Doc: https://github.com/XiaoFaye/WooCommerce.NET/blob/master/Changes.md
</None>
</ItemGroup>

<ItemGroup>
<None Update="Readme.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>

</Project>
2 changes: 2 additions & 0 deletions WooCommerce/v1/Product.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ public class Product : JsonObject
/// <summary>
/// Stock management at product level. Default is false.
/// When Manage stock is checked, string value "parent" will be given, otherwise, it will be bool value false.
/// The "parent" should appear in Variation object, however, when getting Products with variation SKU as parameter,
/// variation object with "parent" value returned in product endpoints. That's why we have to set manage_stock type as object in Product object as well.
/// </summary>
[DataMember(EmitDefaultValue = false)]
public object manage_stock { get; set; }
Expand Down
7 changes: 5 additions & 2 deletions WooCommerce/v1/WCObject.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading.Tasks;
using WooCommerceNET.Base;

Expand Down Expand Up @@ -28,11 +29,13 @@ public class WCObject<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T1
where T10 : ShippingClass where T11 : ProductTag where T12 : TaxRate where T13 : TaxClass where T14 : Webhook
{
protected RestAPI API;
public WCObject(RestAPI api)
public WCObject(RestAPI api, CultureInfo culture = null)
{
if (api.Version != APIVersion.Version1)
throw new Exception("Please use WooCommerce Restful API Version 1 url for this WCObject. e.g.: http://www.yourstore.co.nz/wp-json/wc/v1/");

JsonObject.Culture = culture ?? CultureInfo.InvariantCulture;

API = api;
}

Expand Down Expand Up @@ -614,7 +617,7 @@ public async Task<WebhookDelivery> GetWebhookDelivery(int webhookid, int deliver
public class WCObject : WCObject<Coupon, Customer, Order, OrderNote, OrderRefund, Product, ProductCategory,
ProductAttribute, ProductAttributeTerm, ShippingClass, ProductTag, TaxRate, TaxClass, Webhook>
{
public WCObject(RestAPI api) : base(api)
public WCObject(RestAPI api, CultureInfo culture = null) : base(api, culture)
{
}
}
Expand Down
5 changes: 4 additions & 1 deletion WooCommerce/v2/Product.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,12 @@ public class Product : JsonObject

/// <summary>
/// Stock management at product level. Default is false.
/// When Manage stock is checked, string value "parent" will be given, otherwise, it will be bool value false.
/// The "parent" should appear in Variation object, however, when getting Products with variation SKU as parameter,
/// variation object with "parent" value returned in product endpoints. That's why we have to set manage_stock type as object in Product object as well.
/// </summary>
[DataMember(EmitDefaultValue = false)]
public bool? manage_stock { get; set; }
public object manage_stock { get; set; }

[DataMember(EmitDefaultValue = false, Name = "stock_quantity")]
protected object stock_quantityValue { get; set; }
Expand Down
7 changes: 5 additions & 2 deletions WooCommerce/v2/WCObject.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using WooCommerceNET.Base;
Expand All @@ -16,11 +17,13 @@ public class WCObject<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T1
protected RestAPI API { get; set; }
public static Func<string, object, object> MetaValueProcessor { get; set; }
public static Func<string, object, object> MetaDisplayValueProcessor { get; set; }
public WCObject(RestAPI api)
public WCObject(RestAPI api, CultureInfo culture = null)
{
if (api.Version != APIVersion.Version2)
throw new Exception("Please use WooCommerce Restful API Version 2 url for this WCObject. e.g.: http://www.yourstore.co.nz/wp-json/wc/v2/");

JsonObject.Culture = culture ?? CultureInfo.InvariantCulture;

API = api;

Coupon = new WCItem<T1>(api);
Expand Down Expand Up @@ -222,7 +225,7 @@ public WCShippingZoneItem(RestAPI api) : base(api)
public class WCObject: WCObject<Coupon, Customer, Product, ProductReview, Variation, Order, OrderNote, OrderRefund, ProductAttribute, ProductAttributeTerm,
ProductCategory, ShippingClass, ProductTag, TaxRate, TaxClass>
{
public WCObject(RestAPI api) : base(api)
public WCObject(RestAPI api, CultureInfo culture = null) : base(api, culture)
{
}
}
Expand Down
5 changes: 4 additions & 1 deletion WooCommerce/v3/Product.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,12 @@ public class Product : JsonObject

/// <summary>
/// Stock management at product level. Default is false.
/// When Manage stock is checked, string value "parent" will be given, otherwise, it will be bool value false.
/// The "parent" should appear in Variation object, however, when getting Products with variation SKU as parameter,
/// variation object with "parent" value returned in product endpoints. That's why we have to set manage_stock type as object in Product object as well.
/// </summary>
[DataMember(EmitDefaultValue = false)]
public bool? manage_stock { get; set; }
public object manage_stock { get; set; }

[DataMember(EmitDefaultValue = false, Name = "stock_quantity")]
protected object stock_quantityValue { get; set; }
Expand Down
Loading

0 comments on commit 9def407

Please sign in to comment.