/// <inheritdoc />
 public Task <IHypermediaContainer> Process(
     IResponse response,
     IHydraClient hydraClient,
     CancellationToken cancellationToken)
 {
     return(Process(response, hydraClient, null, cancellationToken));
 }
 /// <inheritdoc />
 public Task <IHypermediaContainer> Process(
     IResponse response,
     IHydraClient hydraClient,
     IHypermediaProcessingOptions options = null)
 {
     return(Process(response, hydraClient, options, CancellationToken.None));
 }
 private static Task <IResource> PointingResourceInitializer(
     ITypedEntity resource,
     IHydraClient client,
     ProcessingState processingState,
     CancellationToken cancellationToken)
 {
     resource.Unwrap().SetProperty(ResourceExtensions.BaseUrlPropertyInfo, (Uri)processingState.BaseUrl);
     return(ResourceInitializer(resource.ActLike <IHydraResource>(), client, processingState, cancellationToken));
 }
 private static Task <IResource> ClientInitializer(
     ITypedEntity resource,
     IHydraClient client,
     ProcessingState processingState,
     CancellationToken cancellationToken)
 {
     resource.Unwrap().SetProperty(ClientPropertyInfo, client);
     return(ResourceInitializer(
                resource.Is(hydra.ApiDocumentation) ? (ITypedEntity)resource.ActLike <IApiDocumentation>() : resource.ActLike <ICollection>(),
                client,
                processingState,
                cancellationToken));
 }
        /// <inheritdoc />
        public async Task <IHypermediaContainer> Process(
            IResponse response,
            IHydraClient hydraClient,
            IHypermediaProcessingOptions options,
            CancellationToken cancellationToken)
        {
            if (response == null)
            {
                throw new ArgumentNullException(nameof(response));
            }

            if (hydraClient == null)
            {
                throw new ArgumentNullException(nameof(hydraClient));
            }

            var       responseIri = options?.OriginalUrl ?? response.Url;
            var       context     = EntityContextFactory.Value.Create();
            IResource resource;
            var       hypermedia = new List <IResource>();

            using (var processingState = new ProcessingState(
                       context,
                       _ontologyProvider,
                       responseIri,
                       options?.LinksPolicy ?? LinksPolicy.Strict,
                       response.Headers[HydraClient.ContentType].FirstOrDefault()))
            {
                var rdfReader = await CreateRdfReader(response, cancellationToken);

                var serializableSource = (ISerializableEntitySource)context.EntitySource;
                using (processingState.StartGatheringStatementsFor(serializableSource, _ => _.Object != null && !IsStandaloneControl(_.Predicate)))
                    using (var reader = new StreamReader(await response.GetBody(cancellationToken)))
                    {
                        await serializableSource.Read(reader, rdfReader, response.Url, cancellationToken);
                    }

                resource = await ProcessResources(processingState, hydraClient, hypermedia, cancellationToken);
            }

            return(new HypermediaContainer(response, resource, hypermedia));
        }
        private async Task <IResource> ProcessResources(
            ProcessingState processingState,
            IHydraClient hydraClient,
            List <IResource> hypermedia,
            CancellationToken cancellationToken)
        {
            var resource = await processingState.Context.Load <IResource>(processingState.BaseUrl, cancellationToken);

            foreach (var entity in processingState.Context.AsQueryable <ITypedEntity>())
            {
                Func <ITypedEntity, IHydraClient, ProcessingState, CancellationToken, Task <IResource> > initializer = Initializers[Untyped];
                foreach (var type in entity.Type.Where(item => item.ToString().StartsWith(hydra.Namespace)))
                {
                    if (!Initializers.TryGetValue(type, out initializer))
                    {
                        initializer = Initializers[Untyped];
                        continue;
                    }

                    break;
                }

                var currentResource = await initializer(entity, hydraClient, processingState, cancellationToken);

                if (currentResource != null)
                {
                    if (processingState.AllHypermedia.Contains(currentResource.Iri))
                    {
                        currentResource = EnsureTypeCastedFor(currentResource);
                        hypermedia.Add(currentResource);
                    }

                    if (currentResource.Iri == resource.Iri)
                    {
                        resource = currentResource;
                    }
                }
            }

            return(resource);
        }
Example #7
0
        private static async Task <IResource> ResourceInitializer(
            ITypedEntity resource,
            IHydraClient client,
            ProcessingState processingState,
            CancellationToken cancellationToken)
        {
            IHydraResource hydraResource = resource as IHydraResource
                                           ?? (resource.Is(hydra.Resource) ? resource.ActLike <IHydraResource>() : null);

            bool hasView = false;

            foreach (var statement in processingState.StatementsOf(resource.Iri))
            {
                hydraResource = await GatherLinks(resource, statement, processingState, hydraResource, cancellationToken);

                if (statement.Predicate == hydra.view)
                {
                    hasView = true;
                }
            }

            if (hydraResource != null && processingState.NumberOfStatementsOf(resource.Iri) > 0)
            {
                GatherOperationTargets(hydraResource, processingState);
                if (hasView)
                {
                    hydraResource = hydraResource.ActLike <IResourceView>();
                }

                var addToHypermedia = !processingState.ForbiddenHypermeda.Contains(hydraResource.Iri) &&
                                      (!hydraResource.Iri.IsBlank || IsHydraDependent(hydraResource));
                if (addToHypermedia)
                {
                    processingState.AllHypermedia.Add(hydraResource.Iri);
                }
            }

            return(hydraResource);
        }
Example #8
0
 public async Task Setup()
 {
     Client = ConfigureClient(HydraClientFactory.Configure().WithDefaults()).AndCreate();
     ScenarioSetup();
     await TheTest();
 }
Example #9
0
 internal PartialCollectionIterator(ICollection collection, IHydraClient hydraClient)
 {
     _hydraClient          = hydraClient;
     Update(_collectionIri = collection.Iri, collection.View);
 }