HTTP(S) - RestSharp
We love and use RestSharp throughout the project. It's the best possible way of interesting with any modern API. It provides an easy to use method to call, execute, and parse data back into structured classes.
With Perigee, we've integrated the RestSharp package directly as a dependency, and we've written a few Extension methods to further integrate it into our system.
First Call with RestSharp
Here's a demo of using RestSharp to call out to Postman-Echo
, and retrieve your IP address.
The steps are as follows:
Create a
RestClient
, which usually contains thebaseAddress
of the endpoint you're going to be connecting to (https://postman-echo.com
)Create a
RestRequest
, this contains information about the specific request being made.The Uri (
/ip
)The verb (
POST/GET/PATCH
)If you are adding body data, or query parameters (see demo usage for adding body, or using the other helper methods)
Call
Execute()
.
Log:
Authentication
This is an identical pattern to above, only this time we've added an authenticator, and changed the request uri.
Log:
Authentication with Perigee Credentials
This performs the same HTTPS request, only this time, we're using Perigee's Credential Store to manage, refresh, and obtain tokens. This gives a lot of flexibility on how to retrieve and maintain an active token state.
Part of the "secret sauce" here is that any time a credential is about to expire (within a few minutes), a preemptive request is sent to the registered refresh action to renew the token before it's expired.
This prevents an expired token from being sent to the API downstream
This prevents the need for complicated patterns when an error occurs due to expired tokens
These credentials are automatically persisted to disk, and reloaded on application start. This is explained in more detail in the Custom Refresh Logic section.
Some authentication sources will rate limit your authentication requests, this is another method to use to prevent this issue from occurring.
Log:
Extensions
There are a few notable extensions added by Perigee:
Execute Retry
This method has quite a few overloads you can call, but all of them do the same thing:
Retry a request if the method failed to connect, or if it timed out.
If the third party API suddenly revoked your current API credentials AND you are using a CredentialAuthenticator, it will fire the renewal for a new token before attempting another retry
IsTimeout
If the request timed out, this is an easy Boolean to check so you can proceed with other options.
Last updated