Tutorials

Tutorials

Calling REST Endpoints with GPAL.RESTClient

GPAL.RESTClient gives you a fluent, chainable client for talking to any REST API - including GPAL's own OttoMagic browser-automation API. This tutorial drives a remote browser purely through REST calls: opening tabs, navigating, filling in forms, scrolling, and reading page state, all with the same With*/Execute pattern.

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.WithUseOttoMagic(@"C:OttoMagic").WithPublishToConsole().WithPublishStackTrace();

IBrowser browser = GPAL.Browser

.WithBrowserType(BrowserType.Chrome)

.WithUseAutomationEngine(AutomationEngine.OttoMagic)

.WithDriverLocation(@"C:drivers")

.ToGPALObject();

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

IRESTClient client = GPAL.OttoMagicClient

.WithName("OttoMagicClient")

.ToGPALObject();

// 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.SubmitForm("#search-form").Execute();

// 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

WithUseOttoMagic points GPAL at the OttoMagic engine, which exposes a REST API for browser automation. GPAL.OttoMagicClient is a ready-made IRESTClient targeting that API - WithName labels it for logging and ToGPALObject() returns the live client object.

GPAL.WithUseOttoMagic(@"C:OttoMagic").WithPublishToConsole().WithPublishStackTrace();

IRESTClient client = GPAL.OttoMagicClient

.WithName("OttoMagicClient")

.ToGPALObject();

TIP

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. SubmitForm submits the enclosing form. 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.SubmitForm("#search-form").Execute();

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

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

TIP

Use the generic Execute<T>() 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.