11// Root myDeserializedClass = JsonSerializer.Deserialize<Root>(myJsonResponse);
22
3+ using System . Text . Json ;
34using System . Text . Json . Serialization ;
45using Flurl . Http ;
6+ using Flurl . Http . Configuration ;
57
68namespace SmartImage . Lib . Clients ;
79
10+ public class FlareSolverrClient : IDisposable
11+ {
12+
13+ public const string CMD_REQUEST_GET = "request.get" ;
14+ public const string CMD_REQUEST_POST = "request.post" ;
15+
16+ public const int DEFAULT_MAX_TIMEOUT = 60000 ;
17+
18+ private static readonly JsonSerializerOptions s_jsonSerializerOptions = new ( JsonSerializerOptions . Default )
19+ {
20+ DefaultIgnoreCondition =
21+ JsonIgnoreCondition . WhenWritingDefault
22+ } ;
23+
24+ private static readonly DefaultJsonSerializer s_settingsJsonSerializer = new ( s_jsonSerializerOptions )
25+ { } ;
26+
27+ // TODO: FlareSolverrSharp doesn't work 10/8/24
28+
29+ public FlurlClient Client { get ; }
30+
31+ public string BaseUrl { get ; }
32+
33+ public FlareSolverrClient ( string baseUrl )
34+ {
35+ BaseUrl = baseUrl ;
36+
37+ Client = ( FlurlClient ) FlurlHttp . Clients . GetOrAdd ( nameof ( FlareSolverrClient ) , BaseUrl , builder =>
38+ {
39+ builder . AllowAnyHttpStatus ( ) ;
40+
41+ builder . Settings . JsonSerializer = s_settingsJsonSerializer ;
42+ } ) ;
43+
44+ }
45+
46+ public async ValueTask < bool > IsConnectedAsync ( int maxTimeout = DEFAULT_MAX_TIMEOUT )
47+ {
48+ try {
49+ using var res = await SendAsync ( "https://nowsecure.nl/" , CMD_REQUEST_GET , maxTimeout ) ;
50+
51+ if ( res == null ) {
52+ return false ;
53+ }
54+
55+ var obj = await res . GetJsonAsync < FlareSolverrRoot > ( ) ;
56+ return obj . Status == "ok" ;
57+ }
58+ catch {
59+ return false ;
60+ }
61+ finally { }
62+
63+
64+ }
65+
66+ public static readonly FlareSolverrClient Instance = new ( "http://localhost:8191/v1" ) ;
67+
68+ public Task < IFlurlResponse > SendAsync ( FlareSolverrRequest request )
69+ {
70+ var resp = Client . Request ( )
71+ . PostJsonAsync ( request ) ;
72+
73+ return resp ;
74+ }
75+
76+ public Task < IFlurlResponse > SendAsync ( string url , string cmd , int maxTimeout = DEFAULT_MAX_TIMEOUT )
77+ {
78+
79+ return SendAsync ( new FlareSolverrRequest ( ) { Url = url , Command = cmd , MaxTimeout = maxTimeout } ) ;
80+
81+ }
82+
83+ public void Dispose ( )
84+ {
85+ Client ? . Dispose ( ) ;
86+ }
87+
88+ }
89+
90+ #region API Objects
91+
92+ public record FlareSolverrRequest
93+ {
94+
95+ // todo
96+
97+ [ JsonPropertyName ( "cmd" ) ]
98+ public string Command { get ; set ; }
99+
100+ public List < FlareSolverrCookie > Cookies { get ; set ; }
101+
102+ public int MaxTimeout { get ; set ; }
103+
104+ public Dictionary < string , object > Proxy { get ; set ; } //todo
105+
106+ public string Session { get ; set ; }
107+
108+ [ JsonPropertyName ( "session_ttl_minutes" ) ]
109+ public int SessionTtl { get ; set ; }
110+
111+ public string Url { get ; set ; }
112+
113+ public string PostData { get ; set ; }
114+
115+ public bool ReturnOnlyCookies { get ; set ; }
116+
117+
118+ public FlareSolverrRequest ( ) { }
119+
120+ }
121+
8122public class FlareSolverrCookie
9123{
10124
@@ -130,43 +244,4 @@ public class FlareSolverrSolution
130244
131245}
132246
133- public class FlareSolverrClient : IDisposable
134- {
135-
136- // TODO: FlareSolverrSharp doesn't work 10/8/24
137-
138- public FlurlClient Client { get ; }
139-
140- public string BaseUrl { get ; }
141-
142- public FlareSolverrClient ( string baseUrl )
143- {
144- BaseUrl = baseUrl ;
145- Client = new FlurlClient ( BaseUrl ) ;
146- }
147-
148- public static readonly FlareSolverrClient Instance = new ( "http://localhost:8191/v1" ) ;
149-
150- public Task < IFlurlResponse > Get ( string url , string cmd , int maxTimeout = 60000 )
151- {
152- var body = new
153- {
154- cmd = cmd ,
155- url = url ,
156- maxTimeout = maxTimeout
157- } ;
158-
159- var resp = Client . Request ( )
160- . WithHeader ( "Accept" , "text/html; charset=utf-8" )
161- . PostJsonAsync ( body ) ;
162-
163- return resp ;
164-
165- }
166-
167- public void Dispose ( )
168- {
169- Client ? . Dispose ( ) ;
170- }
171-
172- }
247+ #endregion
0 commit comments