18using System.Collections.Generic;
19using System.Text.RegularExpressions;
31 public string Url {
get;
set; }
46 internal string RequestId {
get;
set; }
51 public Dictionary<string, string>
Headers {
get;
set; } =
new Dictionary<string, string>();
79 public string Path {
get;
internal set; }
82 public string Method {
get;
internal set; } =
"GET";
85 public string Body {
get;
internal set; }
88 public string ContentType {
get;
internal set; } =
"application/json";
91 public List<KeyValuePair<string, string>>
Parameters {
get;
internal set; } =
new List<KeyValuePair<string, string>>();
94 public List<KeyValuePair<string, string>>
Headers {
get;
internal set; } =
new List<KeyValuePair<string, string>>();
106 public string PageToken {
get;
internal set; } =
"{page}";
112 public string Name {
get;
internal set; }
204 while (
true ==
Path?.Contains($
"{{{token}}}") ||
true ==
Body?.Contains($
"{{{token}}}"))
209 string inUrl = $
"([?&]{Regex.Escape(parameterName)}=)[^&]*";
210 string inJson =
"(\"" + Regex.Escape(parameterName) +
"\"\\s*:\\s*)(\"[^\"]*\"|[^,}\\s]+)";
212 if (
false ==
string.IsNullOrEmpty(
Path))
213 Path = Regex.Replace(
Path, inUrl,
"${1}" + $
"{{{token}}}");
215 if (
false ==
string.IsNullOrEmpty(
Body))
217 Body = Regex.Replace(
Body, inUrl,
"${1}" + $
"{{{token}}}");
218 Body = Regex.Replace(
Body, inJson,
"${1}" + $
"\"{{{token}}}\"");
232 Headers.Add(SplitPair(nameValue,
':'));
302 public IAllowToGPALObject<GPALRequest>
WithName(
string name)
316 internal string ResolveUrl(
int page, List<string> row)
318 List<string> query =
new List<string>();
320 foreach (KeyValuePair<string, string> parameter
in Parameters)
321 query.Add($
"{Uri.EscapeDataString(parameter.Key)}={Uri.EscapeDataString(Resolve(parameter.Value ?? string.Empty, page, row))}");
323 string path = Resolve(
Path, page, row);
325 return 0 == query.Count
327 : path + (path?.Contains(
"?") ??
false ?
"&" :
"?") +
string.Join(
"&", query);
337 internal string ResolveBody(
int page, List<string> row)
339 return Resolve(
Body, page, row);
351 string Resolve(
string text,
int page, List<string> row)
355 if (
null != resolved &&
null != row)
356 for (
int token = 0; token < row.Count; token++)
357 resolved = resolved.Replace($
"{{{token}}}", row[token] ??
string.Empty);
367 internal int TokenCount()
371 foreach (
string text
in TokenizedText())
372 foreach (Match match
in Regex.Matches(text ??
string.Empty,
@"\{(\d+)\}"))
374 highest = Math.Max(highest,
int.Parse(match.Groups[1].Value));
384 List<string> TokenizedText()
386 List<string> text =
new List<string> {
Path,
Body };
388 foreach (KeyValuePair<string, string> parameter
in Parameters)
389 text.Add(parameter.Value);
391 foreach (KeyValuePair<string, string> header
in Headers)
392 text.Add(header.Value);
404 internal string[] HeaderPairs(
int page, List<string> row)
406 List<string> pairs =
new List<string>();
408 foreach (KeyValuePair<string, string> header
in Headers)
410 pairs.Add(header.Key);
411 pairs.Add(Resolve(header.Value, page, row));
414 return pairs.ToArray();
424 static KeyValuePair<string, string> SplitPair(
string pair,
char separator)
426 KeyValuePair<string, string> split =
new KeyValuePair<string, string>(pair,
string.Empty);
427 int at = pair?.IndexOf(separator) ?? -1;
430 split =
new KeyValuePair<string, string>(pair.Substring(0, at).Trim(), pair.Substring(at + 1).Trim());
Browser object that contains the fluent methods to create your Browser workflow. Instatiated using G...
delegate CallIfStatus CallAfterFetchDelegate(IBrowser browser, List< string > results, IGPALGrid< string > tokens, int tokenIdx)
Delegate callback for the CallAfterFetch handler declared on a GPALRequest, invoked after each row of...
One request the page made, as reported by the browser while .CaptureCalls was on. Where a GPALRequest...
string ResourceType
What the browser was fetching it as: XHR, Fetch, Document, Script, Image and the rest.
int Status
The HTTP status the site answered with, or 0 when nothing came back yet. What the page itself got,...
string Initiator
What set it off: parser, script, preload or other.
string Url
The full url requested.
string PostData
The request body, when it had one. Null for a GET.
Dictionary< string, string > Headers
The headers the page sent with it. What its own code added is here, which is what an API expects and ...
string Method
The HTTP method, GET, POST and so on.
Describes an API request for .Fetch to issue from inside the page, so it carries the session the brow...
IAllowRequestSettings WithContentType(Enums.ContentType contentType)
Content type of the body. application/json when nothing was said.
string ContentType
Content type of the body.
IAllowRequestSettings WithParameter(string nameValue)
Adds a query parameter, written as "name=value". Call once per parameter.
Browser.Browser.CallAfterFetchDelegate AfterFetch
Called once per row of tokens, with that row's pages. Null when nothing was said.
IAllowToGPALObject< GPALRequest > WithName(string name)
Names the request for messages and errors. Narrows the interface, so it goes last.
string PageToken
Marks where the page number goes. Put it anywhere .Fetch sends: the path, a parameter value,...
IAllowRequestSettings WithHeader(string nameValue)
Adds a request header, written as "Name: value". Call once per header. The browser sends its own head...
IAllowRequestSettings WithHttpMethod(Enums.HttpVerb method)
HTTP method to use, for example GET, POST, PUT or DELETE. GET when nothing was said.
IAllowRequestSettings WithPath(string path)
Path or full URL to request. A path such as "/api/search" is resolved against the origin the browser ...
IAllowRequestSettings WithBody(string body)
Body to send, used as-is. Sets the content type to application/json unless .WithContentType says othe...
IAllowRequestSettings WithTokenFor(string parameterName)
Replaces the value of a query parameter with the next numbered token, so a request taken from a page ...
string Method
HTTP method. GET when nothing was said.
List< KeyValuePair< string, string > > Headers
Extra request headers, on top of whatever the browser sends for itself.
List< KeyValuePair< string, string > > Parameters
Query parameters, in the order they were added.
string Path
Path or full URL to request. A path rides the origin the browser is already on.
string Name
Name used in messages and errors.
IAllowRequestSettings WithPageToken(string token="{page}")
The marker replaced with the page number, wherever it appears in the path, a parameter value,...
IAllowRequestSettings WithFirstPage(int firstPage)
What this API calls its first page, which is what the page token counts on from. Zero for Algolia and...
IAllowRequestSettings CallAfterFetch(Browser.Browser.CallAfterFetchDelegate callAfterFetch)
Called after each row of tokens has been fetched, with that row's pages, so the workflow can work the...
int FirstPage
What the API calls its first page. Zero for Algolia and most search backends, one for many others....
string Body
Request body, sent as-is.
GPALRequest ToGPALObject()
Returns this request, so a chain can be assigned without an explicit cast.