示例#1
0
        public override IEnumerable <RestResponse> POST(string url, SerializationContext data, RestSettings restSettings)
        {
            Contract.AssertArgumentNotTrue(string.IsNullOrEmpty(url), nameof(url));

            using HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, url);
            PrepareContent(requestMessage, data);
            return(Raw(_httpClient.SendAsync(requestMessage), restSettings));
        }
示例#2
0
        private async Task <RestResponse[]> Raw(string method, string url, IReadOnlyCollection <HttpStatusCode> allowedStatuses, RestSettings restSettings, SerializationContext?data = null)
        {
            Contract.EnsureWebhookIsNotBroken(_webhook.Status);
            Contract.AssertArgumentNotTrue(string.IsNullOrEmpty(method), nameof(method));
            Contract.AssertArgumentNotTrue(string.IsNullOrEmpty(url), nameof(url));
            Contract.AssertNotNull(allowedStatuses, nameof(allowedStatuses));
            Contract.CheckForNull(restSettings, nameof(restSettings));

            List <RestResponse> responses = new List <RestResponse>();

            uint currentAttimpts = 0;
            // Used to prevent calls if something went wrong
            bool forceStop = false;

            do
            {
                if (responses.Count != 0)
                {
                    await _webhook.ActionManager.FollowRateLimit(responses.Last().RateLimit).ConfigureAwait(false);
                }

                HttpWebRequest request = WebRequest.CreateHttp(url);
                request.CachePolicy = _cachePolicy;
                request.Method      = method;
                // Calling 'GetRequestStream()' after setting the request type
                using var requestStream = request.GetRequestStream();
                PrepareRequest(request, requestStream, data);
                // Identify themselves
                request.UserAgent = $"DSharp4Webhook ({WebhookProvider.LibraryUrl}, {WebhookProvider.LibraryVersion})";
                // The content type is assigned in 'PrepareRequest'
                // Uses it for accurate measurement RateLimit
                request.Headers.Set("X-RateLimit-Precision", "millisecond");
                // Disabling keep-alive, this is a one-time connection
                request.KeepAlive = false;
                // I noticed a memory leak on a stress test
                // It wat because System.PinnableBufferCache not cleared
                // If we use 'request.AllowWriteStreamBuffering = false', it just stops working and throwing an WebException

                RestResponse restResponse;
                using (HttpWebResponse response = request.GetResponseNoException())
                {
                    RateLimitInfo rateLimitInfo = new RateLimitInfo(response.Headers.GetAsDictionary());
                    restResponse = new RestResponse(response, rateLimitInfo, currentAttimpts);
                    responses.Add(restResponse);

                    // Processing the necessary status codes
                    ProcessStatusCode(response.StatusCode, ref forceStop, allowedStatuses);
                }
                Log(new LogContext(LogSensitivity.VERBOSE, $"[A {currentAttimpts}] [SC {(int)responses.Last().StatusCode}] [RLR {restResponse.RateLimit.Reset:yyyy-MM-dd HH:mm:ss.fff zzz}] [RLMW {restResponse.RateLimit.MustWait}] Post request completed:{(restResponse.Content?.Length != 0 ? string.Concat(Environment.NewLine, restResponse.Content ?? string.Empty) : " No content")}", _webhook.Id));

                // first of all we check the forceStop so that we don't go any further if
#pragma warning disable IDE0075 // Simplify conditional expression
            } while (!forceStop && (!allowedStatuses.Contains(responses.Last().StatusCode) && (restSettings.Attempts > 0 ? ++currentAttimpts <= restSettings.Attempts : true)));
#pragma warning restore IDE0075 // Simplify conditional expression

            return(responses.ToArray());
        }
示例#3
0
        public Message(string message, IMessageMention messageMention, bool isTTS = false) : this()
        {
            Contract.AssertArgumentNotTrue(string.IsNullOrEmpty(message), nameof(message));
            Contract.AssertNotNull(messageMention, nameof(messageMention));

            Content = message;
            Mention = messageMention;
            IsTTS   = isTTS;
        }
示例#4
0
        public override IEnumerable <RestResponse> PATCH(string url, SerializationContext data, RestSettings restSettings)
        {
            Contract.AssertArgumentNotTrue(string.IsNullOrEmpty(url), nameof(url));
            Contract.AssertRequiredSerizationType(data, SerializationType.APPLICATION_JSON);

            using HttpRequestMessage requestMessage = new HttpRequestMessage(PATCHMethod, url);
            PrepareContent(requestMessage, data);
            return(Raw(_httpClient.SendAsync(requestMessage), restSettings));
        }
示例#5
0
 public override IEnumerable <RestResponse> DELETE(string url, RestSettings restSettings)
 {
     Contract.AssertArgumentNotTrue(string.IsNullOrEmpty(url), nameof(url));
     return(Raw(_httpClient.DeleteAsync(url), restSettings));
 }