Esempio n. 1
0
        private async Task <List <PackageDetails> > GetPackages(NugetFeedConfig feed)
        {
            var request = new HttpRequestMessage(HttpMethod.Get, feed.FeedURL);
            var apiKey  = Crypto.DecryptString(feed.ApiKey, SquidConfig.CryptoKey);

            if (string.IsNullOrWhiteSpace(feed.ApiKeyHeaderName))
            {
                request.Headers.Add("X-NuGet-ApiKey", apiKey);
            }
            else
            {
                request.Headers.Add(feed.ApiKeyHeaderName, apiKey);
            }

            var response = await _client.SendAsync(request);

            if (response.IsSuccessStatusCode)
            {
                var content = await response.Content.ReadAsStringAsync();

                var packageVersions = JsonConvert.DeserializeObject <PackageVersions>(content);

                if (!feed.Valid)
                {
                    feed.ErrorMessage = null;
                    feed.Valid        = true;

                    await _repo.Update(feed);
                }

                if (packageVersions != null)
                {
                    var packageDetails = packageVersions.data;

                    foreach (var package in packageDetails)
                    {
                        package.FeedId   = feed.Id;
                        package.FeedName = feed.Name;
                    }

                    return(packageDetails);
                }
            }
            else
            {
                var content = await response.Content.ReadAsStringAsync();

                feed.ErrorMessage = content;
                feed.Valid        = false;

                await _repo.Update(feed);
            }

            return(null);
        }
Esempio n. 2
0
        public async Task <IActionResult> Put(string id, [FromBody] NuGetFeedFormModel model)
        {
            if (model == null)
            {
                return(BadRequest("Model invalid"));
            }

            var feed = await _repo.Get(id);

            if (feed == null)
            {
                return(NotFound());
            }

            feed.Name             = model.Name;
            feed.FeedURL          = model.FeedURL;
            feed.ApiKeyHeaderName = model.ApiKeyHeaderName;
            if (!string.IsNullOrWhiteSpace(model.ApiKey))
            {
                feed.ApiKey = Crypto.EncryptString(model.ApiKey, SquidConfig.CryptoKey);
            }
            feed.Valid        = false;
            feed.ErrorMessage = "Validation pending...";

            await _repo.Update(feed);

            var returnModel = _mapper.Map <NuGetFeedModel>(feed);

            return(Ok(returnModel));
        }