Пример #1
0
        /// <summary>
        /// Get data from API
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="urlApi"></param>
        /// <returns></returns>
        public static async Task <T> Get <T>(string urlApi, string applicationTag = "cms")
        {
            Uri _baseAddress = URLBuilder.APIBaseAddress("webapi:" + applicationTag);

            using (var httpClient = new HttpClient())
            {
                try
                {
                    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-protobuf"));
                    httpClient.BaseAddress = _baseAddress;

                    var response = await httpClient.GetAsync(apiPrefix + urlApi);

                    if (!response.IsSuccessStatusCode)
                    {
                        return(default(T));                               //Check status code
                    }
                    var data = response.Content.ReadAsAsync <T>(new[] { new ProtoBufFormatter() }).Result;

                    return(data == null? default(T):data);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Dynamic transaction
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="Q"></typeparam>
        /// <param name="data"></param>
        /// <param name="urlApi"></param>
        /// <returns></returns>
        public static async Task <Q> DynamicTransaction <T, Q>(T data, string urlApi, string applicationTag = "cms")
        {
            Uri _baseAddress = URLBuilder.APIBaseAddress("webapi:" + applicationTag);

            try
            {
                var httpClient = new HttpClient();
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-protobuf"));

                StringContent content = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json");
                httpClient.BaseAddress = _baseAddress;

                HttpResponseMessage response = await httpClient.PostAsync(apiPrefix + urlApi, content);

                if (!response.IsSuccessStatusCode)
                {
                    return(default(Q));                               //Check status code
                }
                return(response.Content.ReadAsAsync <Q>(new[] { new ProtoBufFormatter() }).Result);
            }
            catch (Exception ex)
            {
                throw;
            }
        }