public void Fire(Bucket bucket) { this.bucket = bucket; this.InProgress = true; this.StartTime = DateTime.UtcNow; var req = WebRequest.Create(RequestURL); req.Method = Method.ToString(); req.ContentType = "application/json"; req.Timeout = 5000; if (this.Headers != null) { req.SetRawHeaders(this.Headers); } if (this.Data != null) { WriteRequestData(req, this.Data); } else { req.ContentLength = 0; } HttpWebResponse response; try { response = req.GetResponse() as HttpWebResponse; } catch (WebException ex) { var httpResponse = ex.Response as HttpWebResponse; if (httpResponse == null) { Interface.Oxide.LogException($"[Discord Ext] A web request exception occured (internal error).", ex); Interface.Oxide.LogError($"[Discord Ext] Request URL: [{Method.ToString()}] {RequestURL}"); Interface.Oxide.LogError($"[Discord Ext] Exception message: {ex.Message}"); this.Close(false); return; } string message = this.ParseResponse(ex.Response); Interface.Oxide.LogWarning($"[Discord Ext] An error occured whilst submitting a request to {req.RequestUri} (code {httpResponse.StatusCode}): {message}"); if ((int)httpResponse.StatusCode == 429) { Interface.Oxide.LogWarning($"[Discord Ext] Ratelimit info: remaining: {bucket.Remaining}, limit: {bucket.Limit}, reset: {bucket.Reset}, time now: {Helpers.Time.TimeSinceEpoch()}"); } httpResponse.Close(); bool shouldRemove = (int)httpResponse.StatusCode != 429; this.Close(shouldRemove); return; } this.ParseResponse(response); try { Callback?.Invoke(this.Response); } catch (Exception ex) { Interface.Oxide.LogException("[Discord Ext] Request callback raised an exception", ex); } finally { this.Close(); } }
public void Fire(Bucket bucket) { _bucket = bucket; InProgress = true; StartTime = DateTime.UtcNow; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(RequestUrl); req.Method = Method.ToString(); req.ContentType = "application/json"; req.Timeout = RequestMaxLength * 1000; req.ContentLength = 0; if (Headers != null) { req.SetRawHeaders(Headers); } try { //Can timeout while writing request data if (Data != null) { WriteRequestData(req, Data); } using (HttpWebResponse response = req.GetResponse() as HttpWebResponse) { if (response != null) { ParseResponse(response); } } Callback?.Invoke(Response); Close(); } catch (WebException ex) { using (HttpWebResponse httpResponse = ex.Response as HttpWebResponse) { if (httpResponse == null) { _logger.LogException($"A web request exception occured (internal error) [RETRY={_retries}/3].", ex); _logger.LogError($"Request URL: [{Method.ToString()}] {RequestUrl}"); Close(false); return; } string message = ParseResponse(ex.Response); bool isRateLimit = (int)httpResponse.StatusCode == 429; if (isRateLimit) { _logger.LogInfo($"Discord ratelimit reached. (Ratelimit info: remaining: {bucket.Remaining}, limit: {bucket.Limit}, reset: {bucket.Reset}, time now: {Helpers.Time.TimeSinceEpoch()}"); } else { DiscordApiError apiError = Response.ParseData <DiscordApiError>(); if (!string.IsNullOrEmpty(apiError.Code)) { _logger.LogWarning($"Discord has returned error Code - {apiError.Code}: {apiError.Message} - {req.RequestUri} (code {httpResponse.StatusCode})"); } else { _logger.LogWarning($"An error occured whilst submitting a request to {req.RequestUri} (code {httpResponse.StatusCode}): {message}"); } } Close(!isRateLimit); } } catch (Exception ex) { _logger.LogException("Request callback raised an exception", ex); Close(); } }