Пример #1
0
        async public Task <TM> Add(TM model)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(ResourceName))
                {
                    throw new ArgumentNullException(nameof(ResourceName));
                }

                if (model == null)
                {
                    throw new ArgumentNullException(nameof(model));
                }

                var request  = new HttpBody <TM>(model);
                var response = await RestService.Post(ResourceName, request);

                return(response?.Content);
            }
            catch (HttpException ex)
            {
                if (ex.StatusCode == HttpStatusCode.NotFound)
                {
                    return(null);
                }

                ex.CheckRule();
                throw;
            }
        }
Пример #2
0
        async Task <HttpBody <T> > GetApi <T>(string resourceOrUrl, object param)
            where T : class
        {
            HttpBody <T> result;

            try
            {
                // Builds the url
                string url = param.ToQueryString(resourceOrUrl);

                // Gets JSON and parse the result
                StringBody response = await HttpService.Get(url);

                T data = JsonConvert.DeserializeObject <T>(response.Content);
                result = new HttpBody <T>(data, response);
            }
            catch (HttpException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new HttpException(ex);
            }

            return(result);
        }
Пример #3
0
        async public Task <ModelType> Update(ModelType model)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(ResourceName))
                {
                    throw new ArgumentNullException("ResourceName");
                }

                if (model == null)
                {
                    throw new ArgumentNullException("model");
                }

                var url      = $"{ResourceName}/{model.Id}";
                var request  = new HttpBody <ModelType>(model);
                var response = await RestService.Put(url, request);

                return(response?.Content);
            }
            catch (HttpException ex)
            {
                if (ex.StatusCode == HttpStatusCode.NotFound)
                {
                    return(null);
                }

                ex.CheckRule();
                throw;
            }
        }
Пример #4
0
        async Task <HttpBody <ResponseType> > PutApi <ResponseType>(string url, object input)
            where ResponseType : class
        {
            var result = new HttpBody <ResponseType>();

            try
            {
                // Posta o JSON no servidor, e realiza parse do retorno
                var json     = JsonConvert.SerializeObject(input);
                var response = await HttpService.Put(url, json);

                var data = JsonConvert.DeserializeObject <ResponseType>(response.Content);
                result = new HttpBody <ResponseType>(data, response);
            }
            catch (HttpException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new HttpException(ex);
            }

            return(result);
        }
Пример #5
0
        async Task <HttpBody <TR> > PostApi <TR, TB>(string url, HttpBody <TB> body)
            where TR : class
            where TB : class
        {
            HttpBody <TR> result;

            try
            {
                HttpBody <string> response;

                if (body.IsForm)
                {
                    // Post FORM and parse the result
                    var dic = body.ToFormDictionaty();
                    response = await HttpService.Post(url, dic);
                }
                else
                {
                    string json = string.Empty;

                    // Post JSON and parse the result
                    if (body.Content != null)
                    {
                        if (body.GetType() == typeof(string))
                        {
                            json = body as string;
                        }
                        else
                        {
                            json = JsonConvert.SerializeObject(body.Content);
                        }
                    }

                    response = await HttpService.Post(url, json);
                }

                var data = JsonConvert.DeserializeObject <TR>(response.Content);
                result = new HttpBody <TR>(data, response);
            }
            catch (HttpException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new HttpException(ex);
            }

            return(result);
        }
Пример #6
0
        async Task <HttpBody <T> > DeleteApi <T>(string url)
            where T : class
        {
            HttpBody <T> result;

            try
            {
                var response = await HttpService.Delete(url);

                var data = JsonConvert.DeserializeObject <T>(response.Content);
                result = new HttpBody <T>(data, response);
            }
            catch (HttpException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new HttpException(ex);
            }

            return(result);
        }
Пример #7
0
        async Task <HttpBody <TR> > PutApi <TR>(string url, object input)
            where TR : class
        {
            HttpBody <TR> result;

            try
            {
                var json     = JsonConvert.SerializeObject(input);
                var response = await HttpService.Put(url, json);

                var data = JsonConvert.DeserializeObject <TR>(response.Content);
                result = new HttpBody <TR>(data, response);
            }
            catch (HttpException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new HttpException(ex);
            }

            return(result);
        }
Пример #8
0
        async Task <HttpBody <ResponseType> > DeleteApi <ResponseType>(string url)
            where ResponseType : class
        {
            var result = new HttpBody <ResponseType>();

            try
            {
                // Recupera o JSON no servidor, e realiza parse do retorno
                var response = await HttpService.Delete(url);

                var data = JsonConvert.DeserializeObject <ResponseType>(response.Content);
                result = new HttpBody <ResponseType>(data, response);
            }
            catch (HttpException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new HttpException(ex);
            }

            return(result);
        }
Пример #9
0
 /// <summary>
 /// Performs POST calls to add a new resource.
 /// </summary>
 /// <typeparam name="TR">The result model type</typeparam>
 /// <typeparam name="TB">The body type</typeparam>
 /// <param name="resource">The relative endpoint Url</param>
 /// <param name="body">The body data</param>
 /// <returns>The requested response</returns>
 async public virtual Task <HttpBody <TR> > Post <TR, TB>(string resource, HttpBody <TB> body)
     where TR : class
     where TB : class
 {
     return(await PostApi <TR, TB>(resource, body));
 }
Пример #10
0
 /// <summary>
 /// Performs PUT calls to add an existing resource.
 /// </summary>
 /// <typeparam name="T">The body type</typeparam>
 /// <param name="resource">The relative endpoint Url</param>
 /// <param name="body">The body data</param>
 /// <returns>The requested response</returns>
 async public virtual Task <HttpBody <T> > Put <T>(string resource, HttpBody <T> body)
     where T : class
 {
     return(await Put <T, T>(resource, body));
 }
Пример #11
0
 /// <summary>
 /// Performs POST calls to add a new resource.
 /// </summary>
 /// <typeparam name="ResponseType">The result model type</typeparam>
 /// <typeparam name="BodyType">The body type</typeparam>
 /// <param name="resource">The relative endpoint Url</param>
 /// <param name="body">The body data</param>
 /// <returns>The requested response</returns>
 async public virtual Task <HttpBody <ResponseType> > Post <ResponseType, BodyType>(string resource, HttpBody <BodyType> body)
     where ResponseType : class
     where BodyType : class
 {
     return(await PostApi <ResponseType, BodyType>(resource, body));
 }
Пример #12
0
 /// <summary>
 /// Performs PUT calls to add an existing resource.
 /// </summary>
 /// <typeparam name="BodyType">The body type</typeparam>
 /// <param name="resource">The relative endpoint Url</param>
 /// <param name="body">The body data</param>
 /// <returns>The requested response</returns>
 async public virtual Task <HttpBody <ResponseType> > Put <ResponseType>(string resource, HttpBody <ResponseType> body)
     where ResponseType : class
 {
     return(await Put <ResponseType, ResponseType>(resource, body));
 }