Example #1
0
        private void AddBlobFormatXml(XmlWriter writer)
        {
            if (BlobNames.Count > 0 || BlobFormat != BlobFormat.Unknown)
            {
                writer.WriteStartElement("blob-payload-request");

                if (BlobNames.Count > 0)
                {
                    writer.WriteStartElement("blob-filters");

                    for (int index = 0; index < BlobNames.Count; ++index)
                    {
                        writer.WriteStartElement("blob-filter");
                        writer.WriteElementString("blob-name", BlobNames[index]);
                        writer.WriteEndElement();
                    }

                    writer.WriteEndElement();
                }

                BlobFormat format = BlobFormat;
                if (format == BlobFormat.Unknown)
                {
                    format = BlobFormat.Default;
                }

                writer.WriteStartElement("blob-format");
                writer.WriteElementString("blob-format-spec", format.ToString().ToLowerInvariant());
                writer.WriteEndElement();

                writer.WriteEndElement();
            }
        }
        /// <summary>
        /// Retrieve a list of random blob values as an asynchronous operation
        /// </summary>
        /// <param name="numberOfItemsToReturn">How many random blob values you need. Must be between 1 and 100.</param>
        /// <param name="size">The size of each blob, measured in bits. Must be between 1 and 1048576 and must be divisible by 8.</param>
        /// <param name="format">Specifies the format in which the blobs will be returned, default value is Base64</param>
        /// <returns>All information returned from random service, include the list of blob values</returns>
        public async Task<DataResponseInfo<string>> GenerateBlobsAsync(int numberOfItemsToReturn, int size, BlobFormat format = BlobFormat.Base64)
        {
            var parameters = BlobParameters.Create(numberOfItemsToReturn, size, format, _verifyOriginater);
            _verifyOriginater = false;

            var response = await _methodCallBroker.GenerateAsync(parameters);
            return response as DataResponseInfo<string>;
        }
        /// <summary>
        /// Retrieves a list of random blob values in an asynchronous manners
        /// </summary>
        /// <param name="numberOfItemsToReturn">How many random blob values you need. Must be between 1 and 100.</param>
        /// <param name="size">The size of each blob, measured in bits. Must be between 1 and 1048576 and must be divisible by 8.</param>
        /// <param name="format">Specifies the format in which the blobs will be returned, default value is Base64</param>
        /// <returns>List of random blob values</returns>
        public virtual async Task<IEnumerable<string>> GenerateBlobsAsync(int numberOfItemsToReturn, int size, BlobFormat format = BlobFormat.Base64)
        {
            IParameters requestParameters = BlobParameters.Create(numberOfItemsToReturn, size, format);
            MethodCallBroker broker = new MethodCallBroker(RequestBuilder, RandomService, BeforeRequestCommandFactory, ResponseHandlerFactory);
            await broker.GenerateAsync(requestParameters);

            return (ResponseParser.ResponseInfo as DataResponseInfo<string>)?.Data;
        }
        /// <summary>
        /// Validate and set the parameters properties
        /// </summary>
        private void SetParameters(int numberOfItemsToReturn, int size, BlobFormat format)
        {
            if (!numberOfItemsToReturn.Between(1, MaxItemsAllowed))
                throw new RandomOrgRunTimeException(ResourceHelper.GetString(StringsConstants.NUMBER_ITEMS_RETURNED_OUT_OF_RANGE, MaxItemsAllowed));

            if (!size.Between(1, 1048576))
                throw new RandomOrgRunTimeException(ResourceHelper.GetString(StringsConstants.MINIMUM_VALUE_OUT_OF_RANGE));

            if (size % 8 != 0)
                throw new RandomOrgRunTimeException(ResourceHelper.GetString(StringsConstants.BLOB_SIZE_NOT_DIVISIBLE_BY_8));

            NumberOfItemsToReturn = numberOfItemsToReturn;
            Size = size;
            Format = format;
        }
