Skip to content

Commit f751c98

Browse files
authored
Merge pull request #16 from episerver/CG-14843-Content-Sync-Always-Set-to-En
CG-14843: Content Sync Language Always Set to En
2 parents c75c680 + 5014772 commit f751c98

File tree

6 files changed

+32
-12
lines changed

6 files changed

+32
-12
lines changed

Optimizely.Graph.Source.Sdk/Optimizely.Graph.Source.Sdk.Sample/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@
192192
}
193193
};
194194

195-
await client.SaveContentAsync(generateId: (x) => $"{x.Name}_{x.Address.City}", exampleDataInstance1, exampleDataInstance2, exampleDataInstance3);
195+
await client.SaveContentAsync(generateId: (x) => $"{x.Name}_{x.Address.City}", "en", exampleDataInstance1, exampleDataInstance2, exampleDataInstance3);
196196
#endregion
197197

198198
#region ExampleTypes

Optimizely.Graph.Source.Sdk/Optimizely.Graph.Source.Sdk.Tests/RepositoryTests/GraphSourceRepositoryTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ public async Task SaveContentAsync_SerializesData_AndCallsGraphClient()
206206
mockRestClient.Setup(c => c.HandleResponse<ContentV2ApiResponse>(response));
207207

208208
// Act
209-
await repository.SaveContentAsync(generateId: (x) => x.ToString(), exampleData);
209+
await repository.SaveContentAsync(generateId: (x) => x.ToString(), "en", exampleData);
210210

211211
// Assert
212212
mockRestClient.Verify(c => c.SendAsync(It.Is<HttpRequestMessage>(x => Compare(request, x))), Times.Once);
@@ -364,7 +364,7 @@ public async Task SaveContentAsync_WithMultipleTypes_ShouldGenerateJsonForConten
364364
mockRestClient.Setup(c => c.HandleResponse<ContentV2ApiResponse>(response));
365365

366366
// Act
367-
await repository.SaveContentAsync<object>(generateId, locationStockholm, locationLondon, event1, event2, event3);
367+
await repository.SaveContentAsync<object>(generateId, "en", locationStockholm, locationLondon, event1, event2, event3);
368368

369369
// Assert
370370
Assert.AreEqual(expectedJsonString, jsonString);
@@ -401,7 +401,7 @@ public async Task CreateContent_ShouldContainTwoNewLines()
401401
};
402402

403403
// Act
404-
var createdContent = repository.CreateContent(generateId: (x) => x.ToString(), exampleData);
404+
var createdContent = repository.CreateContent(generateId: (x) => x.ToString(), "en", exampleData);
405405
var result = createdContent.ReadAsStringAsync().Result;
406406

407407
// Assert
@@ -435,7 +435,7 @@ public async Task CreateContent_ShouldProduceMinifiedContent()
435435
};
436436

437437
// Act
438-
var createdContent = repository.CreateContent(generateId: (x) => x.ToString(), exampleData);
438+
var createdContent = repository.CreateContent(generateId: (x) => x.ToString(), "en", exampleData);
439439
var result = createdContent.ReadAsStringAsync().Result;
440440

441441
// Assert

Optimizely.Graph.Source.Sdk/Optimizely.Graph.Source.Sdk/GraphSourceClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ public async Task<string> SaveTypesAsync()
8989
/// <param name="generateId">Id associated with content.</param>
9090
/// <param name="data">Dynamic data being saved to Content Graph.</param>
9191
/// <returns></returns>
92-
public async Task<string> SaveContentAsync<T>(Func<T, string> generateId, params T[] data)
92+
public async Task<string> SaveContentAsync<T>(Func<T, string> generateId, string language, params T[] data)
9393
where T : class, new()
9494
{
95-
return await repository.SaveContentAsync(generateId, data);
95+
return await repository.SaveContentAsync(generateId, language, data);
9696
}
9797

9898
/// <summary>

Optimizely.Graph.Source.Sdk/Optimizely.Graph.Source.Sdk/Repositories/GraphSourceRepository.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ public async Task<string> SaveTypesAsync()
8080
}
8181

8282
/// <inheritdoc/>
83-
public async Task<string> SaveContentAsync<T>(Func<T, string> generateId, params T[] data)
83+
public async Task<string> SaveContentAsync<T>(Func<T, string> generateId, string language, params T[] data)
8484
where T : class, new()
8585
{
86-
var content = CreateContent(generateId, data);
86+
var content = CreateContent(generateId, language, data);
8787

8888
using var requestMessage = new HttpRequestMessage(HttpMethod.Post, $"{DataUrl}?id={source}");
8989
requestMessage.Content = content;
@@ -92,7 +92,7 @@ public async Task<string> SaveContentAsync<T>(Func<T, string> generateId, params
9292
return response?.JournalId ?? string.Empty;
9393
}
9494

95-
public StringContent CreateContent<T>(Func<T, string> generateId, params T[] data)
95+
public StringContent CreateContent<T>(Func<T, string> generateId, string language, params T[] data)
9696
{
9797
var serializeOptions = new JsonSerializerOptions
9898
{
@@ -107,7 +107,6 @@ public StringContent CreateContent<T>(Func<T, string> generateId, params T[] dat
107107
foreach (var item in data)
108108
{
109109
var id = generateId(item);
110-
var language = "en";
111110

112111
itemJson += $"{{\"index\":{{\"_id\":\"{id}\",\"language_routing\":\"{language}\"}}}}";
113112
itemJson += Environment.NewLine;

Optimizely.Graph.Source.Sdk/Optimizely.Graph.Source.Sdk/Repositories/IGraphSourceRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public interface IGraphSourceRepository
4040
/// <param name="generateId">Id associated with content.</param>
4141
/// <param name="data">Dynamic data being saved to Content Graph.</param>
4242
/// <returns></returns>
43-
Task<string> SaveContentAsync<T>(Func<T, string> generateId, params T[] data) where T : class, new();
43+
Task<string> SaveContentAsync<T>(Func<T, string> generateId, string language, params T[] data) where T : class, new();
4444

4545
/// <summary>
4646
/// Removes content previously stored by source.

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ You can find more information https://docs.developers.optimizely.com/platform-op
1616

1717
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.
1818

19+
#### Sync Content Types
1920
```csharp
2021
// Initialize the GraphSourceClient by calling the Create method
2122
var client = GraphSourceClient.Create(new Uri("https://cg.optimizely.com"), "", "", "");
@@ -57,6 +58,26 @@ client.ConfigurePropertyType<ExampleClassObject.SubType1>()
5758
var result = await graphSourceClient.SaveTypesAsync();
5859
```
5960

61+
#### Sync Content
62+
```csharp
63+
// Instantiate custom C# object and assign values
64+
var exampleData = new ExampleClassObject
65+
{
66+
FirstName = "First",
67+
LastName = "Last",
68+
Age = 30,
69+
SubType = new SubType1
70+
{
71+
One = "One",
72+
Two = 2,
73+
}
74+
};
75+
76+
// Use the client to sync content
77+
// Parameters are generated id, language, and data object
78+
await client.SaveContentAsync(generateId: (x) => $"{x.FirstName}_{x.LastName}", "en", exampleData);
79+
```
80+
6081
## Run Examples
6182
In visual studio, set your startup project to the Optimizely.Graph.Source.Sdk.Sample project.
6283

0 commit comments

Comments
 (0)