Skip to content

Commit 4658f8b

Browse files
Merge pull request #1 from Mecalc/Ernst/Q2.4.x
Update to Q2.4.x
2 parents f4b888f + f5de8a4 commit 4658f8b

4 files changed

Lines changed: 102 additions & 10 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*.user
1010
*.userosscache
1111
*.sln.docstates
12+
*.idea
1213

1314
# User-specific files (MonoDevelop/Xamarin Studio)
1415
*.userprefs

QClient.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net7.0</TargetFramework>
4+
<TargetFramework>net8.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
7+
<LangVersion>latestmajor</LangVersion>
78
</PropertyGroup>
89

910
<ItemGroup>

RestfulClient/RestfulInterface.cs

Lines changed: 98 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ namespace QClient.RestfulClient
1414
/// </summary>
1515
public class RestfulInterface : IRestfulInterface
1616
{
17-
private const int timeout = 60;
18-
private JsonElementToInferredTypesConverter customConverter = new();
19-
private JsonSerializerOptions serializerOptions = new();
17+
private const int timeout = 150;
18+
private readonly JsonElementToInferredTypesConverter customConverter = new();
19+
private readonly JsonSerializerOptions serializerOptions = new();
2020

2121
public static HttpClient Client { get; internal set; } = new() { Timeout = new TimeSpan(0, 0, timeout) };
2222

@@ -37,7 +37,7 @@ public class RestfulInterface : IRestfulInterface
3737
/// <exception cref="ArgumentException">Thrown when the URL is null or empty.</exception>
3838
public RestfulInterface(string url)
3939
{
40-
ArgumentException.ThrowIfNullOrEmpty(nameof(url));
40+
ArgumentNullException.ThrowIfNull(nameof(url));
4141

4242
Url = url;
4343
serializerOptions.Converters.Add(customConverter);
@@ -53,13 +53,56 @@ public virtual void Put(string endpoint, params HttpParameter[] parameters)
5353
Put(endpoint, body: null, parameters: parameters);
5454
}
5555

56+
/// <summary>
57+
/// Sends a Put request to the QServer with the specified endpoint and parameters
58+
/// </summary>
59+
/// <param name="endpoint">Specify the endpoint to be used for the request.</param>
60+
/// <param name="parameters">Specify the parameters if applicable.</param>
61+
public virtual T Put<T>(string endpoint, params HttpParameter[] parameters)
62+
{
63+
var buildUri = new StringBuilder();
64+
buildUri.Append(Url);
65+
buildUri.Append(endpoint);
66+
if (parameters != null && parameters.Length > 0)
67+
{
68+
buildUri.Append($"?{string.Join("&", parameters.Select(item => $"{item.Name}={item.Value}"))}");
69+
}
70+
71+
try
72+
{
73+
using var httpResponse = Client.PutAsync(buildUri.ToString(), null).Result;
74+
LastResponse = httpResponse.Content.ReadAsStringAsync().Result;
75+
76+
if (IsSupportedStatusCode(httpResponse.StatusCode) == false)
77+
{
78+
throw new ApplicationException($"GET command failed: {endpoint} with error message: {LastResponse}");
79+
}
80+
81+
QProtocolResponseChecks.CheckAndThrow(LastResponse);
82+
return JsonSerializer.Deserialize<T>(LastResponse, serializerOptions)!;
83+
}
84+
catch (AggregateException info)
85+
{
86+
if (info.InnerException is TaskCanceledException canceledException)
87+
{
88+
throw new TimeoutException($"Unable to reach the QServer on the provided URL {Url}.");
89+
}
90+
else if (info.InnerException != null)
91+
{
92+
throw info.InnerException;
93+
}
94+
95+
throw;
96+
}
97+
}
98+
5699
/// <summary>
57100
/// Sends a Put request to the QServer with the specified endpoint and parameters.
58101
/// </summary>
59102
/// <param name="endpoint">Specify the endpoint to be used for the request.</param>
60103
/// <param name="body">Specify a body for the request.</param>
61104
/// <param name="parameters">Specify the parameters if applicable.</param>
62-
public virtual void Put(string endpoint, object body, params HttpParameter[] parameters)
105+
public virtual void Put(string endpoint, object? body, params HttpParameter[] parameters)
63106
{
64107
var buildUri = new StringBuilder();
65108
buildUri.Append(Url);
@@ -90,10 +133,53 @@ public virtual void Put(string endpoint, object body, params HttpParameter[] par
90133
{
91134
throw new TimeoutException($"Unable to reach the QServer on the provided URL {Url}.");
92135
}
93-
else
136+
else if (info.InnerException != null)
94137
{
95138
throw info.InnerException;
96139
}
140+
141+
throw;
142+
}
143+
}
144+
145+
public T Post<T>(string endpoint, object body, params HttpParameter[] parameters)
146+
{
147+
var buildUri = new StringBuilder();
148+
buildUri.Append(Url);
149+
buildUri.Append(endpoint);
150+
if (parameters != null && parameters.Length > 0)
151+
{
152+
buildUri.Append($"?{string.Join("&", parameters.Select(item => $"{item.Name}={item.Value}"))}");
153+
}
154+
155+
var jsonBody = JsonSerializer.Serialize(body);
156+
var httpJsonBody = new StringContent(jsonBody, Encoding.UTF8, "application/json");
157+
158+
try
159+
{
160+
using var httpResponse = Client.PostAsync(buildUri.ToString(), httpJsonBody).Result;
161+
LastResponse = httpResponse.Content.ReadAsStringAsync().Result;
162+
163+
if (IsSupportedStatusCode(httpResponse.StatusCode) == false)
164+
{
165+
throw new ApplicationException($"PUT command failed: {endpoint} with error message: {LastResponse}");
166+
}
167+
168+
QProtocolResponseChecks.CheckAndThrow(LastResponse);
169+
return JsonSerializer.Deserialize<T>(LastResponse, serializerOptions)!;
170+
}
171+
catch (AggregateException info)
172+
{
173+
if (info.InnerException is TaskCanceledException canceledException)
174+
{
175+
throw new TimeoutException($"Unable to reach the QServer on the provided URL {Url}.");
176+
}
177+
else if (info.InnerException != null)
178+
{
179+
throw info.InnerException;
180+
}
181+
182+
throw;
97183
}
98184
}
99185

@@ -133,10 +219,12 @@ public virtual T Get<T>(string endpoint, params HttpParameter[] parameters)
133219
{
134220
throw new TimeoutException($"Unable to reach the QServer on the provided URL {Url}.");
135221
}
136-
else
222+
else if (info.InnerException != null)
137223
{
138224
throw info.InnerException;
139225
}
226+
227+
throw;
140228
}
141229
}
142230

@@ -173,10 +261,12 @@ public virtual void Delete(string endpoint, params HttpParameter[] parameters)
173261
{
174262
throw new TimeoutException($"Unable to reach the QServer on the provided URL {Url}.");
175263
}
176-
else
264+
else if (info.InnerException != null)
177265
{
178266
throw info.InnerException;
179267
}
268+
269+
throw;
180270
}
181271
}
182272

0 commit comments

Comments
 (0)