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 ) ) ); }
public ResourceController(IResourceApplicationService app, ILoggerFactory logger, IHostingEnvironment env) : base(app, logger, env, new ErrorMapCustom()) { }
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 }
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); }
public ResourceMoreController(IResourceRepository rep, IResourceApplicationService app, ILoggerFactory logger, EnviromentInfo env, CurrentUser user, ICache cache) : base(rep, app, logger, env, user, cache, new ExportExcel <dynamic>(), new ErrorMapCustom()) { }