-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Refactoring publisher to reduce logic and support async operation. * Forgot to fill in environment variable names in lookup table. * Fixed failed conflict resolution. * Updated to use new publish methods. * Use Wait instead of RunSynchronously
- Loading branch information
1 parent
ae46947
commit 80ff768
Showing
3 changed files
with
39 additions
and
36 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,63 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Threading.Tasks; | ||
|
||
namespace Agoda.DevFeedback.Common | ||
{ | ||
public static class DevFeedbackPublisher | ||
{ | ||
public static void Publish<T>(string apiEndpoint, T data) | ||
/// <summary> | ||
/// Data data type to endpoints | ||
/// </summary> | ||
private static Dictionary<DevLocalDataType, List<string>> _dataTypeEndpoints = new Dictionary<DevLocalDataType, List<string>>() | ||
{ | ||
Publish(apiEndpoint,data, DevLocalDataType.Build); | ||
} | ||
{ DevLocalDataType.Build, new List<string>() { "dotnet", "BUILD_METRICS_ES_ENDPOINT" } }, | ||
{ DevLocalDataType.NUnit, new List<string>() { "dotnet/nunit", "NUNIT_METRICS_ES_ENDPOINT" } } | ||
}; | ||
|
||
// Default URL | ||
private const string BASE_URL = "http://compilation-metrics/"; | ||
|
||
public static void Publish<T>(string apiEndpoint, T data, DevLocalDataType devLocalDataType) | ||
/// <summary> | ||
/// Get the endpoint to use with a specific data source. | ||
/// </summary> | ||
private static string GetApiEndpoint(DevLocalDataType dataType, string apiEndpoint) | ||
{ | ||
var targetEndpoint = string.Empty; | ||
switch (devLocalDataType) | ||
_dataTypeEndpoints.TryGetValue(dataType, out var endpointInfo); | ||
if (endpointInfo == null) | ||
throw new ArgumentOutOfRangeException(nameof(dataType), dataType, null); | ||
// Fetch from environment if not manually provided | ||
if (string.IsNullOrEmpty(apiEndpoint)) | ||
{ | ||
case DevLocalDataType.Build: | ||
targetEndpoint = GetApiEndpoint(apiEndpoint); | ||
break; | ||
case DevLocalDataType.NUnit: | ||
targetEndpoint = GetNunitApiEndpoint(apiEndpoint); | ||
break; | ||
default: | ||
throw new ArgumentOutOfRangeException(nameof(devLocalDataType), devLocalDataType, null); | ||
apiEndpoint = Environment.GetEnvironmentVariable(endpointInfo[1]); | ||
} | ||
return string.IsNullOrEmpty(apiEndpoint) ? $"{BASE_URL}{endpointInfo[0]}" : apiEndpoint; | ||
} | ||
|
||
/// <summary> | ||
/// Publish the data as JSON to the appropriate endpoint | ||
/// </summary> | ||
public static async Task PublishAsync(string apiEndpoint, object data, DevLocalDataType devLocalDataType) | ||
{ | ||
var targetEndpoint = GetApiEndpoint(devLocalDataType, apiEndpoint); | ||
using (var httpClient = new HttpClient()) | ||
{ | ||
httpClient.Timeout = TimeSpan.FromSeconds(2); | ||
var content = new StringContent(JsonSerializer.Serialize(data), Encoding.UTF8, "application/json"); | ||
var response = httpClient.PostAsync(targetEndpoint, content).Result; | ||
var response = await httpClient.PostAsync(targetEndpoint, content); | ||
response.EnsureSuccessStatusCode(); | ||
} | ||
} | ||
|
||
private const string BASE_URL = "http://compilation-metrics/"; | ||
//private const string BASE_URL = "http://localhost:5000/"; | ||
static string GetApiEndpoint(string apiEndpoint) | ||
/// <summary> | ||
/// Non-async version of publish | ||
/// </summary> | ||
public static void Publish(string apiEndpoint, object data, DevLocalDataType devLocalDataType) | ||
{ | ||
if (string.IsNullOrEmpty(apiEndpoint)) | ||
{ | ||
apiEndpoint = Environment.GetEnvironmentVariable("BUILD_METRICS_ES_ENDPOINT"); | ||
} | ||
|
||
return string.IsNullOrEmpty(apiEndpoint) ? $"{BASE_URL}dotnet" : apiEndpoint; | ||
} | ||
|
||
static string GetNunitApiEndpoint(string apiEndpoint) | ||
{ | ||
if (string.IsNullOrEmpty(apiEndpoint)) | ||
{ | ||
apiEndpoint = Environment.GetEnvironmentVariable("NUNIT_METRICS_ES_ENDPOINT"); | ||
} | ||
|
||
return string.IsNullOrEmpty(apiEndpoint) ? $"{BASE_URL}dotnet/nunit" : apiEndpoint; | ||
PublishAsync(apiEndpoint, data, devLocalDataType).Wait(); | ||
} | ||
} | ||
} |