Пример #1
0
        /// <summary>
        /// Gets the life span for cache items based on the provided instance of <see cref="IApiEndpointBuilder"/>.
        /// </summary>
        /// <param name="builder">The object that builds the API route endpoint.</param>
        /// <returns>The life span duration after which a cache item is no longer considered fresh.</returns>
        public TimeSpan?GetCacheLifeSpan(IApiEndpointBuilder builder)
        {
            if (builder == null)
            {
                throw new ArgumentNullException("builder");
            }
            ApiMethodEnum method = builder.ApiMethod;

            return(method.GetCacheLifeSpan(Client));
        }
Пример #2
0
        public async Task <T> SendRequest <T>(ApiMethodEnum apiMethod, ApiSerializerEnum serializer, string url, Dictionary <string, string> headers = null, object body = null) where T : class
        {
            var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);

            httpWebRequest.ContentType            = "application/json";
            httpWebRequest.Method                 = apiMethod.ToString();
            httpWebRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;


            if (headers != null)
            {
                foreach (var(key, value) in headers)
                {
                    httpWebRequest.Headers.Add(key, value);
                }
            }

            if (body != null)
            {
                await using var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream());
                var json = JsonConvert.SerializeObject(body, Formatting.None);
                streamWriter.Write(json);
                streamWriter.Flush();
                streamWriter.Close();
            }

            using var response = (HttpWebResponse) await httpWebRequest.GetResponseAsync();

            await using var stream = response.GetResponseStream();
            using var reader       = new StreamReader(stream ?? throw new InvalidOperationException());
            var apiResult = await reader.ReadToEndAsync();

            if (string.IsNullOrEmpty(apiResult))
            {
                return(null);
            }

            switch (serializer)
            {
            case ApiSerializerEnum.None:
                return((T)Convert.ChangeType(apiResult, typeof(T)));

            case ApiSerializerEnum.Json:
                return(JsonConvert.DeserializeObject <T>(apiResult));

            case ApiSerializerEnum.XML:
                return(XmlConvert.DeserializeObject <T>(apiResult));

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Пример #3
0
 /// <summary>
 /// Instances the ApiEndpointBuilder. appKey and accessToken aren't actually required parameters.
 /// </summary>
 /// <param name="client">The Stack Exchange client that owns this API endpoint builder.</param>
 /// <param name="method">The API method to target.</param>
 /// <param name="appKey">The application's key. Grants a higher request quota.</param>
 /// <param name="accessToken">The user's access token. Grants authentication and access to methods which require that the application be acting on behalf of a user in order to be invoked.</param>
 public ApiEndpointBuilder(IStackClient client, ApiMethodEnum? method, string appKey = null, string accessToken = null)
     : this()
 {
     if (client == null)
     {
         throw new ArgumentNullException("client");
     }
     if (!method.HasValue)
     {
         throw new ArgumentNullException("method");
     }
     Client = client;
     ApiMethod = method.Value;
     AppKey = appKey;
     AccessToken = accessToken;
     _out = new Lazy<string>(Build);
 }
Пример #4
0
        /// <summary>
        /// Gets the life span for cache items based on the extended <see cref="ApiMethodEnum"/> member.
        /// </summary>
        /// <param name="method">The API method for which to calculate the cache life span.</param>
        /// <param name="client">The governing <see cref="IStackClient"/> instance.</param>
        /// <returns>The life span duration after which a cache item is no longer considered fresh.</returns>
        public static TimeSpan?GetCacheLifeSpan(this ApiMethodEnum method, IStackClient client)
        {
            if (client == null)
            {
                throw new ArgumentNullException("client");
            }
            if (client.Default.CacheLifeSpan.ContainsKey(method))
            {
                return(client.Default.CacheLifeSpan[method]);
            }
            CacheLifeSpanAttribute lifeSpan = method.GetCustomAttribute <CacheLifeSpanAttribute>();

            if (lifeSpan != null)
            {
                return(lifeSpan.Duration);
            }
            return(null);
        }