/// <inheritdoc />
 public Task <IHypermediaContainer> Process(
     IResponse response,
     IHydraClient hydraClient,
     IHypermediaProcessingOptions options = null)
 {
     return(Process(response, hydraClient, options, CancellationToken.None));
 }
 /// <summary>Merges options.</summary>
 /// <param name="options">Options to merge with.</param>
 /// <returns>Merged options where values are taken first from this instance.</returns>
 public HypermediaProcessingOptions MergeWith(IHypermediaProcessingOptions options)
 {
     return(new HypermediaProcessingOptions(
                AuxiliaryResponse ?? options?.AuxiliaryResponse,
                AuxiliaryOriginalUrl ?? options?.AuxiliaryOriginalUrl,
                OriginalUrl ?? options?.OriginalUrl,
                _linksPolicy ?? options?.LinksPolicy));
 }
        /// <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));
        }
Exemplo n.º 4
0
        private async Task <IHypermediaContainer> GetResourceFrom(Uri url, IHypermediaProcessingOptions options, CancellationToken cancellationToken)
        {
            var response = await MakeRequestTo(
                url ?? throw new ArgumentNullException(nameof(url), NoUrlProvided),
                null,
                cancellationToken);

            if (response.Status != 200)
            {
                throw new InvalidResponseException(response.Status);
            }

            var hypermediaProcessor = GetHypermediaProcessor(response);

            if (hypermediaProcessor == null)
            {
                throw new ResponseFormatNotSupportedException();
            }

            options = new HypermediaProcessingOptions(url, LinksPolicy).MergeWith(options);
            return(await hypermediaProcessor.Process(response, this, options));
        }