-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.cs
More file actions
58 lines (49 loc) · 2.09 KB
/
Copy pathClient.cs
File metadata and controls
58 lines (49 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace StanzaApi.IbanValidator
{
public class IbanValidatorClient
{
private readonly HttpClient _httpClient;
private readonly string _apiKey;
private readonly string _baseUrl;
public string ToolUrl { get; } = "https://stanzaapi.com/tools/iban-validator";
public IbanValidatorClient(string apiKey = null, string baseUrl = null, HttpClient httpClient = null)
{
_apiKey = apiKey ?? Environment.GetEnvironmentVariable("STANZA_API_KEY") ?? Environment.GetEnvironmentVariable("API_KEY") ?? "";
_baseUrl = (baseUrl ?? "https://api.stanzaapi.com/iban-validator").TrimEnd('/');
_httpClient = httpClient ?? new HttpClient { Timeout = TimeSpan.FromSeconds(15) };
}
private async Task<string> SendRequestAsync(string endpoint, HttpMethod method, string jsonBody = null)
{
var url = $"{_baseUrl}/{endpoint.TrimStart('/')}";
var request = new HttpRequestMessage(method, url);
request.Headers.Add("Accept", "application/json");
if (!string.IsNullOrEmpty(_apiKey))
{
request.Headers.Add("x-api-key", _apiKey);
request.Headers.Add("Authorization", $"Bearer {_apiKey}");
}
if (jsonBody != null)
{
request.Content = new StringContent(jsonBody, Encoding.UTF8, "application/json");
}
var response = await _httpClient.SendAsync(request);
return await response.Content.ReadAsStringAsync();
}
public Task<string> GetHealthAsync()
{
return SendRequestAsync("/health", HttpMethod.Get);
}
public Task<string> ValidateAsync(string jsonPayload)
{
return SendRequestAsync("/api/v1/validate", HttpMethod.Post, jsonPayload);
}
public Task<string> ParseAsync(string jsonPayload)
{
return SendRequestAsync("/api/v1/validate", HttpMethod.Post, jsonPayload);
}
}
}