/// <summary>
        /// Downloads the specified list of artifacts using the provided configuration parameters
        /// </summary>
        /// <returns>
        /// The list of file paths to downloaded files.
        /// </returns>
        public List<string> Download(Artifacts artifacts, string artifactRelativeName, string directory, bool flatten, bool overwrite, bool extractArchives)
        {
            // validate that valid artifacts were provided
            if (artifacts == null)
            {
                throw new ArgumentException("Artifacts must be provided, please use one of the other methods of this class to retrieve them.");
            }

            // default the destination directory to the current directory if one was not provided.
            if (string.IsNullOrEmpty(directory))
            {
                directory = _systemIOWrapper.GetCurrentDirectory();
            }

            // build a list of urls to download.
            var urls = new List<string>();
            FillDownloadUrlList(artifacts, extractArchives, urls);

            var downloaded = new List<string>();
            foreach (var url in urls)
            {
                // user probably didnt use the artifact url retrieval functions
                if (!url.Contains(TeamCityRestUrlBegining) || !url.Contains(TeamCityRestUrlContentPart))
                {
                    throw new ArgumentException("Invalid artifact url provided! Please use one of the methods of the IBuildArtifacts interface to retrieve them.");
                }

                // figure out local filename
                var subStringToken = string.IsNullOrEmpty(artifactRelativeName) ? TeamCityRestUrlContentPart : artifactRelativeName;
                IEnumerable<string> parts = GetUrlParts(url, subStringToken);
                parts = parts.Select(part => part.TrimEnd(TeamCityArchiveIdentifier));
                var destination = flatten
                    ? parts.Last()
                    : string.Join(Path.DirectorySeparatorChar.ToString(CultureInfo.InvariantCulture), parts);
                destination = Path.Combine(directory, destination);

                // create directories that doesnt exist
                var directoryName = Path.GetDirectoryName(destination);
                if (directoryName != null && !_systemIOWrapper.DirectoryExists(directoryName))
                {
                    _systemIOWrapper.CreateDirectory(directoryName);
                }

                // add artifact to list regardless if it was downloaded or skipped
                downloaded.Add(Path.GetFullPath(destination));

                // if the file already exists delete it or move to next artifact
                if (_systemIOWrapper.FileExists(destination))
                {
                    if (overwrite) _systemIOWrapper.DeleteFile(destination);
                    else continue;
                }
                _caller.GetDownloadFormat(tempfile => _systemIOWrapper.MoveFile(tempfile, destination), url);
            }
            return downloaded;
        }
 public ArtifactWrapper2(ITeamCityCaller caller, Artifacts artifacts, string artifactRelativeName)
 {
     _caller = caller;
     _artifacts = artifacts;
     _artifactRelativeName = artifactRelativeName;
 }
 private void FillDownloadUrlList(Artifacts artifacts, bool extractArchives, ICollection<string> urls)
 {
     foreach (var artifact in artifacts.Files)
     {
         if (extractArchives || !IsArtifactUnderArchive(artifact))
         {
             if (IsFolder(artifact) || (extractArchives && IsArchive(artifact)))
             {
                 // if this artifact is a folder or an archive that needs to be extracted, then recall
                 // this method recursively for the children of this artifact.
                 if (artifact.Children != null)
                 {
                     var childArtifacts = _caller.Get<Artifacts>(artifact.Children.Href);
                     FillDownloadUrlList(childArtifacts, extractArchives, urls);
                 }
             }
             else
             {
                 urls.Add(artifact.Content.Href);
             }
         }
     }
 }