ResponseInMemory
This commit is contained in:
parent
64d088c879
commit
b7dadd3159
|
|
@ -4,6 +4,6 @@ namespace BotSharp.Abstraction.Browsing;
|
|||
|
||||
public interface IWebPageResponseHook
|
||||
{
|
||||
void OnDataFetched(MessageInfo message, string url, string postData, string responsData);
|
||||
T? GetResponse<T>(MessageInfo message, string url, string? queryParameter = null);
|
||||
void OnDataFetched(MessageInfo message, WebPageResponseData response);
|
||||
T? GetResponse<T>(MessageInfo message, WebPageResponseFilter filter);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,6 +28,12 @@ public class PageActionArgs
|
|||
/// </summary>
|
||||
public string[]? IncludeResponseUrls { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// If set to true, the response will be stored in memory
|
||||
/// </summary>
|
||||
public bool ResponseInMemory { get; set; } = false;
|
||||
public List<WebPageResponseData>? ResponseContainer { get; set; }
|
||||
|
||||
public bool UseExistingPage { get; set; } = false;
|
||||
|
||||
public bool WaitForNetworkIdle { get; set; } = true;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,10 @@
|
|||
namespace BotSharp.Abstraction.Browsing.Models;
|
||||
|
||||
public class WebPageResponseData
|
||||
{
|
||||
public string Url { get; set; } = null!;
|
||||
public string PostData { get; set; } = null!;
|
||||
public string ResponseData { get; set; } = null!;
|
||||
public bool ResponseInMemory { get; set; }
|
||||
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
namespace BotSharp.Abstraction.Browsing.Models;
|
||||
|
||||
public class WebPageResponseFilter
|
||||
{
|
||||
public string Url { get; set; } = null!;
|
||||
public string[]? QueryParameters { get; set; }
|
||||
}
|
||||
|
|
@ -122,7 +122,12 @@ public class PlaywrightInstance : IDisposable
|
|||
return _contexts[ctxId];
|
||||
}
|
||||
|
||||
public async Task<IPage> NewPage(MessageInfo message, bool enableResponseCallback = false, string[]? excludeResponseUrls = null, string[]? includeResponseUrls = null)
|
||||
public async Task<IPage> NewPage(MessageInfo message,
|
||||
bool enableResponseCallback = false,
|
||||
bool responseInMemory = false,
|
||||
List<WebPageResponseData>? responseContainer = null,
|
||||
string[]? excludeResponseUrls = null,
|
||||
string[]? includeResponseUrls = null)
|
||||
{
|
||||
var context = await GetContext(message.ContextId);
|
||||
var page = await context.NewPageAsync();
|
||||
|
|
@ -162,7 +167,20 @@ public class PlaywrightInstance : IDisposable
|
|||
var webPageResponseHooks = _services.GetServices<IWebPageResponseHook>();
|
||||
foreach (var hook in webPageResponseHooks)
|
||||
{
|
||||
hook.OnDataFetched(message, e.Url.ToLower(), e.Request?.PostData ?? string.Empty, JsonSerializer.Serialize(json));
|
||||
var result = new WebPageResponseData
|
||||
{
|
||||
Url = e.Url.ToLower(),
|
||||
PostData = e.Request?.PostData ?? string.Empty,
|
||||
ResponseData = JsonSerializer.Serialize(json),
|
||||
ResponseInMemory = responseInMemory
|
||||
};
|
||||
|
||||
if (responseContainer != null && responseInMemory)
|
||||
{
|
||||
responseContainer.Add(result);
|
||||
}
|
||||
|
||||
hook.OnDataFetched(message, result);
|
||||
}
|
||||
}
|
||||
catch (ObjectDisposedException ex)
|
||||
|
|
|
|||
|
|
@ -11,6 +11,8 @@ public partial class PlaywrightWebDriver
|
|||
var page = args.UseExistingPage ?
|
||||
_instance.GetPage(message.ContextId, pattern: args.Url) :
|
||||
await _instance.NewPage(message, enableResponseCallback: args.EnableResponseCallback,
|
||||
responseInMemory: args.ResponseInMemory,
|
||||
responseContainer: args.ResponseContainer,
|
||||
excludeResponseUrls: args.ExcludeResponseUrls,
|
||||
includeResponseUrls: args.IncludeResponseUrls);
|
||||
|
||||
|
|
@ -25,14 +27,20 @@ public partial class PlaywrightWebDriver
|
|||
|
||||
if (args.UseExistingPage && args.OpenNewTab && page != null && page.Url == "about:blank")
|
||||
{
|
||||
page = await _instance.NewPage(message, enableResponseCallback: args.EnableResponseCallback,
|
||||
page = await _instance.NewPage(message,
|
||||
enableResponseCallback: args.EnableResponseCallback,
|
||||
responseInMemory: args.ResponseInMemory,
|
||||
responseContainer: args.ResponseContainer,
|
||||
excludeResponseUrls: args.ExcludeResponseUrls,
|
||||
includeResponseUrls: args.IncludeResponseUrls);
|
||||
}
|
||||
|
||||
if (page == null)
|
||||
{
|
||||
page = await _instance.NewPage(message, enableResponseCallback: args.EnableResponseCallback,
|
||||
page = await _instance.NewPage(message,
|
||||
enableResponseCallback: args.EnableResponseCallback,
|
||||
responseInMemory: args.ResponseInMemory,
|
||||
responseContainer: args.ResponseContainer,
|
||||
excludeResponseUrls: args.ExcludeResponseUrls,
|
||||
includeResponseUrls: args.IncludeResponseUrls);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue