Created
November 30, 2022 07:19
-
-
Save khushboosorthiya/f9b88268c4ea94c5fcbbb86da88bb44d to your computer and use it in GitHub Desktop.
Sitecore CDP Batch Import Process using C# .NET
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
public class BatchImportModel | |
{ | |
[JsonProperty("ref")] | |
public string ReferenceId { get; set; } | |
[JsonProperty("schema")] | |
public string Schema { get; set; } | |
[JsonProperty("mode")] | |
public string Mode { get; set; } | |
[JsonProperty("value")] | |
public GuestModel Guest { get; set; } | |
} |
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; | |
using System.Collections.Generic; | |
using System.Net.Http.Headers; | |
using System.Net.Http; | |
using Newtonsoft.Json; | |
using ReadMessageFromQueue.Models; | |
using System.IO; | |
using System.IO.Compression; | |
using System.Security.Cryptography; | |
using System.Text; | |
namespace Sitecore.CDP.POC | |
{ | |
public class CDPBatchImport | |
{ | |
private byte[] md5ChecksumHash; | |
private string md5Checksum = string.Empty; | |
public void Run(DBModel data) | |
{ | |
//UUID for the batch | |
string uniqueId = Guid.NewGuid().ToString(); | |
//Guest Model | |
BatchImportModel batchImportModel = new BatchImportModel() | |
{ | |
ReferenceId = Guid.NewGuid().ToString(), | |
Schema = "guest", | |
Mode = "upsert", | |
Guest = new GuestModel() | |
{ | |
FirstName = data.FirstName, | |
LastName = data.LastName, | |
Email = data.Email, | |
City = data.City, | |
Country = "BR", | |
PostCode = data.PostalCode, | |
State = data.State, | |
Street = new List<string>() { data.Address }, | |
Extensions = new List<Extension>() | |
{ | |
new Extension() | |
{ | |
Name = "ext", | |
Key = "default", | |
Tier = "gold", | |
MilesBalance = "50125", | |
MemberSince = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss"), | |
LoyaltyNumber = "123456789" | |
} | |
}, | |
Identifiers = new List<Identifier>() | |
{ | |
new Identifier() | |
{ | |
Provider = "email", | |
Id = data.Email | |
} | |
} | |
} | |
}; | |
//open file stream | |
using (StreamWriter file = File.CreateText(@"C:\sitecore\CDP\batchimport\guest.json")) | |
{ | |
//serialize object directly into file stream | |
JsonSerializer serializer = new(); | |
serializer.Serialize(file, batchImportModel); | |
} | |
CompressFile(); | |
CalculateMD5(@"C:\sitecore\CDP\batchimport\guest.gz"); | |
//Get this values from your CDP environment in System Settings -> API Access | |
string clientKey = "your client key"; | |
string apiToken = "your API token"; | |
HttpClient httpClient = new(); | |
// Add an Accept header for JSON format. | |
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); | |
// Add authorization header | |
var authenticationString = $"{clientKey}:{apiToken}"; | |
var base64EncodedAuthenticationString = Convert.ToBase64String(Encoding.ASCII.GetBytes(authenticationString)); | |
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", base64EncodedAuthenticationString); | |
PUTRequest request = new() | |
{ | |
Checksum = md5Checksum, | |
Size = new FileInfo(@"C:\sitecore\CDP\batchimport\guest.gz").Length | |
}; | |
using var content = new StringContent(JsonConvert.SerializeObject(request)); | |
content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); | |
//First PUT request to get location in AWS S3 | |
var result = httpClient.PutAsync(new Uri($"https://api-us.boxever.com/v2/batches/{uniqueId}"), content).Result; | |
if (result.IsSuccessStatusCode) | |
{ | |
// Parse the response body. | |
var putResponse = result.Content.ReadAsStringAsync().Result; | |
var responseOnj = JsonConvert.DeserializeObject<PUTResponse>(putResponse); | |
if (responseOnj != null && responseOnj.Location != null && !string.IsNullOrEmpty(responseOnj.Location.Href)) | |
{ | |
httpClient.Dispose(); | |
//Second PUT request to upload a file into assigned location as response of first PUT request. | |
HttpClient client = new(); | |
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); | |
HttpRequestMessage requestMessage = new(HttpMethod.Put, new Uri(responseOnj.Location.Href)); | |
requestMessage.Headers.Add("x-amz-server-side-encryption", "AES256"); | |
requestMessage.Content = new ByteArrayContent(File.ReadAllBytes(@"C:\sitecore\CDP\batchimport\guest.gz")); | |
requestMessage.Content.Headers.ContentMD5 = md5ChecksumHash; | |
HttpResponseMessage responseMessage = client.SendAsync(requestMessage).Result; | |
if (responseMessage.IsSuccessStatusCode) | |
{ | |
Console.WriteLine("Success"); | |
} | |
client.Dispose(); | |
} | |
} | |
} | |
private static void CompressFile() | |
{ | |
const string OriginalFileName = @"C:\sitecore\CDP\batchimport\guest.json"; | |
const string CompressedFileName = @"C:\sitecore\CDP\batchimport\guest.gz"; | |
using FileStream originalFileStream = File.Open(OriginalFileName, FileMode.Open); | |
using FileStream compressedFileStream = File.Create(CompressedFileName); | |
using var compressor = new GZipStream(compressedFileStream, CompressionMode.Compress); | |
originalFileStream.CopyTo(compressor); | |
} | |
private void CalculateMD5(string filename) | |
{ | |
using var md5 = MD5.Create(); | |
using var stream = File.OpenRead(filename); | |
var hash = md5.ComputeHash(stream); | |
md5Checksum = BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant(); | |
md5ChecksumHash = hash; | |
} | |
} | |
} |
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
public class DBModel | |
{ | |
[JsonProperty("CustomerId")] | |
public string CustomerId { get; set; } | |
[JsonProperty("FirstName")] | |
public string FirstName { get; set; } | |
[JsonProperty("LastName")] | |
public string LastName { get; set; } | |
[JsonProperty("Company")] | |
public string Company { get; set; } | |
[JsonProperty("Address")] | |
public string Address { get; set; } | |
[JsonProperty("City")] | |
public string City { get; set; } | |
[JsonProperty("State")] | |
public string State { get; set; } | |
[JsonProperty("Country")] | |
public string Country { get; set; } | |
[JsonProperty("PostalCode")] | |
public string PostalCode { get; set; } | |
[JsonProperty("Phone")] | |
public string Phone { get; set; } | |
[JsonProperty("Fax")] | |
public string Fax { get; set; } | |
[JsonProperty("Email")] | |
public string Email { get; set; } | |
[JsonProperty("SupportRepId")] | |
public string SupportRepId { get; set; } | |
} |
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
public class GuestModel | |
{ | |
[JsonProperty("firstName")] | |
public string FirstName { get; set; } | |
[JsonProperty("lastName")] | |
public string LastName { get; set; } | |
[JsonProperty("street")] | |
public List<string> Street { get; set; } | |
[JsonProperty("city")] | |
public string City { get; set; } | |
[JsonProperty("state")] | |
public string State { get; set; } | |
[JsonProperty("country")] | |
public string Country { get; set; } | |
[JsonProperty("postCode")] | |
public string PostCode { get; set; } | |
[JsonProperty("email")] | |
public string Email { get; set; } | |
[JsonProperty("extensions")] | |
public List<Extension> Extensions { get; set; } | |
[JsonProperty("identifiers")] | |
public List<Identifier> Identifiers { get; set; } | |
} | |
public class Extension | |
{ | |
[JsonProperty("name")] | |
public string Name { get; set; } | |
[JsonProperty("key")] | |
public string Key { get; set; } | |
[JsonProperty("tier")] | |
public string Tier { get; set; } | |
[JsonProperty("milesBalance")] | |
public string MilesBalance { get; set; } | |
[JsonProperty("memberSince")] | |
public string MemberSince { get; set; } | |
[JsonProperty("loyaltyNumber")] | |
public string LoyaltyNumber { get; set; } | |
} | |
public class Identifier | |
{ | |
[JsonProperty("provider")] | |
public string Provider { get; set; } | |
[JsonProperty("id")] | |
public string Id { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment