Skip to content

Commit 493a70c

Browse files
committed
Adds state support to OTS API requests
Enables specifying the 'state' (e.g., normal, staging) for retrieving projects from the WebTrends Optimize (OTS) API. This change introduces a `State` enum and includes it as an optional parameter in the `GetProjectAsync` and `GetProjectsAsync` methods of the `IControlOperations` interface and `ControlOperations` class. The state is then passed as the `s_mode` parameter in the OTS request. This allows consumers to retrieve project configurations specific to different environments.
1 parent 20c7d89 commit 493a70c

File tree

8 files changed

+52
-29
lines changed

8 files changed

+52
-29
lines changed

apps/WebTrendsSDK.ConsoleSample/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
const string UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0";
1616

17-
var project = await api.Ots.Control.GetProjectAsync("ta_YieldManagement", userAgent: UserAgent);
17+
var project = await api.Ots.Control.GetProjectAsync("ta_1614ReducedCheckout", userAgent: UserAgent);
1818

1919
var projects = await api.Ots.Control.GetProjectsAsync(userAgent: UserAgent);
2020

libs/WebTrendsSDK/Api/Ots/Control/ControlOperations.cs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ partial interface IOtsOperations
1414
partial class OtsOperations
1515
{
1616
Lazy<IControlOperations> _control;
17-
public IControlOperations Control => (_control ??= client.Defer<IControlOperations>(c => new ControlOperations(path + "/control", c))).Value;
17+
public IControlOperations Control
18+
=> (_control ??= _client.Defer<IControlOperations>(
19+
c => new ControlOperations(_path + "/control", c))).Value;
1820
}
1921

2022
public interface IControlOperations
@@ -26,12 +28,14 @@ public interface IControlOperations
2628
/// <param name="projectAlias">The project alias.</param>
2729
/// <param name="websiteUrl">The website URL.</param>
2830
/// <param name="userAgent">The user agent.</param>
31+
/// <param name="state">The state identity, which controls which tests apply. Default is <see cref="State.Normal"/></param>
2932
/// <param name="cancellationToken">The cancellation token.</param>
3033
/// <returns>The WebTrends response.</returns>
3134
Task<WebTrendsResponse<Project>> GetProjectAsync(
3235
string projectAlias,
3336
string? websiteUrl = null,
3437
string? userAgent = null,
38+
State? state = null,
3539
CancellationToken cancellationToken = default);
3640

3741
/// <summary>
@@ -44,50 +48,58 @@ Task<WebTrendsResponse<Project>> GetProjectAsync(
4448
Task<WebTrendsResponse<Project[]>> GetProjectsAsync(
4549
string? websiteUrl = null,
4650
string? userAgent = null,
51+
State? state = State.Normal,
4752
CancellationToken cancellationToken = default);
4853
}
4954

5055
public class ControlOperations(PathString path, ApiClient client) : IControlOperations
5156
{
57+
readonly PathString _path = path;
58+
readonly ApiClient _client = client;
59+
5260
public async Task<WebTrendsResponse<Project>> GetProjectAsync(
5361
string projectAlias,
5462
string? websiteUrl = null,
5563
string? userAgent = null,
64+
State? state = null,
5665
CancellationToken cancellationToken = default)
5766
{
5867
Ensure.IsNotNullOrEmpty(projectAlias, nameof(projectAlias));
5968

60-
var operationPath = path + $"/{client.Settings.AccountId}-{projectAlias}";
69+
var operationPath = _path + $"/{_client.Settings.AccountId}-{projectAlias}";
6170

6271
var request = new WebTrendsRequest<OtsRequest>(
6372
HttpMethod.Post,
6473
operationPath,
65-
CreateOtsRequest(websiteUrl),
66-
BuildQuery(client.Settings),
74+
CreateOtsRequest(websiteUrl, state),
75+
BuildQuery(_client.Settings),
6776
userAgent);
6877

69-
return await client.FetchSingleAsync<OtsRequest, Project, ProjectBody>(request, cancellationToken).ConfigureAwait(false);
78+
return await _client.FetchSingleAsync<OtsRequest, Project, ProjectBody>(request, cancellationToken).ConfigureAwait(false);
7079
}
7180

7281
public async Task<WebTrendsResponse<Project[]>> GetProjectsAsync(
7382
string? websiteUrl = null,
7483
string? userAgent = null,
84+
State? state = null,
7585
CancellationToken cancellationToken = default)
7686
{
77-
var operationPath = path + $"/{client.Settings.AccountId}";
87+
var operationPath = _path + $"/{_client.Settings.AccountId}";
7888

7989
var request = new WebTrendsRequest<OtsRequest>(
8090
HttpMethod.Post,
8191
operationPath,
82-
CreateOtsRequest(websiteUrl),
83-
BuildQuery(client.Settings),
92+
CreateOtsRequest(websiteUrl, state),
93+
BuildQuery(_client.Settings),
8494
userAgent);
8595

86-
return await client.FetchManyAsync<OtsRequest, Project, ProjectBody>(request, cancellationToken).ConfigureAwait(false);
96+
return await _client.FetchManyAsync<OtsRequest, Project, ProjectBody>(request, cancellationToken).ConfigureAwait(false);
8797
}
8898

89-
OtsRequest CreateOtsRequest(string? websiteUrl)
90-
=> new OtsRequest(websiteUrl is { Length: > 0 } ? websiteUrl : client.Settings.WebsiteUrl);
99+
OtsRequest CreateOtsRequest(string? websiteUrl, State? state)
100+
=> new OtsRequest(
101+
websiteUrl is { Length: > 0 } ? websiteUrl : _client.Settings.WebsiteUrl,
102+
state.GetValueOrDefault(_client.Settings.State).ToString().ToLower());
91103

92104
QueryString BuildQuery(WebTrendsSettings settings)
93105
=> new QueryStringBuilder()

libs/WebTrendsSDK/Api/Ots/Control/Project.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// This work is licensed under the terms of the MIT license.
22
// For a copy, see <https://opensource.org/licenses/MIT>.
33

4+
using System.ComponentModel.DataAnnotations;
5+
using System.Runtime.Serialization;
46
using System.Text.Json.Serialization;
57

68
using WebTrendsSDK.Api;
@@ -39,3 +41,9 @@ public class ProjectFactor
3941
[JsonPropertyName("value")]
4042
public string? Value { get; set; }
4143
}
44+
45+
public enum State
46+
{
47+
Normal,
48+
Staging
49+
}

