private static async Task <Nancy.Response> ProcessReadMethod(NancyModule module, string outUri, IRequestProvider requestProvider) { var result = new Nancy.Response(); HttpResponseMessage response = null; var responseContent = string.Empty; try { var hc = new HttpClient() { BaseAddress = new Uri(outUri) }; hc.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var initialPath = InputModule.GetPath(module.Context.Request); response = await hc.GetAsync( initialPath + (initialPath.Contains('?') && initialPath.Contains('=') ? "&" : "?") + "userid=" + await InputModule.GetUserId(module, requestProvider) ); 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); }
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); }
private static async Task <Nancy.Response> ProcessWriteMethod(NancyModule module, HttpMethod method, string inUri, IRequestProvider requestProvider) { var result = new Nancy.Response(); HttpResponseMessage response = null; var responseContent = string.Empty; try { var hc = new HttpClient() { BaseAddress = new Uri(inUri) }; string jsonString = RequestStream.FromStream(module.Request.Body).AsString(); dynamic jsonObj = string.IsNullOrEmpty(jsonString) ? new ExpandoObject() : JsonConvert.DeserializeObject <ExpandoObject>(jsonString); jsonObj.UserId = await InputModule.GetUserId(module, requestProvider); var content = new StringContent(JsonConvert.SerializeObject(jsonObj)); content.Headers.ContentType = new System.Net.Http.Headers.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); }
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 }