예제 #1
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(applicationTag);

            try
            {
                var httpClient = new HttpClient();
                httpClient.DefaultRequestHeaders.Accept.Clear();
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(APIConstant.ContentType));

                StringContent content = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, APIConstant.SerializeContentType);
                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 ex;
            }
        }
예제 #2
0
        public static async Task <T> Get <T>(string urlApi, string action, object dynamicPara, string applicationTag = "cms")
        {
            urlApi += "/" + action + (dynamicPara == null? "":"/?" + URLBuilder.GetQueryString(dynamicPara));
            Uri _baseAddress = URLBuilder.APIBaseAddress("webapi:" + applicationTag);

            using (var httpClient = new HttpClient())
            {
                try
                {
                    httpClient.DefaultRequestHeaders.Accept.Clear();
                    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(APIConstant.ContentType));
                    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;
                }
            }
        }
예제 #3
0
        public static async Task <Q> Authorize_DynamicTransaction <T, Q>(T data, string token, string urlApi, string applicationTag = "cms", string ars = "")
        {
            if (token == null)
            {
                throw new ArgumentNullException(nameof(token));
            }

            Uri _baseAddress = URLBuilder.APIBaseAddress("webapi:" + applicationTag);

            using (var httpClient = HttpClientHelper.GetHttpClient(token))
            {
                try
                {
                    httpClient.DefaultRequestHeaders.Accept.Clear();
                    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(APIConstant.ContentType));

                    StringContent content = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, APIConstant.SerializeContentType);
                    httpClient.BaseAddress = _baseAddress;

                    if (ars.Length < 1 && ars != null)
                    {
                        ars = ARS.IgnoredARS;
                    }
                    if (ars != null && ars.Length > 0)
                    {
                        httpClient.DefaultRequestHeaders.TryAddWithoutValidation(APIConstant.XAuthorizeHeaderARS, ars);
                    }

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

                    if (!response.IsSuccessStatusCode)
                    {
                        var errorMess = response.Content.ReadAsStringAsync().Result;
                        if (errorMess.Length > 0)
                        {
                            Exception ex = new Exception(errorMess);
                            Logger.LogError(ex);
                        }
                        throw new HttpException((int)response.StatusCode, errorMess);
                    }

                    return(response.Content.ReadAsAsync <Q>(new[] { new ProtoBufFormatter() }).Result);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
예제 #4
0
        public static async Task <T> Authorize_Get <T>(string token, string urlApi, string applicationTag = "cms", string ars = null)
        {
            Uri _baseAddress = URLBuilder.APIBaseAddress("webapi:" + applicationTag);

            using (var httpClient = HttpClientHelper.GetHttpClient(token))
            {
                try
                {
                    httpClient.DefaultRequestHeaders.Accept.Clear();
                    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(APIConstant.ContentType));
                    httpClient.BaseAddress = _baseAddress;
                    if (ars != null && ars.Length > 0)
                    {
                        httpClient.DefaultRequestHeaders.TryAddWithoutValidation(APIConstant.XAuthorizeHeaderARS, ars);
                    }

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

                    if (!response.IsSuccessStatusCode)
                    {
                        var errorMess = response.Content.ReadAsStringAsync().Result;
                        if (errorMess.Length > 0)
                        {
                            Exception ex = new Exception(errorMess);
                            Logger.LogError(ex);
                        }
                        throw new HttpException((int)response.StatusCode, errorMess);
                    }

                    var data = response.Content.ReadAsAsync <T>(new[] { new ProtoBufFormatter() }).Result;

                    return(data == null ? default(T) : data);
                }
                catch (HttpException ex)
                {
                    Logger.LogError(ex);
                    throw ex;
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex);
                    throw ex;
                }
            }
        }
예제 #5
0
        public static T Authorize_GetNonAsync <T>(string token, string urlApi, string action, object dynamicPara, string applicationTag = "cms", string ars = null)
        {
            if (token == null)
            {
                throw new ArgumentNullException(nameof(token));
            }

            using (var httpClient = HttpClientHelper.GetHttpClient(token))
            {
                try
                {
                    urlApi += "/" + action + (dynamicPara == null ? "" : "/?" + URLBuilder.GetQueryString(dynamicPara));
                    Uri _baseAddress = URLBuilder.APIBaseAddress("webapi:" + applicationTag);

                    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(APIConstant.ContentType));
                    httpClient.BaseAddress = _baseAddress;
                    if (ars != null && ars.Length > 0)
                    {
                        httpClient.DefaultRequestHeaders.TryAddWithoutValidation(APIConstant.XAuthorizeHeaderARS, ars);
                    }

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

                    if (!response.IsSuccessStatusCode)
                    {
                        var errorMess = response.Content.ReadAsStringAsync().Result;
                        if (errorMess.Length > 0)
                        {
                            Exception ex = new Exception(errorMess);
                            Logger.LogError(ex);
                        }
                        throw new HttpException((int)response.StatusCode, errorMess);
                    }

                    var data = response.Content.ReadAsAsync <T>(new[] { new ProtoBufFormatter() }).Result;

                    return(data == null ? default(T) : data);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
예제 #6
0
        public static async Task <bool> Authorization(string token, string ars = null)
        {
            Uri _baseAddress = URLBuilder.APIBaseAddress("webapi:" + APIConstant.API_Resource_CORE);

            using (var httpClient = HttpClientHelper.GetHttpClient(token))
            {
                try
                {
                    httpClient.DefaultRequestHeaders.Accept.Clear();
                    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(APIConstant.ContentType));
                    httpClient.BaseAddress = _baseAddress;
                    if (ars != null && ars.Length > 0)
                    {
                        httpClient.DefaultRequestHeaders.TryAddWithoutValidation(APIConstant.XAuthorizeHeaderARS, ars);
                    }

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

                    if (!response.IsSuccessStatusCode)
                    {
                        var errorMess = response.Content.ReadAsStringAsync().Result;
                        if (errorMess.Length > 0)
                        {
                            Exception ex = new Exception(errorMess);
                            Logger.LogError(ex);
                        }
                        throw new HttpException((int)response.StatusCode, errorMess);
                    }

                    return(true);
                }
                catch (HttpException ex)
                {
                    // Logger.LogError(ex);
                    throw ex;
                }
            }
        }