libs/WebTrendsSDK/Api/Ots/OtsOperations.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ public IOtsOperations Ots
1818
{
1919
get
2020
{
21-
return (_ots ??= Defer<IOtsOperations>(
22-
c => new OtsOperations(new("/ots/api/rest-1.2"), c))).Value;
21+
return (_ots ??= Defer<IOtsOperations>(c => new OtsOperations(new("/ots/api/rest-1.2"), c))).Value;
2322
}
2423
}
2524
}
@@ -32,9 +31,8 @@ public partial interface IOtsOperations
3231

3332
}
3433

35-
public partial class OtsOperations(
36-
PathString path,
37-
ApiClient client) : IOtsOperations
34+
public partial class OtsOperations(PathString path, ApiClient client) : IOtsOperations
3835
{
39-
36+
readonly PathString _path = path;
37+
readonly ApiClient _client = client;
4038
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// This work is licensed under the terms of the MIT license.
22
// For a copy, see <https://opensource.org/licenses/MIT>.
33

4+
using System.Text.Json.Serialization;
5+
46
namespace WebTrendsSDK.Api;
57

6-
internal record OtsRequest(string url);
8+
internal record OtsRequest(
9+
string url,
10+
[property: JsonPropertyName("s_mode")] string state);

libs/WebTrendsSDK/Api/WebTrendsApiClient.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ public partial interface IWebTrendsApiClient
88

99
}
1010

11-
public partial class WebTrendsApiClient : ApiClient, IWebTrendsApiClient
12-
{
13-
public WebTrendsApiClient(HttpClient http, WebTrendsSettings settings)
14-
: base(http, settings) { }
15-
}
11+
public partial class WebTrendsApiClient(HttpClient http, WebTrendsSettings settings)
12+
: ApiClient(http, settings), IWebTrendsApiClient
13+
{ }

libs/WebTrendsSDK/Api/WebTrendsApiClientFactory.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@ public interface IWebTrendsApiClientFactory
1515
IWebTrendsApiClient CreateApiClient(WebTrendsSettings settings);
1616
}
1717

18-
public class WebTrendsApiClientFactory : IWebTrendsApiClientFactory
18+
public class WebTrendsApiClientFactory(IWebTrendsHttpClientFactory httpClientFactory) : IWebTrendsApiClientFactory
1919
{
20-
readonly IWebTrendsHttpClientFactory _httpClientFactory;
21-
22-
public WebTrendsApiClientFactory(IWebTrendsHttpClientFactory httpClientFactory) =>
23-
_httpClientFactory = Ensure.IsNotNull(httpClientFactory, nameof(httpClientFactory));
20+
readonly IWebTrendsHttpClientFactory _httpClientFactory
21+
= Ensure.IsNotNull(httpClientFactory, nameof(httpClientFactory));
2422

2523
public IWebTrendsApiClient CreateApiClient(WebTrendsSettings settings)
2624
{

libs/WebTrendsSDK/WebTrendsSettings.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ public class WebTrendsSettings
4949
/// </summary>
5050
public string OtsBaseUrl { get; set; } = "https://ots.webtrends-optimize.com";
5151

52+
/// <summary>
53+
/// Gets or sets the default state for the WebTrends API.
54+
/// </summary>
55+
public State State { get; set; } = State.Normal;
56+
5257
/// <summary>
5358
/// Gets or sets whether to track the returned content as a View in WebTrends.
5459
/// </summary>

0 commit comments

Comments
 (0)