예제 #1
0
 public RestRequest(string endpoint, HttpMethodsEnum method = HttpMethodsEnum.GET, string postData = "", string contentType = "application/json")
 {
     EndPoint    = new Uri(endpoint);
     Method      = method;
     PostData    = postData;
     ContentType = contentType;
 }
예제 #2
0
        public static async Task <T> SendRequestAsync <T>(string apiAddress,
                                                          Type modelTypeToPost, object modelToPost,
                                                          HttpMethodsEnum httpMethodsEnum)
        {
            Task <T> resultTask = null;

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(BaseAddress);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                HttpStatusCode httpStatusCode;
                switch (httpMethodsEnum)
                {
                case HttpMethodsEnum.Get:
                {
                    HttpResponseMessage response = await client.GetAsync(apiAddress);

                    httpStatusCode = response.StatusCode;
                    if (response.IsSuccessStatusCode)
                    {
                        resultTask = response.Content.ReadAsAsync <T>();
                    }
                }
                break;

                case HttpMethodsEnum.Post:
                {
                    // HTTP POST
                    var httpContent = new ObjectContent(modelTypeToPost, modelToPost, new JsonMediaTypeFormatter(), "application/json");
                    HttpResponseMessage response = await client.PostAsync(apiAddress, httpContent);

                    httpStatusCode = response.StatusCode;
                    if (response.IsSuccessStatusCode)
                    {
                        resultTask = response.Content.ReadAsAsync <T>();
                    }
                }
                break;

                default:
                    throw new ArgumentOutOfRangeException("httpMethodsEnum", httpMethodsEnum, null);
                }
                if (resultTask == null)
                {
                    throw new Exception(string.Format("Error: Status code {0} {1} ", (int)httpStatusCode, httpStatusCode));
                }
                return(await resultTask);
            }
        }
예제 #3
0
        public static string fromHttpMethodsEnum(HttpMethodsEnum e)
        {
            switch (e)
            {
            case HttpMethodsEnum.Delete: return(Delete);

            case HttpMethodsEnum.Get: return(Get);

            case HttpMethodsEnum.Head: return(Head);

            case HttpMethodsEnum.Options: return(Options);

            case HttpMethodsEnum.Post: return(Post);

            case HttpMethodsEnum.Put: return(Put);

            case HttpMethodsEnum.Trace: return(Trace);

            default: throw new NotSupportedException();
            }
        }
예제 #4
0
 // constructor
 protected IVPNRestRequest(string endpoint, HttpMethodsEnum method = HttpMethodsEnum.GET, string postData = "", string contentType = "application/json") : base(endpoint, method, postData, contentType)
 {
 }
예제 #5
0
 public DirectRouteAction(HttpMethodsEnum method, string urlPath, Func <ViewObject> action)
 {
     Method  = method;
     UrlPath = urlPath;
     Action  = action;
 }
예제 #6
0
 public DirectRouteAction(HttpMethodsEnum method, string urlPath, Func <HttpListenerContext, ViewObject> action)
 {
     Method        = method;
     UrlPath       = urlPath;
     ContextAction = action;
 }