Skip to content

Commit

Permalink
Update SampleCode using the new API Client
Browse files Browse the repository at this point in the history
  • Loading branch information
sponsCA committed Mar 24, 2021
1 parent 7bd9a0e commit 17030dc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
52 changes: 26 additions & 26 deletions SampleCode/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.Rest;
using Sherweb.Apis.Authorization;
using Sherweb.Apis.Distributor;
using Sherweb.Apis.Distributor.Factory;
using Sherweb.Apis.Distributor.Models;

namespace Sherweb.SampleCode
Expand All @@ -11,13 +12,14 @@ public class Program
{
static void Main(string[] args)
{
const string baseUrl = "https://api.sherweb.com/distributor/v1";
const string clientId = "your client id";
const string clientSecret = "your client secret";
const string subscriptionKey = "your subscription key";

// Optional. This should follow [RFC 7231, section 5.3.5: Accept-Language]: https://tools.ietf.org/html/rfc7231#section-5.3.5
// Example: en, en-CA;q=0.8, fr-CA;q=0.7
const string acceptLanguageHeader = null;
const string acceptLanguageHeader = null;

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

// Instantiate Distributor API client using Bearer token
var svcClientCreds = new TokenCredentials(token.AccessToken, "Bearer");
var distributorClient = new DistributorService(
new Uri("https://api.sherweb.com/distributor/v1"),
svcClientCreds,
new SubscriptionKeyHandler(subscriptionKey));
var credentials = new TokenCredentials(token.AccessToken, "Bearer");

var response = distributorClient.GetPayableCharges(acceptLanguage: acceptLanguageHeader);
if (response is ProblemDetails problemDetails)
var distributorConfiguration = new DistributorServiceConfiguration
{
Console.WriteLine($"{nameof(problemDetails.Instance)}={problemDetails.Instance}");
Console.WriteLine($"{nameof(problemDetails.Title)}={problemDetails.Title}");
Console.WriteLine($"{nameof(problemDetails.Status)}={problemDetails.Status}");
Console.WriteLine($"{nameof(problemDetails.Type)}={problemDetails.Type}");
Console.WriteLine($"{nameof(problemDetails.Detail)}={problemDetails.Detail}");
Uri = new Uri(baseUrl),
Credentials = credentials,
RetryCount = 0
};

if (problemDetails.Extensions != null)
{
foreach (var extension in problemDetails.Extensions)
{
Console.WriteLine($"{nameof(extension.Key)}={extension.Key}");
Console.WriteLine($"{nameof(extension.Value)}={extension.Value}");
Console.WriteLine("-------------------------------------------------");
}
}
var distributorServiceFactory = new DistributorServiceFactory(distributorConfiguration,
new SubscriptionKeyHandler(subscriptionKey));

var distributorClient = distributorServiceFactory.Create();

PayableCharges payableCharges = null;

try
{
payableCharges = distributorClient.GetPayableCharges(acceptLanguage: acceptLanguageHeader);
}
catch (HttpOperationException exception)
{
// ProblemDetails returned by the API are handled and converted to a HttpOperationException in the ProblemDetailsHandler of the API Client
// https://github.com/sherweb/Public-Apis/blob/7bd9a0ecc37f0fbe3d9085c3e911ade3ca9a0c66/NugetPackagesSourceCode/Sherweb.Apis.Distributor/DelegatingHandlers/OnProblemDetailsHandler.cs
Console.WriteLine($"{nameof(exception.Message)}={exception.Message}");
return;
}

var payableCharges = (PayableCharges) response;


Console.WriteLine($"{nameof(payableCharges.PeriodFrom)}={payableCharges.PeriodFrom}");
Console.WriteLine($"{nameof(payableCharges.PeriodTo)}={payableCharges.PeriodTo}");

Expand Down
2 changes: 1 addition & 1 deletion SampleCode/Sherweb.SampleCode.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<ItemGroup>
<PackageReference Include="Sherweb.Apis.Authorization" Version="1.0.0" />
<PackageReference Include="Sherweb.Apis.Distributor" Version="1.0.0" />
<PackageReference Include="Sherweb.Apis.Distributor" Version="1.1.0" />
</ItemGroup>

</Project>

0 comments on commit 17030dc

Please sign in to comment.