public IRestEndpoint AggregateNext(string fullResourcePath) { if (string.IsNullOrEmpty(fullResourcePath)) { return(_startEndpoint); } IRestEndpoint endpoint = _startEndpoint; string[] dirs = fullResourcePath.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries); foreach (string dir in dirs) { var path = new AggregatorNextPath(dir, _namingConventionSwitcher); try { endpoint = endpoint.Next(path); } catch (Exception ex) { throw new NextEndpointErrorException(path, ex); } if (endpoint == null) { throw new NextEndpointNotFoundException(path); } } return(endpoint); }
/// <summary> /// Finds the endpoint and executes the request onto it. /// Handles errors and disposes of the endpoint when completed. /// </summary> public async Task InvokeAsync(IHttpRequestReader requestReader, IHttpRequestResponder responder, IRequestContext context) { var response = new Response(requestReader.ResourcePath); var builder = new ResponseBuilder(response, _services.Modifiers); var reader = new RequestReader(requestReader, _services.NameSwitcher, _services.QueryCreator); var writer = new ResponseWriter(responder, response, _services.NameSwitcher); try { var navigator = new EndpointNavigator(context, _startResourceFactory, _services); IRestEndpoint endpoint = navigator.GetEndpointFromPath(requestReader.ResourcePath); IExecutor executor = _services.ExecutorFactory.GetExecutor(endpoint); await executor.ExecuteAsync(reader, builder); await writer.WriteAsync(); } catch (Exception ex) { var errorInfo = new ExceptionErrorInfo(ex); builder.AddError(errorInfo); await writer.WriteAsync(); } finally { context.Dispose(); } }
public async Task FieldEndpoint() { IRestEndpoint endpoint = _navigator.GetEndpointFromPath("artists/123/Name"); ResourceBody response = await endpoint.GetAsync(null); Assert.Equal(response.GetObject(), TestRepositories.ArtistName); }
public async Task MemoryCollection() { var endpointContext = new TestEndpointContext(); IRestEndpoint endpoint = endpointContext.Services.EndpointResolver.GetFromResource(endpointContext, new ArtistMemoryCollection()); var response = (CollectionBody)(await endpoint.GetAsync(null)); var firstObj = response.Items.First(); Assert.Equal(firstObj["Name"], TestRepositories.ArtistName); }
public async Task ListArtists() { IRestEndpoint endpoint = _navigator.GetEndpointFromPath("artists"); var response = (CollectionBody)(await endpoint.GetAsync(null)); RestItemData[] arr = response.Items.ToArray(); int firstId = (int)arr[0]["ID"]; Assert.True(firstId > 0); }
public EndpointProxy(IRestEndpoint endpoint, EndpointMetadata metadata, IRestContainer components) { endpoint.AssertNotNull(nameof(endpoint)); metadata.AssertNotNull(nameof(metadata)); components.AssertNotNull(nameof(components)); _endpoint = endpoint; _metadata = metadata; _modelMapper = components.Required <RestModelMapper>(); _resultValidators = components.Resolve <IInvocatioтFilter>().ToList(); }
private async Task <IHttpActionResult> GetResultFromMethodFeedbackAsync(UnsafeMethod method, ResourceBody body) { IRestEndpoint endpoint = GetEndpoint(); if (!endpoint.EvaluatePreconditions(GetPreconditions())) { return(StatusCode(HttpStatusCode.PreconditionFailed)); } Feedback feedback = await endpoint.CommandAsync(method, body); return(GetResultFromFeedback(feedback)); }
public IExecutor GetExecutor(IRestEndpoint endpoint) { IExecutor executor = new EndpointExecutor(endpoint); executor = new PreconditionsExecutor(executor); if (_configuration.Response.ResourceOnSuccessfulCommand) { executor = new RequeryExecutor(executor); } return(executor); }
public IRestEndpoint GetEndpointFromPath(string resourcePath) { IRestResource startResource = _startResourceFactory.GetStartResource(_requestContext); var context = new EndpointContext(_requestContext, _services); IRestEndpoint startEndpoint = _services.EndpointResolver.GetFromResource(context, startResource); var nextAggregator = new NextAggregator(startEndpoint, _services.NameSwitcher ?? new VoidNamingConventionSwitcher()); IRestEndpoint endpoint = nextAggregator.AggregateNext(resourcePath); //var wrappedEndpoint = new NamingSwitcherEndpointWrapper(endpoint, namingConventionSwitcher); return(endpoint); }
public async Task ItemEndpoint() { IRestEndpoint endpoint = _navigator.GetEndpointFromPath("artists/123"); ItemBody itemBody = (ItemBody)(await endpoint.GetAsync(null)); RestItemData itemData = itemBody.Item; Assert.Equal("ID", itemData.Keys.First()); int firstId = (int)itemData["ID"]; Assert.True(firstId > 0); }
public async Task <object> GetAsync() { IRestEndpoint endpoint = GetEndpoint(); if (!endpoint.EvaluatePreconditions(GetPreconditions())) { return(StatusCode(HttpStatusCode.NotModified)); } ResourceBody resourceBody = await endpoint.GetAsync(GetQuery()); ResponseBuilder.AddResource(resourceBody); return(Response.ResourceBody); // TODO headers? }
public async Task DictionaryEndpoint() { IRestEndpoint endpoint = _navigator.GetEndpointFromPath("artists/by_ID"); DictionaryBody dictionaryBody = (DictionaryBody)(await endpoint.GetAsync(null)); foreach (var pair in dictionaryBody.Items) { var itemData = pair.Value as RestItemData; int id = (int)itemData["ID"]; Assert.True(id > 0); Assert.Equal(pair.Key, id.ToString()); } }
public async Task FieldSelector_ManualNext_CorrectName() { var endpointContext = new TestEndpointContext(); var testQuery = new TestCollectionQuery { SelectFields = new[] { "Name" } }; EngineRestCollection <Artist> artistsCollection = IntegratedRestDirectory.GetArtistCollection(endpointContext.Request); IRestEndpoint endpoint = endpointContext.Services.EndpointResolver.GetFromResource(endpointContext, artistsCollection); endpoint = endpoint.Next(new AggregatorNextPath("123", endpointContext.Services.NameSwitcher)); var response = (ItemBody)(await endpoint.GetAsync(testQuery)); Assert.Equal(response.Item["Name"], TestRepositories.ArtistName); }
private async Task <object> ExecuteWithRequeryAsync(IRequestReader reader, ResponseBuilder response) { object returnValue = await _executor.ExecuteAsync(reader, response); IRestEndpoint endpoint = _executor.Endpoint; if (returnValue is AcknowledgmentFeedback ackFeedback && ackFeedback.Acknowledgment is CreatedItemAcknowledgment created) { endpoint = endpoint.Next(new RawNextPath(created.NewIdentifier.ToString())); Debug.Assert(endpoint != null); } ResourceBody resourceBody = await endpoint.GetAsync(reader.GetQuery()); response.AddResource(resourceBody); return(returnValue); }
public async Task FieldSelector_Collection_DoesntThrow() { var endpointContext = new TestEndpointContext(); var testQuery = new TestCollectionQuery { SelectFields = new[] { "Id", "Name" } }; EngineRestCollection <Artist> artistsCollection = IntegratedRestDirectory.GetArtistCollection(endpointContext.Request); IRestEndpoint endpoint = endpointContext.Services.EndpointResolver.GetFromResource(endpointContext, artistsCollection); var resource = (CollectionBody)(await endpoint.GetAsync(testQuery)); Assert.Equal(1, resource.Items.Count()); var builder = new ResponseBuilder(new Response("/"), endpointContext.Modifiers); builder.AddResource(resource); }
public EndpointExecutor(IRestEndpoint endpoint) { Endpoint = endpoint; }
public NextAggregator([NotNull] IRestEndpoint startEndpoint, [NotNull] INamingConventionSwitcher namingConventionSwitcher) { _startEndpoint = startEndpoint ?? throw new ArgumentNullException(nameof(startEndpoint)); _namingConventionSwitcher = namingConventionSwitcher ?? throw new ArgumentNullException(nameof(namingConventionSwitcher)); }