Example #1
0
        /// <summary>
        /// Reads the JSON representation of the object.
        /// </summary>
        /// <param name="reader">The <see cref="Newtonsoft.Json.JsonReader"/> to read from.</param>
        /// <param name="objectType">The <see cref="System.Type"/> of the object.</param>
        /// <param name="existingValue">The existing value of object being read.</param>
        /// <param name="serializer">The calling serializer.</param>
        /// <returns>The object value.</returns>
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            switch (reader.TokenType)
            {
            case JsonToken.String:
                return(DateTimeOffset.ParseExact(reader.Value as string, "ddd MMM dd HH:mm:ss K yyyy",
                                                 DateTimeFormatInfo.InvariantInfo,
                                                 DateTimeStyles.AllowWhiteSpaces));

            case JsonToken.Date:
                if (reader.Value is DateTimeOffset)
                {
                    return((DateTimeOffset)reader.Value);
                }
                else
                {
                    return(new DateTimeOffset(((DateTime)reader.Value).ToUniversalTime(), TimeSpan.Zero));
                }

            case JsonToken.Integer:
                return(InternalUtils.GetUnixTime((long)reader.Value));

            case JsonToken.Null:
                return(DateTimeOffset.Now);
            }

            throw new InvalidOperationException("This object is not a DateTimeOffset");
        }
Example #2
0
        internal static RateLimit ReadRateLimit(HttpWebResponse response)
        {
            var limit     = response.Headers[XRateLimitLimit];
            var remaining = response.Headers[XRateLimitRemaining];
            var reset     = response.Headers[XRateLimitReset];

            return(limit != null && remaining != null && reset != null
                ? new RateLimit()
            {
                Limit = int.Parse(limit),
                Remaining = int.Parse(remaining),
                Reset = InternalUtils.GetUnixTime(long.Parse(reset))
            }
                : null);
        }
Example #3
0
        internal static RateLimit ReadRateLimit(AsyncResponse response)
        {
            if (!new[] { XRateLimitLimit, XRateLimitRemaining, XRateLimitReset }
                .All(x => response.Headers.ContainsKey(x)))
            {
                return(null);
            }

            var limit     = response.Headers[XRateLimitLimit];
            var remaining = response.Headers[XRateLimitRemaining];
            var reset     = response.Headers[XRateLimitReset];

            return(new RateLimit()
            {
                Limit = int.Parse(limit),
                Remaining = int.Parse(remaining),
                Reset = InternalUtils.GetUnixTime(long.Parse(reset))
            });
        }