示例#1
0
 public InputModule(IResourceApplicationService resourceApplicationService, IRequestProvider requestProvider) : base(string.Empty)
 {
     AsyncContext.Run(() => resourceApplicationService.GetResources())
     .ToList().ForEach(r =>
                       r.Methods.Split(',').ToList().ForEach(m =>
                                                             this.AddRoute(
                                                                 m,
                                                                 r.PathPattern,
                                                                 async(parameters, token) =>
                                                                 (
                                                                     m == "GET" ?
                                                                     await InputModule.ProcessReadMethod(this, r.OutUri, requestProvider) :
                                                                     await InputModule.ProcessWriteMethod(this, new HttpMethod(m), r.InUri, requestProvider)
                                                                 ),
                                                                 (nc) => true,
                                                                 r.PathPattern
                                                                 )
                                                             )
                       );
 }
示例#2
0
        public InputModule(IAuthorApplicationService authorApplicationService, IResourceApplicationService resourceApplicationService) : base(string.Empty)
        {
            if (bool.TryParse(Environment.GetEnvironmentVariable(EnvironmentVariableKeys.RequireAuthentication), out bool value) && value)
            {
                this.RequiresAuthentication();
            }

            // TODO: handle single path, 3 paths etc.
            this.Post("/{path1}/{path2}/{any*}", (Func <dynamic, Task <Response> >)(async(parameters) =>
            {
                return(await InputModule.ProcessWriteMethod(
                           this,
                           HttpMethod.Post,
                           authorApplicationService,
                           resourceApplicationService,
                           parameters
                           ));
            })
                      );

            this.Patch("/{path1}/{path2}/{any*}", async(parameters) =>
            {
                return(await InputModule.ProcessWriteMethod(
                           this,
                           new HttpMethod("PATCH"),
                           authorApplicationService,
                           resourceApplicationService,
                           parameters
                           ));
            }
                       );

            this.Get("/{path1}/{path2}/{any*}", async(parameters) =>
            {
                var result = new Response();
                HttpResponseMessage response = null;
                var responseContent          = string.Empty;
                try
                {
                    var resource = await resourceApplicationService.GetByPath(
                        InputModule.GetCombinedPath(parameters)
                        );
                    var hc = new HttpClient()
                    {
                        BaseAddress = new Uri(resource.OutUri)
                    };

                    hc.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    response        = await hc.GetAsync(InputModule.GetPath(this.Context.Request));
                    responseContent = await response.Content.ReadAsStringAsync();
                    response.EnsureSuccessStatusCode();

                    return(new TextResponse(HttpStatusCode.OK, responseContent));
                }
                catch (Exception ex)
                {
                    result = new TextResponse(HttpStatusCode.BadRequest, (response != null) ? responseContent : ex.ToString());
                }
                return(result);
            }
                     );

            // TODO: DELETE
        }