Beispiel #1
0
        /// <summary>
        ///     Find Full Hashes Asynchronously.
        /// </summary>
        /// <param name="request">
        ///     A <see cref="FullHashRequest" />.
        /// </param>
        /// <param name="cancellationToken">
        ///     A cancellation token to cancel the asynchronous operation with.
        /// </param>
        /// <returns>
        ///     A <see cref="FullHashResponse" />.
        /// </returns>
        /// <exception cref="Gee.External.Browsing.Clients.BrowsingClientException">
        ///     Thrown if an error communicating with the Google Safe Browsing API occurs.
        /// </exception>
        /// <exception cref="System.ArgumentNullException">
        ///     Thrown if <paramref name="request" /> is a null reference.
        /// </exception>
        /// <exception cref="System.ObjectDisposedException">
        ///     Thrown if the object is disposed.
        /// </exception>
        /// <exception cref="System.OperationCanceledException">
        ///     Thrown if the asynchronous operation is cancelled.
        /// </exception>
        /// <exception cref="System.TimeoutException">
        ///     Thrown if communication with the Google Safe Browsing API times out.
        /// </exception>
        public async Task <FullHashResponse> FindFullHashesAsync(FullHashRequest request, CancellationToken cancellationToken)
        {
            this.ThrowIfDisposed();
            try {
                var findFullHashesTask = this._client.FindFullHashesAsync(request, cancellationToken);
                var response           = await findFullHashesTask.ConfigureAwait(false);

                return(response);
            }
            catch (ArgumentNullException) {
                throw;
            }
            catch (BrowsingClientException) {
                throw;
            }
            catch (ObjectDisposedException) {
                this.Dispose();
                throw;
            }
            catch (OperationCanceledException) {
                throw;
            }
            catch (TimeoutException) {
                throw;
            }
            catch (Exception ex) {
                const string detailMessage = "An HTTP request to the Google Safe Browsing API failed.";
                throw new BrowsingClientException(detailMessage, HttpStatusCode.BadRequest, ex);
            }
        }
        /// <summary>
        ///     Set Request.
        /// </summary>
        /// <param name="value">
        ///     The <see cref="FullHashRequest" /> made to the Google Safe Browsing API for which the full hash
        ///     response has been returned.
        /// </param>
        /// <returns>
        ///     This full hash response builder.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        ///     Thrown if <paramref name="value" /> is a null reference.
        /// </exception>
        public FullHashResponseBuilder SetRequest(FullHashRequest value)
        {
            Guard.ThrowIf(nameof(value), value).Null();

            this.Request = value;
            return(this);
        }
Beispiel #3
0
        public Task <FullHashResponse> FindFullHashesAsync(FullHashRequest request, CancellationToken cancellationToken)
        {
            this.ThrowIfDisposed();

            Func <Task <FullHashResponse> > resiliencyPolicyAction = () => this._client.FindFullHashesAsync(request, cancellationToken);
            var executeResiliencyPolicyTask = this.ExecuteResiliencyPolicyAsync(resiliencyPolicyAction);

            return(executeResiliencyPolicyTask);
        }
Beispiel #4
0
        /// <summary>
        ///     Build a Full Hash Request.
        /// </summary>
        /// <returns>
        ///     A <see cref="FullHashRequest" />.
        /// </returns>
        public FullHashRequest Build()
        {
            // ...
            //
            // Reinitialize the builder's state to prevent it from corrupting the immutable built object's state after
            // its built. If the object holds a reference to the builder's state, any mutation to the builder's state
            // will be reflected in the built object's state.
            var fullHashRequest = new FullHashRequest(this);

            this.ClientMetadata     = ClientMetadata.Default;
            this.Queries            = new HashSet <FullHashQuery>();
            this.Sha256HashPrefixes = new HashSet <string>();

            return(fullHashRequest);
        }
Beispiel #5
0
        public FullHashResponse(FullHashRequest request, DateTime safeThreatsExpirationDate, IEnumerable <UnsafeThreat> unsafeThreats, DateTime?waitToDate)
        {
            Guard.ThrowIf(nameof(request), request).Null();

            this.Request = request;
            this.SafeThreatsExpirationDate = safeThreatsExpirationDate.ToUniversalTime();
            this.UnsafeThreats             = CreateUnsafeThreats(unsafeThreats);
            this.WaitToDate = waitToDate?.ToUniversalTime();

            // <summary>
            //      Create Unsafe Threats.
            // </summary>
            IReadOnlyCollection <UnsafeThreat> CreateUnsafeThreats(IEnumerable <UnsafeThreat> cUnsafeThreats)
            {
                IReadOnlyCollection <UnsafeThreat> cNewUnsafeThreats = Array.Empty <UnsafeThreat>();

                if (cUnsafeThreats != null)
                {
                    cNewUnsafeThreats = new List <UnsafeThreat>(cUnsafeThreats);
                }

                return(cNewUnsafeThreats);
            }
        }
        public static Task <FullHashResponse> FindFullHashesAsync(this IBrowsingClient @this, string sha256HashPrefix, IEnumerable <ThreatList> threatLists, CancellationToken cancellationToken)
        {
            Guard.ThrowIf(nameof(@this), @this).Null();
            Guard.ThrowIf(nameof(threatLists), threatLists).Null();

            // ...
            //
            // Throws an exception if the operation fails.
            var fullHashRequestBuilder = FullHashRequest.Build();

            fullHashRequestBuilder.AddSha256HashPrefix(sha256HashPrefix);
            foreach (var threatList in threatLists)
            {
                fullHashRequestBuilder.AddQuery(threatList.Descriptor, threatList.State);
            }

            // ...
            //
            // Throws an exception if the operation fails.
            var fullHashRequest    = fullHashRequestBuilder.Build();
            var findFullHashesTask = @this.FindFullHashesAsync(fullHashRequest, cancellationToken);

            return(findFullHashesTask);
        }
        /// <summary>
        ///     Find Full Hashes Asynchronously.
        /// </summary>
        /// <param name="this">
        ///     A <see cref="IBrowsingClient" />.
        /// </param>
        /// <param name="request">
        ///     A <see cref="FullHashRequest" />.
        /// </param>
        /// <returns>
        ///     A <see cref="FullHashResponse" />.
        /// </returns>
        /// <exception cref="Gee.External.Browsing.Clients.BrowsingClientException">
        ///     Thrown if an error communicating with the Google Safe Browsing API occurs.
        /// </exception>
        /// <exception cref="System.ArgumentNullException">
        ///     Thrown if <paramref name="this" /> is a null reference, or if <paramref name="request" /> is a null
        ///     reference.
        /// </exception>
        /// <exception cref="System.ObjectDisposedException">
        ///     Thrown if <paramref name="this" /> is disposed.
        /// </exception>
        /// <exception cref="System.TimeoutException">
        ///     Thrown if communication with the Google Safe Browsing API times out.
        /// </exception>
        public static Task <FullHashResponse> FindFullHashesAsync(this IBrowsingClient @this, FullHashRequest request)
        {
            Guard.ThrowIf(nameof(@this), @this).Null();

            // ...
            //
            // Throws an exception if the operation fails.
            var findFullHashesTask = @this.FindFullHashesAsync(request, CancellationToken.None);

            return(findFullHashesTask);
        }