Skip to content

Commit

Permalink
refactor: replace null by optional values in ApiRequest (#527)
Browse files Browse the repository at this point in the history
* Make ApiRequest constructor private

* Use Maybe for configuration on ApiRequest
  • Loading branch information
Tr00d authored Sep 21, 2023
1 parent 9d5239e commit dc3498e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
3 changes: 0 additions & 3 deletions Vonage.Common/Vonage.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@
</ItemGroup>


<ItemGroup>
<Folder Include="Http\" />
</ItemGroup>

<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
<Exec Command="node ../.scripts/init.js"/>
Expand Down
15 changes: 8 additions & 7 deletions Vonage/Request/ApiRequest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
Expand All @@ -11,6 +11,7 @@
using Newtonsoft.Json;
using Vonage.Common.Client;
using Vonage.Common.Exceptions;
using Vonage.Common.Monads;
using Vonage.Cryptography;
using Vonage.Logger;
using Vonage.Serialization;
Expand All @@ -22,25 +23,25 @@ internal partial class ApiRequest
private readonly Credentials credentials;
private readonly ILogger logger;
private readonly string userAgent;
private readonly Configuration configuration;
private readonly Maybe<Configuration> configuration;

private ApiRequest()
{
this.logger = LogProvider.GetLogger("Vonage.Request.ApiRequest");
this.userAgent = UserAgentProvider.GetFormattedUserAgent(this.GetConfiguration().UserAgent);
}

public ApiRequest(Credentials credentials) : this()
private ApiRequest(Credentials credentials) : this()
{
this.credentials = credentials;
this.userAgent = UserAgentProvider.GetFormattedUserAgent(this.GetUserAgent());
}

private ApiRequest(Credentials credentials, Configuration configuration) : this(credentials) => this.configuration = configuration;

internal static ApiRequest Build(Credentials credentials, Configuration configuration) => new ApiRequest(credentials, configuration);

private Configuration GetConfiguration() => this.configuration ?? Configuration.Instance;
private Configuration GetConfiguration() => this.configuration.IfNone(Configuration.Instance);

public async Task<VonageResponse> DoDeleteRequestWithUrlContentAsync(Uri uri,
Dictionary<string, string> parameters, AuthType authType = AuthType.Query) =>
Expand All @@ -60,7 +61,7 @@ public async Task<HttpResponseMessage> DoGetRequestWithJwtAsync(Uri uri)
catch (HttpRequestException ex)
{
this.logger.LogError("FAIL: {StatusCode}", result.StatusCode);
throw new VonageHttpRequestException(ex) {HttpStatusCode = result.StatusCode};
throw new VonageHttpRequestException(ex) { HttpStatusCode = result.StatusCode };
}
}

Expand Down Expand Up @@ -327,7 +328,7 @@ public enum UriType
}

internal static Uri GetBaseUriFor(string url = null) =>
string.IsNullOrEmpty(url) ? Configuration.Instance.RestApiUrl : new Uri( Configuration.Instance.RestApiUrl, url);
string.IsNullOrEmpty(url) ? Configuration.Instance.RestApiUrl : new Uri(Configuration.Instance.RestApiUrl, url);

internal async Task<T> DoRequestWithJsonContentAsync<T>(HttpMethod method, Uri uri, object payload,
AuthType authType, Func<object, string> payloadSerialization,
Expand Down

0 comments on commit dc3498e

Please sign in to comment.