public async Task <RestResponse> SendAsync(HttpRequestMessage message) { var response = await _client.SendAsync(message); RestResponse restResponse = new RestResponse(); restResponse.HttpResponseMessage = response; restResponse.Body = await response.Content.ReadAsStringAsync(); restResponse.Success = response.IsSuccessStatusCode; if (restResponse.Success) { OnRequestComplete?.Invoke( message.Method.Method.ToString(), message.RequestUri.AbsolutePath ); } if (_ensureSuccess) { response.EnsureSuccessStatusCode(); } return(restResponse); }
public async ValueTask <HttpResponse> SendAsync(HttpRequestMessage message) { if (_rateLimiter != null) { await _rateLimiter.WaitAsync( message.Method.Method, message.RequestUri.ToString() ); } var response = await _client.SendAsync(message); var restResponse = new HttpResponse { HttpResponseMessage = response, Body = await response.Content.ReadAsByteArrayAsync(), Success = response.IsSuccessStatusCode }; if (restResponse.Success) { if (_rateLimiter != null) { await _rateLimiter.OnRequestSuccessAsync(restResponse); } OnRequestComplete?.Invoke( message.Method.Method, message.RequestUri.AbsolutePath ); } if (_ensureSuccess) { response.EnsureSuccessStatusCode(); } return(restResponse); }
public IEnumerator PagedRequest(int per_page, string api_path) { int page = 1; JSONNode data = JSONNode.Parse("[]"); string response = null; JSONNode responseJSON = null; do { UnityWebRequest request = UnityWebRequest.Get(API_URL + api_path + "&page=" + page + "&per_page=" + per_page); PrepareHeaders(request); yield return(VimeoApi.SendRequest(request)); if (request.error != null) { if (request.responseCode == 401) { SendError("401 Unauthorized request. Are you using a valid token?", request.downloadHandler.text); } else if (IsNetworkError(request)) { Debug.LogError("[VimeoApi] It seems like you are not connected to the internet or are having connection problems."); OnNetworkError?.Invoke(request.error); } else { if (page > 1) { // It's OK, make it the end of pages Debug.LogWarning("[VimeoApi] Paged request error for url " + request.url + ", at page #" + page + ", error: " + request.error + ", " + request.downloadHandler.text); break; } else { SendError(request.url + " - " + request.downloadHandler.text, request.downloadHandler.text); } } yield break; } response = request.downloadHandler.text; responseJSON = JSONNode.Parse(response); JSONNode pageData = responseJSON["data"]; for (int i = 0; i < pageData.Count; i++) { JSONNode element = pageData[i]; data.Add(element); } if (pageData.Count < per_page) { break; } page++; }while (true); if (responseJSON != null) { responseJSON.Remove("data"); responseJSON.Add("data", data); } OnRequestComplete?.Invoke(responseJSON.ToString()); }
private void Client_OnEvent(string method, Newtonsoft.Json.Linq.JToken args) { Frame frame = null; switch (method) { case "Page.navigatedWithinDocument": var newUrl = args["url"].ToString(); Frames.TryGetValue(args["frameId"].ToString(), out frame); OnUrlChanged?.Invoke(newUrl, frame); break; case "Page.frameNavigated": frame = args["frame"].ToObject <Frame>(); Frames[frame.id] = frame; OnNavigated?.Invoke(frame); break; case "Runtime.executionContextsCleared": OnPageDestroyed?.Invoke(); IsScreencastStarted = false; break; case "Page.frameStartedLoading": OnStartedLoading?.Invoke(args["frameId"].ToString()); break; case "Page.loadEventFired": OnLoaded?.Invoke(); break; case "Network.loadingFinished": var reqId = args["requestId"].ToString(); if (Responses.TryGetValue(reqId, out Response resp)) { OnRequestComplete?.Invoke(resp.Request, resp); } break; case "Network.responseReceived": reqId = args["requestId"].ToString(); if (Requests.TryGetValue(reqId, out Request req)) { Requests.Remove(reqId); } resp = args["response"].ToObject <Response>(); resp.Request = req; resp.requestId = reqId; Responses[reqId] = resp; // HACK: Chrome sucks and the response is not actually ready yet. // OnRequestComplete?.Invoke(req, resp); break; case "Network.requestWillBeSent": reqId = args["requestId"].ToString(); req = args.ToObject <Request>(); req.frameId = args["frameId"].ToString(); Frames.TryGetValue(req.frameId, out req.Frame); req.requestId = reqId; Requests[reqId] = req; OnRequestStarted?.Invoke(req); break; case "Page.screencastFrame": var sessionId = (int)args["sessionId"]; var t = Client.Send("Page.screencastFrameAck", new { sessionId }); MostRecentFrameBase64 = args["data"].ToString(); MostRecentFrameMetadata = args["metadata"].ToObject <ScreencastFrameMetadata>(); OnScreencastFrame?.Invoke(MostRecentFrameBase64, MostRecentFrameMetadata); break; } }