Skip to content

Commit

Permalink
Merge pull request #8 from VahidFarahmandian/response-sender
Browse files Browse the repository at this point in the history
ResponseReceived event added to connectors
  • Loading branch information
VahidFarahmandian committed Jun 27, 2023
2 parents d619a56 + c3f7fa2 commit 562fc44
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
26 changes: 21 additions & 5 deletions Jinget.AzureDevOps.Connector/AzureDevOpsConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ namespace Jinget.AzureDevOps.Connector
{
public abstract class AzureDevOpsConnector
{
public event EventHandler<string> ResponseReceived;
public virtual void OnResponseReceived(string response) => ResponseReceived?.Invoke(this, response);

private readonly HttpClient client;
private readonly string apiVersion;
/// <summary>
/// indicates the web api request path first segment. usually starts with '_apis/...'
/// </summary>
protected string RootPathSegment;

//public AzureDevOpsConnector(string pat) : this(pat, "") { }
//public AzureDevOpsConnector(string pat, string rootPathSegment) : this(pat, Constants.DefaultBaseUrl, rootPathSegment) { }
//public AzureDevOpsConnector(string pat, string url, string rootPathSegment) : this(pat, url, Constants.DefaultRestApiVersion, rootPathSegment) { }
public AzureDevOpsConnector(string pat, string url, string apiVersion, string rootPathSegment)
{
client = new HttpClient
Expand Down Expand Up @@ -58,8 +58,12 @@ private string GetUrl(string path, Dictionary<string, string>? urlParameters = n
protected async Task<T> GetAsync<T>(string path, Dictionary<string, string>? urlParameters = null, bool appendApiVersion = true)
{
using var response = await client.GetAsync(GetUrl(path, urlParameters, appendApiVersion));
response.EnsureSuccessStatusCode();
var responseBody = await response.Content.ReadAsStringAsync();

OnResponseReceived(responseBody);

response.EnsureSuccessStatusCode();

#pragma warning disable CS8603 // Possible null reference return.
return JsonSerializer.Deserialize<T>(responseBody);
#pragma warning restore CS8603 // Possible null reference return.
Expand All @@ -71,8 +75,12 @@ protected async Task<TResponseBody> PostAsync<TRequestBody, TResponseBody>(strin
using StringContent content = new StringContent(JsonSerializer.Serialize(requestBody), Encoding.UTF8, contentType);

using var response = await client.PostAsync(GetUrl(path, urlParameters, appendApiVersion), content);
response.EnsureSuccessStatusCode();
var responseBody = await response.Content.ReadAsStringAsync();

OnResponseReceived(responseBody);

response.EnsureSuccessStatusCode();

#pragma warning disable CS8603 // Possible null reference return.
return JsonSerializer.Deserialize<TResponseBody>(responseBody);
#pragma warning restore CS8603 // Possible null reference return.
Expand All @@ -83,12 +91,20 @@ protected async Task<bool> PostAsync<T>(string path, T requestBody, Dictionary<s
using StringContent content = new StringContent(JsonSerializer.Serialize(requestBody), Encoding.UTF8, contentType);

using var response = await client.PostAsync(GetUrl(path, urlParameters, appendApiVersion), content);

var responseBody = await response.Content.ReadAsStringAsync();
OnResponseReceived(responseBody);

return response.IsSuccessStatusCode;
}

protected async Task<bool> DeleteAsync(string path, Dictionary<string, string>? urlParameters = null, bool appendApiVersion = true)
{
using var response = await client.DeleteAsync(GetUrl(path, urlParameters, appendApiVersion));

var responseBody = await response.Content.ReadAsStringAsync();
OnResponseReceived(responseBody);

return response.IsSuccessStatusCode;
}
}
Expand Down
11 changes: 11 additions & 0 deletions Jinget.AzureDevOps.ConnectorTests/TeamsTests/TeamConnectorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ public class TeamConnectorTests : _BaseTests
[TestInitialize]
public void TestInitialize() => connector = new TeamConnector(pat, organization);


[TestMethod()]
public async Task should_get_raw_response()
{
connector.ResponseReceived += (object? sender, string e) =>
{
Assert.IsTrue(e != "");
};
TeamListViewModel result = await connector.GetAllTeamsAsync();
}

[TestMethod()]
public async Task should_get_list_of_all_teams()
{
Expand Down

0 comments on commit 562fc44

Please sign in to comment.