Example #5
0
        /// <summary>
        /// Download a blob's contents.
        /// </summary>
        /// <param name="repositoryId">The ID of the repository</param>
        /// <param name="blobId">The object ID of the blob</param>
        /// <param name="format">The format to download as</param>
        /// <param name="outputStream">The stream to write the response content to asynchronously</param>
        public async Task DownloadBlob(Guid repositoryId, ObjectId blobId, BlobFormat format, Stream outputStream)
        {
            Assert.NotNull(repositoryId, "repositoryId");
            Assert.NotNull(blobId, "blobId");
            Assert.NotNull(outputStream, "outputStream");

            var request = new TfsRestRequest("/_apis/git/repositories/{RepositoryId}/blobs/{BlobId}");
            request.AddUrlSegment("RepositoryId", repositoryId.ToString());
            request.AddUrlSegment("BlobId", blobId.ToString());

            if (format == BlobFormat.Zip)
            {
                request.AddUrlSegment("$format", "zip");
                request.AddHeader("Accept", "application/zip");
            }
            else
            {
                request.AddUrlSegment("$format", "octetstream");
                request.AddHeader("Accept", "application/octet-stream");
            }

            await Executor.Execute(request, outputStream);
        }
 /// <summary>
 /// Build an instance of <see cref="BlobParameters"/>
 /// </summary>
 /// <param name="numberOfItemsToReturn">How many random blob values you need. Must be between 1 and 100.</param>
 /// <param name="size">The size of each blob, measured in bits. Must be between 1 and 1048576 and must be divisible by 8.</param>
 /// <param name="format">Specifies the format in which the blobs will be returned, default value is Base64</param>
 /// <param name="verifyOriginator">Verify that the response is what was sent by random.org and it was not tampered with before receieved</param>
 /// <returns>Instance of <see cref="BlobParameters"/></returns>
 public static BlobParameters Create(int numberOfItemsToReturn, int size, BlobFormat format = BlobFormat.Base64, bool verifyOriginator = false)
 {
     var parameters = new BlobParameters(MethodType.Blob, verifyOriginator);
     parameters.SetParameters(numberOfItemsToReturn, size, format);
     return parameters;
 }
Example #7
0
        public int DownloadBlob(string[] args)
        {
            if (args.Length < 2)
            {
                Console.Error.WriteLine("usage: {0} <url> Git.DownloadBlob [repositoryId] [blobId] [--filename=filename]", Program.ProgramName);
                return(1);
            }

            Guid       repositoryId = new Guid(args[0]);
            ObjectId   blobId       = new ObjectId(args[1]);
            string     filename     = null;
            BlobFormat format       = BlobFormat.Raw;

            for (int i = 1; i < args.Length; i++)
            {
                if (args[i].StartsWith("--") || args[i].StartsWith("/"))
                {
                    string   arg     = args[i].Substring(args[i].StartsWith("--") ? 2 : 1);
                    string[] options = arg.Split(new char[] { '=' }, 2);

                    if (options[0] != null &&
                        options[0].Equals("filename", StringComparison.InvariantCultureIgnoreCase))
                    {
                        filename = options[1];
                    }
                    else if (options[0] != null &&
                             options[0].Equals("format", StringComparison.InvariantCultureIgnoreCase))
                    {
                        if (options[1].Equals("raw", StringComparison.InvariantCultureIgnoreCase))
                        {
                            format = BlobFormat.Raw;
                        }
                        else if (options[1].Equals("zip", StringComparison.InvariantCultureIgnoreCase))
                        {
                            format = BlobFormat.Zip;
                        }
                        else
                        {
                            Console.Error.WriteLine("{0}: unknown format '{1}'", Program.ProgramName, options[1]);
                            return(1);
                        }
                    }
                    else
                    {
                        Console.Error.WriteLine("{0}: unknown option '{1}'", Program.ProgramName, args[i]);
                        return(1);
                    }
                }
            }

            if (filename == null)
            {
                filename = (format == BlobFormat.Zip) ? String.Format("{0}", args[1]) : args[1];
            }

            Task.Run(async() =>
            {
                using (Stream outputStream = File.Create(filename))
                {
                    await Client.Git.DownloadBlob(repositoryId, blobId, format, outputStream);
                    outputStream.Close();
                }
            }).Wait();

            return(0);
        }