Skip to content

Commit fd544c4

Browse files
authored
Merge pull request #14 from sherweb/spons/UpdateSampleCode
Update SampleCode using the new API Client
2 parents 7bd9a0e + 17030dc commit fd544c4

File tree

2 files changed

+27
-27
lines changed

2 files changed

+27
-27
lines changed

SampleCode/Program.cs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Microsoft.Rest;
44
using Sherweb.Apis.Authorization;
55
using Sherweb.Apis.Distributor;
6+
using Sherweb.Apis.Distributor.Factory;
67
using Sherweb.Apis.Distributor.Models;
78

89
namespace Sherweb.SampleCode
@@ -11,13 +12,14 @@ public class Program
1112
{
1213
static void Main(string[] args)
1314
{
15+
const string baseUrl = "https://api.sherweb.com/distributor/v1";
1416
const string clientId = "your client id";
1517
const string clientSecret = "your client secret";
1618
const string subscriptionKey = "your subscription key";
17-
19+
1820
// Optional. This should follow [RFC 7231, section 5.3.5: Accept-Language]: https://tools.ietf.org/html/rfc7231#section-5.3.5
1921
// Example: en, en-CA;q=0.8, fr-CA;q=0.7
20-
const string acceptLanguageHeader = null;
22+
const string acceptLanguageHeader = null;
2123

2224
// Get Bearer Token from Authorization API
2325
var authorizationClient = new AuthorizationService(new Uri("https://api.sherweb.com/auth"));
@@ -29,36 +31,34 @@ static void Main(string[] args)
2931
"client_credentials");
3032

3133
// Instantiate Distributor API client using Bearer token
32-
var svcClientCreds = new TokenCredentials(token.AccessToken, "Bearer");
33-
var distributorClient = new DistributorService(
34-
new Uri("https://api.sherweb.com/distributor/v1"),
35-
svcClientCreds,
36-
new SubscriptionKeyHandler(subscriptionKey));
34+
var credentials = new TokenCredentials(token.AccessToken, "Bearer");
3735

38-
var response = distributorClient.GetPayableCharges(acceptLanguage: acceptLanguageHeader);
39-
if (response is ProblemDetails problemDetails)
36+
var distributorConfiguration = new DistributorServiceConfiguration
4037
{
41-
Console.WriteLine($"{nameof(problemDetails.Instance)}={problemDetails.Instance}");
42-
Console.WriteLine($"{nameof(problemDetails.Title)}={problemDetails.Title}");
43-
Console.WriteLine($"{nameof(problemDetails.Status)}={problemDetails.Status}");
44-
Console.WriteLine($"{nameof(problemDetails.Type)}={problemDetails.Type}");
45-
Console.WriteLine($"{nameof(problemDetails.Detail)}={problemDetails.Detail}");
38+
Uri = new Uri(baseUrl),
39+
Credentials = credentials,
40+
RetryCount = 0
41+
};
4642

47-
if (problemDetails.Extensions != null)
48-
{
49-
foreach (var extension in problemDetails.Extensions)
50-
{
51-
Console.WriteLine($"{nameof(extension.Key)}={extension.Key}");
52-
Console.WriteLine($"{nameof(extension.Value)}={extension.Value}");
53-
Console.WriteLine("-------------------------------------------------");
54-
}
55-
}
43+
var distributorServiceFactory = new DistributorServiceFactory(distributorConfiguration,
44+
new SubscriptionKeyHandler(subscriptionKey));
5645

46+
var distributorClient = distributorServiceFactory.Create();
47+
48+
PayableCharges payableCharges = null;
49+
50+
try
51+
{
52+
payableCharges = distributorClient.GetPayableCharges(acceptLanguage: acceptLanguageHeader);
53+
}
54+
catch (HttpOperationException exception)
55+
{
56+
// ProblemDetails returned by the API are handled and converted to a HttpOperationException in the ProblemDetailsHandler of the API Client
57+
// https://github.com/sherweb/Public-Apis/blob/7bd9a0ecc37f0fbe3d9085c3e911ade3ca9a0c66/NugetPackagesSourceCode/Sherweb.Apis.Distributor/DelegatingHandlers/OnProblemDetailsHandler.cs
58+
Console.WriteLine($"{nameof(exception.Message)}={exception.Message}");
5759
return;
5860
}
59-
60-
var payableCharges = (PayableCharges) response;
61-
61+
6262
Console.WriteLine($"{nameof(payableCharges.PeriodFrom)}={payableCharges.PeriodFrom}");
6363
Console.WriteLine($"{nameof(payableCharges.PeriodTo)}={payableCharges.PeriodTo}");
6464

SampleCode/Sherweb.SampleCode.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
<ItemGroup>
99
<PackageReference Include="Sherweb.Apis.Authorization" Version="1.0.0" />
10-
<PackageReference Include="Sherweb.Apis.Distributor" Version="1.0.0" />
10+
<PackageReference Include="Sherweb.Apis.Distributor" Version="1.1.0" />
1111
</ItemGroup>
1212

1313
</Project>

0 commit comments

Comments
 (0)