LogoLogo
HomePricingDocumentation
  • 💿Getting Started
    • Installation and Project Setup
    • Hello Perigee!
    • Perigee Application Design
    • Hello Configuration
    • Hello Logs
    • Hello Integration
    • Troubleshooting
    • Case Studies
  • 📃License + Notice
    • 📂Licensing
    • Notice of Third Party Agreements
  • 🚀Perigee and Beyond
    • Extending - Threads
    • Extending - Loaders
    • ⏳All about CRON
  • 🔮API Generation
    • What is API Generation?
    • API Builder
  • 🗺️Architecting YOUR App
    • Design and Requirements
    • Define Sources
    • Requirements
  • 🧩Core Modules
    • 🌐PerigeeApplication
    • 🪡Thread Registry
    • Event Sources
      • Scheduled/Logic
        • CRON Thread
        • Scheduler
        • Sync Agent
      • Watchers
        • SalesForce
        • Sharepoint
        • Directory Watch
        • Directory Notifier
        • IMAP
    • Credential Management
      • Connection Strings
      • Custom Refresh Logic
      • RestSharp Authenticator
      • Credential Store SDK
      • ⁉️Troubleshooting Credentials
    • Integration Utilities
      • HTTP(S) - RestSharp
      • Transaction Coordinator
      • Limiter
      • Watermarking
    • Alert Managers
      • SMS
      • Email
      • Discord
      • Teams
    • File Formats
      • Excel
      • CSV
    • 📁File System Storage
      • File Revision Store
      • Concurrent File Store
      • FileSync + Cache
    • Third Party
      • SmartSheets
      • Microsoft Graph
    • Perigee In Parallel
      • Parallel Processing Reference
      • Extensions
      • GroupProcessor
      • SingleProcessor
    • 🧱Utility Classes
      • Metrics
      • F(x) Expressions
      • Multi-Threaded Processor (Scatter Gather)
      • OpenAI - GPT
      • XML Converter
      • Dynamic Data Table
      • Debounce
      • Thread Conditions
      • Perigee Utility Class
      • Network Utility
      • Lists
      • FileUtil
      • Inclusive2DRange
      • Strings, Numbers, Dates
      • Nested Sets
      • Behavior Trees
      • JsonCompress
      • Topological Sorting
      • DBDownloader
    • 🈁Bit Serializer
  • 📣Examples and Demos
    • API + Perigee
    • 📰Excel Quick Load
    • SalesForce Watcher
    • Report Scheduler
    • Agent Data Synchronization
    • 📩IMAP Echo bot
    • Watch and load CSVs
    • Graph Delegated Authorization + DataVerse
    • Coordinator Demo
    • Azure Service Bus
    • QuickBooks Online
  • 📘Blueprints
    • Perigee With .NET Hosting
    • Web Host Utilities
    • 🔌Plugin Load Context
  • 🎞️Transforms
    • 🌟What is Transforms?
    • 📘Terminology
    • 🦾The Mapping Document
    • 👾Transformation Process
    • 😎Profile
    • 🎒Automation
      • 🕓Package Options
      • 🔳Configuration
    • 🔧Utilities
      • 🧹Clean
      • 📑Map File
      • 🔎File Identification
      • 🗺️Map Generation
      • 🪅Insert Statement Generation
  • 🗃️Transform SDK
    • 👋Quick Start Guide
    • 🥳MapTo
    • 🔌Authoring Plugins
      • 🔘File IO Process
      • 📢Data Quality
      • 🟢Transform Process
    • SDK Reference
      • 🔘FileIOProcessData
      • 📢DataQualityContext
      • 🎛️TransformDataContext
      • 🏅TransformResult
Powered by GitBook
On this page
  • First Call with RestSharp
  • Authentication
  • Authentication with Perigee Credentials
  • Extensions
  • Execute Retry
  • IsTimeout
Export as PDF
  1. Core Modules
  2. Integration Utilities

HTTP(S) - RestSharp

PreviousIntegration UtilitiesNextTransaction Coordinator

Last updated 4 months ago

We love and use 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 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:

  1. Create a RestClient, which usually contains the baseAddress of the endpoint you're going to be connecting to (https://postman-echo.com)

  2. Create a RestRequest, this contains information about the specific request being made.

    1. The Uri (/ip)

    2. The verb (POST/GET/PATCH)

    3. If you are adding body data, or query parameters (see for adding , or using the other )

  3. Call Execute().

