REST and APIs

Calling REST Endpoints with GPAL.RESTClient

Complete Program

Here's the whole workflow, start to finish. Each piece is broken down and explained below.

using GenerallyPositive;

using GenerallyPositive.Browser;

using static GenerallyPositive.Enums;


GPAL.WithPublishToConsole().WithPublishStackTrace();


IBrowser browser = GPAL.Browser

.WithBrowserType(BrowserType.Chrome)

.WithAutomationEngine(AutomationEngine.OttoMagic)

.WithDriverLocation(@"C:drivers")

.ToGPALObject();


browser.Get(""); // launches with the default page


IRESTClient client = browser.OttoMagicClient;


// open a couple of tabs and navigate

var result = client.GoTo("https://example.com").Execute();

result = client.CheckNetworkIdle().Execute();

result = client.NewTab("https://example.com").Execute();

result = client.CheckNetworkIdle().Execute();


// fill in a search box and submit

result = client.FillInOverwrite("#search-input").WithText("hello world").Execute();

result = client.SendKey(0x0D).Execute(); // Enter submits the form the input is in


// read back simple page state

string url = client.GetCurrentUrl().Execute<string>();

bool clickable = client.IsClickable("#search-input").Execute<bool>();


browser.Close(true);

Configure GPAL and Get an IRESTClient

browser.OttoMagicClient is the ready-made IRESTClient the browser keeps for talking to the OttoMagic extension's REST API, already pointed at browser.RestApiBaseUrl. It is a property rather than a factory, so there is nothing to build: ask the browser for it and start chaining.

IRESTClient client = browser.OttoMagicClient;

NOTE

OttoMagicClient is just a pre-configured RESTClient. The same With*/Execute pattern - WithAPIBase, WithEndpoint, WithHttpMethod, and so on - works against any REST service, not just GPAL's own.

Every Call Ends in Execute

Each call describes one request: GoTo navigates the remote browser, CheckNetworkIdle waits for network activity to settle, and NewTab opens another tab. Calling Execute() sends the request and returns the result. Chaining these in sequence builds up a multi-step workflow against a single remote browser session.

var result = client.GoTo("https://example.com").Execute();

result = client.CheckNetworkIdle().Execute();

result = client.NewTab("https://example.com").Execute();

result = client.CheckNetworkIdle().Execute();

Filling In Forms and Reading State

FillInOverwrite targets an element by selector and WithText supplies what to type, replacing any existing value. SendKey sends one key by its virtual key code, and 0x0D is Enter, which submits the form the input sits in. Execute<T>() is the typed variant - GetCurrentUrl().Execute<string>() returns a string, IsClickable(...).Execute<bool>() returns a bool, so you can use the result directly in your C# code.

result = client.FillInOverwrite("#search-input").WithText("hello world").Execute();

result = client.SendKey(0x0D).Execute(); // Enter submits the form the input is in


string url = client.GetCurrentUrl().Execute<string>();

bool clickable = client.IsClickable("#search-input").Execute<bool>();

NOTE

Use the generic Execute() when you need a strongly-typed result (string, bool, int, or a custom object). Plain Execute() returns a dynamic result, handy when you just want to confirm the call ran or don't need the response.

Closing Down

When every REST call is done, Close(true) on the IBrowser tears down the underlying driver process that OttoMagic was controlling.

browser.Close(true);

WARNING

Passing true kills the running web driver process. Use true when the workflow is completely done; use false if another workflow in the same run still needs that driver.

💬 Ask GPAL