/// <summary> /// Initializes a new JsonClient. /// </summary> /// <param name="baseUrl">The base url to be prefixed to every request.</param> /// <param name="options">Options to be passed into every request.</param> /// <param name="client">Client to pass in - only used for tests really.</param> public JsonClient(string baseUrl, HttpClientOptions options = null) { options = options ?? new HttpClientOptions(); options.Headers.Add("Accept", "application/json"); Client = new HttpClient(baseUrl, options); _jss = new JsonSerializerSettings { ContractResolver = new DefaultContractResolver { NamingStrategy = new SnakeCaseNamingStrategy() } }; }
private TimeSpan getTimeout(HttpClientOptions options, HttpClientOptions domOpts) { if (domOpts?.Timeout != null) { return(domOpts.Timeout); } if (options?.Timeout != null) { return(options.Timeout); } // Should never happen return(TimeSpan.FromSeconds(2)); }
private async Task EnsureBrowserReady() { if (_config.RemoteEndpoint == null) { Browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true }); return; } var chromeClient = new JsonClient($"http://{_config.RemoteEndpoint}"); var options = new HttpClientOptions { Headers = new Dictionary <string, string> { { "Host", "0.0.0.0" } } }; var meta = await chromeClient.Do <ChromeInstanceMeta>("GET", "/json/version", options); var url = meta.WebSocketDebuggerUrl; var id = url.Split("/").Last(); var finalRemoteUrl = $"ws://{_config.RemoteEndpoint}/devtools/browser/{id}"; Browser = await Puppeteer.ConnectAsync(new ConnectOptions { BrowserWSEndpoint = finalRemoteUrl }); }
public async Task <TRes> Do <TRes>(string verb, string path, Dictionary <string, string> query, object body, HttpClientOptions newOpts = null) where TRes : class { HttpContent content = null; if (body != null) { content = new StringContent(JsonConvert.SerializeObject(body, _jss), Encoding.UTF8, "application/json"); } var response = await Client.Do(verb, path, query, content, newOpts); var str = await response.Content.ReadAsStringAsync(); Console.WriteLine(str); if (response.IsSuccessStatusCode) { if (String.IsNullOrWhiteSpace(str)) { return(null); } return(JsonConvert.DeserializeObject <TRes>(str)); } var isJson = response.Content.Headers?.ContentType?.MediaType == "application/json"; var hasContent = !String.IsNullOrWhiteSpace(str); if (isJson && hasContent) { throwIfCrpcException(str); } response.EnsureSuccessStatusCode(); // NOTE(afr): This shouldn't be possible to hit throw new InvalidOperationException("Not poss"); }
public async Task <TRes> Do <TRes>(string verb, string path, object body, HttpClientOptions newOpts = null) where TRes : class { return(await Do <TRes>(verb, path, null, body, newOpts)); }
public async Task <TRes> Do <TRes>(string verb, string path, Dictionary <string, string> query = null, HttpClientOptions newOpts = null) where TRes : class { return(await Do <TRes>(verb, path, query, null, newOpts)); }