This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
using System.Net; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
using Microsoft.Azure.WebJobs; | |
using Microsoft.Azure.WebJobs.Extensions.Http; | |
using Microsoft.Azure.WebJobs.Host; | |
using Sitecore.Commerce.ServiceProxy; | |
using System; | |
using Sitecore.Commerce.Engine; | |
using Microsoft.OData.Client; | |
using System.Globalization; | |
using System.Security.Cryptography.X509Certificates; | |
using Newtonsoft.Json.Linq; | |
namespace Sitecore.Services.Examples.Azure.Functions | |
{ | |
public static class CreateProductRequest | |
{ | |
[FunctionName("CreateProductRequest")] | |
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log) | |
{ | |
log.Info("C# HTTP trigger function processed a request."); | |
// parse query parameter | |
var productid = req.GetQueryNameValuePairs() | |
.FirstOrDefault(q => string.Compare(q.Key, "productid", StringComparison.OrdinalIgnoreCase) == 0) | |
.Value; | |
var priceStr = req.GetQueryNameValuePairs() | |
.FirstOrDefault(q => string.Compare(q.Key, "price", StringComparison.OrdinalIgnoreCase) == 0) | |
.Value; | |
if (decimal.TryParse(priceStr, out var outPrice)) | |
{ | |
outPrice = System.Convert.ToDecimal(priceStr); | |
} | |
var productsArray = new JArray(); | |
dynamic variant1 = new JObject(); | |
variant1.variantid = productid + "-Variant1"; | |
variant1.listprice = outPrice; | |
dynamic variant2 = new JObject(); | |
variant2.variantid = productid + "-Variant2"; | |
variant2.listprice = outPrice; | |
dynamic variant3 = new JObject(); | |
variant3.variantid = productid + "-Variant3"; | |
variant3.listprice = outPrice; | |
dynamic product = new JObject(); | |
product.productid = productid; | |
product.listprice = outPrice; | |
product.name = "Product " + productid; | |
product.displayname = "Product " + productid; | |
product.description = "Product " + productid + " is the best!!!"; | |
product.variants = new JArray(variant1, variant2, variant3); | |
productsArray.Add(product); | |
var o = new JObject | |
{ | |
["products"] = productsArray | |
}; | |
var products = o.ToString(); | |
var re = GetContainer( System.Configuration.ConfigurationManager.AppSettings["defaultEnvironment"], | |
System.Configuration.ConfigurationManager.AppSettings["defaultShopName"], | |
string.Empty).CreateUpdateSellableItem(products); | |
var result = Proxy.GetValue(re); | |
return result == false | |
? req.CreateResponse(HttpStatusCode.BadRequest, "Failed to update or create") | |
: req.CreateResponse(HttpStatusCode.OK, "Created or updated " + productid ); | |
} | |
public static Container GetContainer(string environment, string shopName, string userId, string customerId = "", string language = "", string currency = "", DateTime? effectiveDate = null) | |
{ | |
return GetShopsContainer(environment, shopName, userId, customerId, language, currency, effectiveDate); | |
} | |
public static Container GetShopsContainer(string environment = "", string shopName = "", string userId = "", string customerId = "", string language = "", string currency = "", DateTime? effectiveDate = null) | |
{ | |
var container = new Container(new Uri(System.Configuration.ConfigurationManager.AppSettings["shopsServiceUrl"])) | |
{ | |
MergeOption = MergeOption.OverwriteChanges, | |
DisableInstanceAnnotationMaterialization = true | |
}; | |
container.BuildingRequest += (s, e) => { | |
e.Headers.Add("ShopName", shopName); | |
e.Headers.Add("Environment", environment); | |
if (effectiveDate.HasValue) | |
{ | |
DateTimeOffset value = effectiveDate.Value; | |
e.Headers.Add("EffectiveDate", value.ToString(CultureInfo.InvariantCulture)); | |
} | |
e.Headers.Add("ShopperId", string.Empty); | |
e.Headers.Add("IsRegistered", string.Empty); | |
if (!string.IsNullOrEmpty(customerId)) | |
{ | |
e.Headers.Add("CustomerId", customerId); | |
} | |
if (!string.IsNullOrEmpty(language)) | |
{ | |
e.Headers.Add("Language", language); | |
} | |
if (!string.IsNullOrEmpty(currency)) | |
{ | |
e.Headers.Add("Currency", currency); | |
} | |
var certificate = GetCertificate(); | |
if (certificate != null) | |
{ | |
e.Headers.Add(System.Configuration.ConfigurationManager.AppSettings["certificateHeaderName"], certificate); | |
} | |
}; | |
return container; | |
} | |
public static string GetCertificate() | |
{ | |
var thumbPrint = System.Configuration.ConfigurationManager.AppSettings["certificateThumbprint"]; | |
if (string.IsNullOrEmpty(thumbPrint)) | |
{ | |
return null; | |
} | |
//for azure change to User from LocalMachine | |
var x509Store = new X509Store(StoreName.My,StoreLocation.LocalMachine); | |
x509Store.Open(OpenFlags.ReadOnly); | |
var x509Certificate2Collection = x509Store.Certificates.Find(X509FindType.FindByThumbprint, thumbPrint, false); | |
if (x509Certificate2Collection.Count == 0) | |
{ | |
return null; | |
} | |
var item = x509Certificate2Collection[0]; | |
return item == null ? null : System.Convert.ToBase64String(item.Export(X509ContentType.Cert), Base64FormattingOptions.None); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment