示例#1
0
        private static async Task <Response> ProcessWriteMethod(NancyModule module, HttpMethod method, IAuthorApplicationService authorApplicationService, IResourceApplicationService resourceApplicationService, dynamic 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.InUri)
                };

                string jsonString = RequestStream.FromStream(module.Request.Body).AsString();
                var    subjectId  = GetUserSubjectId(module.Context);
                var    author     = await authorApplicationService.GetAuthorBySubjectId(subjectId);

                dynamic jsonObj = JsonConvert.DeserializeObject <ExpandoObject>(jsonString);

                jsonObj.AuthorId = author.User.NeuronId.ToString();
                var content = new StringContent(JsonConvert.SerializeObject(jsonObj));
                content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                var message = new HttpRequestMessage(
                    method,
                    InputModule.GetPath(module.Context.Request)
                    )
                {
                    Content = content
                };
                foreach (var kvp in module.Context.Request.Headers.ToList())
                {
                    if (Array.IndexOf(new string[] { "Content-Length", "Content-Type" }, kvp.Key) < 0)
                    {
                        message.Headers.Add(kvp.Key, string.Join(',', kvp.Value));
                    }
                }
                response = await hc.SendAsync(message);

                responseContent = await response.Content.ReadAsStringAsync();

                response.EnsureSuccessStatusCode();

                result = new TextResponse(HttpStatusCode.OK, responseContent);
            }
            catch (Exception ex)
            {
                result = new TextResponse(HttpStatusCode.BadRequest, (response != null) ? responseContent : ex.ToString());
            }
            return(result);
        }
示例#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
        }