diff --git a/Optimizely.Graph.Source.Sdk/Optimizely.Graph.Source.Sdk.Sample/Program.cs b/Optimizely.Graph.Source.Sdk/Optimizely.Graph.Source.Sdk.Sample/Program.cs index 0a2d77f..42d6795 100644 --- a/Optimizely.Graph.Source.Sdk/Optimizely.Graph.Source.Sdk.Sample/Program.cs +++ b/Optimizely.Graph.Source.Sdk/Optimizely.Graph.Source.Sdk.Sample/Program.cs @@ -192,7 +192,7 @@ } }; -await client.SaveContentAsync(generateId: (x) => $"{x.Name}_{x.Address.City}", exampleDataInstance1, exampleDataInstance2, exampleDataInstance3); +await client.SaveContentAsync(generateId: (x) => $"{x.Name}_{x.Address.City}", "en", exampleDataInstance1, exampleDataInstance2, exampleDataInstance3); #endregion #region ExampleTypes diff --git a/Optimizely.Graph.Source.Sdk/Optimizely.Graph.Source.Sdk.Tests/RepositoryTests/GraphSourceRepositoryTests.cs b/Optimizely.Graph.Source.Sdk/Optimizely.Graph.Source.Sdk.Tests/RepositoryTests/GraphSourceRepositoryTests.cs index 86e350a..77c1258 100644 --- a/Optimizely.Graph.Source.Sdk/Optimizely.Graph.Source.Sdk.Tests/RepositoryTests/GraphSourceRepositoryTests.cs +++ b/Optimizely.Graph.Source.Sdk/Optimizely.Graph.Source.Sdk.Tests/RepositoryTests/GraphSourceRepositoryTests.cs @@ -206,7 +206,7 @@ public async Task SaveContentAsync_SerializesData_AndCallsGraphClient() mockRestClient.Setup(c => c.HandleResponse(response)); // Act - await repository.SaveContentAsync(generateId: (x) => x.ToString(), exampleData); + await repository.SaveContentAsync(generateId: (x) => x.ToString(), "en", exampleData); // Assert mockRestClient.Verify(c => c.SendAsync(It.Is(x => Compare(request, x))), Times.Once); @@ -316,7 +316,7 @@ public async Task SaveContentAsync_WithMultipleTypes_ShouldGenerateJsonForConten mockRestClient.Setup(c => c.HandleResponse(response)); // Act - await repository.SaveContentAsync(generateId, locationStockholm, locationLondon, event1, event2, event3); + await repository.SaveContentAsync(generateId, "en", locationStockholm, locationLondon, event1, event2, event3); // Assert Assert.AreEqual(expectedJsonString, jsonString); @@ -353,7 +353,7 @@ public async Task CreateContent_ShouldContainTwoNewLines() }; // Act - var createdContent = repository.CreateContent(generateId: (x) => x.ToString(), exampleData); + var createdContent = repository.CreateContent(generateId: (x) => x.ToString(), "en", exampleData); var result = createdContent.ReadAsStringAsync().Result; // Assert @@ -387,7 +387,7 @@ public async Task CreateContent_ShouldProduceMinifiedContent() }; // Act - var createdContent = repository.CreateContent(generateId: (x) => x.ToString(), exampleData); + var createdContent = repository.CreateContent(generateId: (x) => x.ToString(), "en", exampleData); var result = createdContent.ReadAsStringAsync().Result; // Assert diff --git a/Optimizely.Graph.Source.Sdk/Optimizely.Graph.Source.Sdk/GraphSourceClient.cs b/Optimizely.Graph.Source.Sdk/Optimizely.Graph.Source.Sdk/GraphSourceClient.cs index cc493a5..488f83a 100644 --- a/Optimizely.Graph.Source.Sdk/Optimizely.Graph.Source.Sdk/GraphSourceClient.cs +++ b/Optimizely.Graph.Source.Sdk/Optimizely.Graph.Source.Sdk/GraphSourceClient.cs @@ -89,10 +89,10 @@ public async Task SaveTypesAsync() /// Id associated with content. /// Dynamic data being saved to Content Graph. /// - public async Task SaveContentAsync(Func generateId, params T[] data) + public async Task SaveContentAsync(Func generateId, string language, params T[] data) where T : class, new() { - return await repository.SaveContentAsync(generateId, data); + return await repository.SaveContentAsync(generateId, language, data); } /// diff --git a/Optimizely.Graph.Source.Sdk/Optimizely.Graph.Source.Sdk/Repositories/GraphSourceRepository.cs b/Optimizely.Graph.Source.Sdk/Optimizely.Graph.Source.Sdk/Repositories/GraphSourceRepository.cs index 594a0ef..ae5aed4 100644 --- a/Optimizely.Graph.Source.Sdk/Optimizely.Graph.Source.Sdk/Repositories/GraphSourceRepository.cs +++ b/Optimizely.Graph.Source.Sdk/Optimizely.Graph.Source.Sdk/Repositories/GraphSourceRepository.cs @@ -80,10 +80,10 @@ public async Task SaveTypesAsync() } /// - public async Task SaveContentAsync(Func generateId, params T[] data) + public async Task SaveContentAsync(Func generateId, string language, params T[] data) where T : class, new() { - var content = CreateContent(generateId, data); + var content = CreateContent(generateId, language, data); using (var requestMessage = new HttpRequestMessage(HttpMethod.Post, $"{DataUrl}?id={source}")) { @@ -96,7 +96,7 @@ public async Task SaveContentAsync(Func generateId, params return string.Empty; } - public StringContent CreateContent(Func generateId, params T[] data) + public StringContent CreateContent(Func generateId, string language, params T[] data) { var serializeOptions = new JsonSerializerOptions { @@ -111,7 +111,6 @@ public StringContent CreateContent(Func generateId, params T[] dat foreach (var item in data) { var id = generateId(item); - var language = "en"; itemJson += $"{{\"index\":{{\"_id\":\"{id}\",\"language_routing\":\"{language}\"}}}}"; itemJson += Environment.NewLine; diff --git a/Optimizely.Graph.Source.Sdk/Optimizely.Graph.Source.Sdk/Repositories/IGraphSourceRepository.cs b/Optimizely.Graph.Source.Sdk/Optimizely.Graph.Source.Sdk/Repositories/IGraphSourceRepository.cs index 346c3bc..5eb0bf6 100644 --- a/Optimizely.Graph.Source.Sdk/Optimizely.Graph.Source.Sdk/Repositories/IGraphSourceRepository.cs +++ b/Optimizely.Graph.Source.Sdk/Optimizely.Graph.Source.Sdk/Repositories/IGraphSourceRepository.cs @@ -40,7 +40,7 @@ public interface IGraphSourceRepository /// Id associated with content. /// Dynamic data being saved to Content Graph. /// - Task SaveContentAsync(Func generateId, params T[] data) where T : class, new(); + Task SaveContentAsync(Func generateId, string language, params T[] data) where T : class, new(); /// /// Removes content previously stored by source. diff --git a/README.md b/README.md index ad67af1..77ff99a 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ You can find more information https://docs.developers.optimizely.com/platform-op You can use the client by calling `Create()` and providing your base url, Content Graph source, application key and secret, then calling one of the provided functions for synchronizing Content Types and Content Data. +#### Sync Content Types ```csharp // Initialize the GraphSourceClient by calling the Create method var client = GraphSourceClient.Create(new Uri("https://cg.optimizely.com"), "", "", ""); @@ -57,6 +58,26 @@ client.ConfigurePropertyType() var result = await graphSourceClient.SaveTypesAsync(); ``` +#### Sync Content +```csharp +// Instantiate custom C# object and assign values +var exampleData = new ExampleClassObject +{ + FirstName = "First", + LastName = "Last", + Age = 30, + SubType = new SubType1 + { + One = "One", + Two = 2, + } +}; + +// Use the client to sync content +// Parameters are generated id, language, and data object +await client.SaveContentAsync(generateId: (x) => $"{x.FirstName}_{x.LastName}", "en", exampleData); +``` + ## Run Examples In visual studio, set your startup project to the Optimizely.Graph.Source.Sdk.Sample project.