PerigeeApplication.ApplicationNoInit("HTTP", (c) => {

    //Declare a client, assign the timeout to 10 seconds
    using var client = new RestClient(new RestClientOptions("https://postman-echo.com") { MaxTimeout = 10000 });
    
    //Create a request
    var req = new RestRequest("/ip", Method.Get);
    
    //Execute
    var rsp = client.Execute<postmanEchoIP>(req);

    if (rsp.IsSuccessful)
        c.GetLogger<Program>().LogInformation("Response [{code}]: {ip}", (int)rsp.StatusCode, rsp.Data?.ip ?? "");
    else
        c.GetLogger<Program>().LogInformation("Response [{code}]: {content}", (int)rsp.StatusCode, rsp.Content);

});

public class postmanEchoIP { public string ip { get; set; } }

Log:

[15:11:51 INF]() Response [200]: 100.200.0.123

Authentication

This is an identical pattern to above, only this time we've added an authenticator, and changed the request uri.

PerigeeApplication.ApplicationNoInit("HTTP", (c) => {

    //Declare a client, assign the timeout to 10 seconds
    var rco = new RestClientOptions("https://postman-echo.com")
    {
        MaxTimeout = 10000,

        //Use a basic authenticator
        Authenticator = new RestSharp.Authenticators.HttpBasicAuthenticator("postman", "password")
    };

    using var client = new RestClient(rco);

    //Create a request
    var req = new RestRequest("/basic-auth", Method.Get);

    //Execute
    var rsp = client.Execute(req);

    //Log
    c.GetLogger<Program>().LogInformation("Response [{code}]: {content}", (int)rsp.StatusCode, rsp.Content);

});

Log:

[15:14:02 INF]() Response [200]: {
  "authenticated": true
}

Authentication with Perigee Credentials

  1. 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.

    1. This prevents an expired token from being sent to the API downstream

    2. This prevents the need for complicated patterns when an error occurs due to expired tokens

    1. Some authentication sources will rate limit your authentication requests, this is another method to use to prevent this issue from occurring.

PerigeeApplication.ApplicationNoInit("HTTP", (c) => {

    //Register a refresh with the name "RestSharpToken"
    CredentialStore.RegisterRefresh("RestSharpToken", (o) => {

        //Add any logic you need to here, including:
        // 1. Pulling configuration values (like so: c.GetValue<string>("AppSettings:AuthToken") )
        // 2. Executing other authorization requests to obtain a token
        // 3. Reaching out to another library
        // 4. etc

        //In this example we use a basic authentication header, many modern authentication API's now use a Bearer token:
        //      new RestSharp.Authenticators.JwtAuthenticator("eyJ.........");

        return new RestSharpCredentialStoreItem(new RestSharp.Authenticators.HttpBasicAuthenticator("postman", "password"), Expiration: DateTimeOffset.Now.AddSeconds(3600));
    });

    //Declare a client, assign the timeout to 10 seconds
    var rco = new RestClientOptions("https://postman-echo.com")
    {
        MaxTimeout = 10000,

        //Use a Credential Authenticator instead...
        Authenticator = new CredentialAuthenticator("RestSharpToken")
    };

    using var client = new RestClient(rco);

    //Create a request
    var req = new RestRequest("/basic-auth", Method.Get);

    //Execute
    var rsp = client.Execute(req);

    //Log
    c.GetLogger<Program>().LogInformation("Response [{code}]: {content}", (int)rsp.StatusCode, rsp.Content);

});

Log:

[15:16:26 INF]() Response [200]: {
  "authenticated": true
}

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:

  1. Retry a request if the method failed to connect, or if it timed out.

//Execute a retry a total of 2 times (1 original attempt, 1 retry)
var rsp = client.ExecuteRetry<postmanEchoIP>(req, 2);

IsTimeout

If the request timed out, this is an easy Boolean to check so you can proceed with other options.

//Check if a timeout occured
bool isTimeout = rsp.IsTimeout();

This performs the same HTTPS request, only this time, we're using Perigee's to manage, refresh, and obtain tokens. This gives a lot of flexibility on how to retrieve and maintain an active token state.

These credentials are automatically persisted to disk, and reloaded on application start. This is explained in more detail in the section.

If the third party API suddenly revoked your current API credentials AND you are using a , it will fire the renewal for a new token before attempting another retry

RestSharp
RestSharp
demo usage
body
helper methods
Credential Store
Custom Refresh Logic
CredentialAuthenticator
🧩
Page cover image