コード例 #1
0
        public static async Task <T> AzureStorageGetAsync <T>(this Rest restme, string storageRelativePath)
        {
            MustBeStorageMode(restme);
            var container = await restme.GetAzureBlobContainerAsync(storageRelativePath);

            var blobItemPath = restme.IdentifyBlobItemPath(storageRelativePath);

            if (blobItemPath.IsNullOrEmpty())
            {
                throw new PranamWebException("Invalid blob item name.");
            }
            var blockBlob = container.GetBlockBlobReference(blobItemPath);

            using (var stream = new MemoryStream())
            {
                try
                {
                    if (!await blockBlob.ExistsAsync())
                    {
                        return(default(T));
                    }

                    if (typeof(Stream).IsAssignableFrom(typeof(T)))
                    {
                        await blockBlob.DownloadToStreamAsync(stream);

                        var bytes = FileUtils.ReadStreamToEnd(stream);
                        T   result;
                        if (typeof(T).GetTypeInfo().IsAbstract)
                        {
                            result = (T)Activator.CreateInstance(typeof(MemoryStream), bytes);
                        }
                        else
                        {
                            result = (T)Activator.CreateInstance(typeof(T), bytes);
                        }

                        return(result);
                    }


                    var jsonStringValue = await blockBlob.DownloadTextAsync();

                    if (!jsonStringValue.IsNotNullOrEmpty())
                    {
                        return(default(T));
                    }

                    if (typeof(T) == typeof(string))
                    {
                        return((T)Convert.ChangeType(jsonStringValue, typeof(T)));
                    }

                    return(jsonStringValue.JsonDeserialize <T>());
                }
                catch (Exception ex)
                {
                    restme.LogDebug(
                        $"Unable to fetch requested blob: {storageRelativePath}\n {ex.Message} \n {ex.StackTrace}", ex);
                    return(default(T));
                }
            }
        }
コード例 #2
0
 public static HttpResponseMessage <T> HttpRequestFull <T>(this Rest restme, HttpMethod method,
                                                           string relativePath = null)
 {
     return(HttpRequestFullAsync <T>(restme, method, relativePath)
            .WaitAndGetResult(restme.Configuration.DefaultTimeout));
 }
コード例 #3
0
        public static async Task <HttpResponseMessage <T> > HttpRequestFullAsync <T>(this Rest restme, HttpMethod method,
                                                                                     string relativePath = null)
        {
            using (var httpClient = new HttpClient {
                BaseAddress = restme.BaseUri
            })
            {
                restme.PrepareHeaders(httpClient.DefaultRequestHeaders);
                HttpResponseMessage response      = null;
                ByteArrayContent    submitContent = null;
                if (restme.CurrentMode == RestMode.HTTPClient)
                {
                    if (restme._params?.Count > 0)
                    {
                        submitContent = new PranamFormUrlEncodedContent(restme._params);
                    }
                    else if (restme?._objAsParam != null)
                    {
                        submitContent = new StringContent(restme._objAsParam.JsonSerialize());
                        submitContent.Headers.ContentType =
                            new System.Net.Http.Headers.MediaTypeHeaderValue("application/x-www-form-urlencoded");
                    }
                }
                else
                {
                    if (restme._params?.Count > 0)
                    {
                        submitContent = new PranamRestfulHttpContent(restme._params);
                    }
                    else if (restme._objAsParam != null)
                    {
                        submitContent = new StringContent(restme._objAsParam.JsonSerialize());
                    }
                    else
                    {
                        submitContent = new StringContent(string.Empty);
                    }

                    submitContent.Headers.ContentType =
                        new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                }

                if (method == HttpMethod.Post)
                {
                    response =
                        await
                        httpClient.PostAsync(new Uri(restme.BaseUri, relativePath),
                                             submitContent);
                }
                else if (method == HttpMethod.Put)
                {
                    response =
                        await
                        httpClient.PutAsync(new Uri(restme.BaseUri, relativePath),
                                            submitContent);
                }
                else if (method == HttpMethod.Get)
                {
                    response =
                        await
                        httpClient.GetAsync(new Uri(restme.BaseUri,
                                                    restme.PrepareInjectParamsIntoQuery(relativePath)));
                }
                else if (method == HttpMethod.Delete)
                {
                    response =
                        await
                        httpClient.DeleteAsync(new Uri(restme.BaseUri,
                                                       restme.PrepareInjectParamsIntoQuery(relativePath)));
                }

                if (response == null)
                {
                    return(default);
コード例 #4
0
 public static T HttpRequest <T>(this Rest restme, HttpMethod method, string relativeUrlPath = null)
     where T : class
 {
     return(HttpRequestAsync <T>(restme, method, relativeUrlPath)
            .WaitAndGetResult(restme.Configuration.DefaultTimeout));
 }