Exemple #1
0
 /// <summary>
 /// Creates a new list services response
 /// </summary>
 /// <param name="services"></param>
 internal ListServicesResponse(GetOfferIndexFileResponse offer) : base(offer)
 {
     if (offer == null)
     {
         throw new ArgumentNullException("offer");
     }
 }
Exemple #2
0
        /// <summary>
        /// Lists the services that have price data available
        /// </summary>
        /// <returns></returns>
        public async Task <ListServicesResponse> ListServicesAsync(ListServicesRequest request)
        {
            if (this.cachedOfferIndexFile == null || this.Config.NoCache)
            {
                GetOfferIndexFileResponse response = await this.GetOfferIndexFileAsync();

                if (response.IsError())
                {
                    throw new PriceListException(response.ResponseMetadata.Metadata["ErrorReason"], response.HttpStatusCode);
                }

                this.cachedOfferIndexFile = response;
            }

            return(new ListServicesResponse(this.cachedOfferIndexFile));
        }
Exemple #3
0
        /// <summary>
        /// Gets price data for a specified product
        /// </summary>
        /// <param name="product">The product to get price data for</param>
        /// <returns></returns>
        public async Task <GetProductResponse> GetProductAsync(GetProductRequest request)
        {
            if (this.cachedOfferIndexFile == null || this.Config.NoCache)
            {
                GetOfferIndexFileResponse offerIndexFile = await GetOfferIndexFileAsync();

                if (offerIndexFile.IsError())
                {
                    throw new PriceListException(offerIndexFile.ResponseMetadata.Metadata.ContainsKey("ErrorReason") ? offerIndexFile.ResponseMetadata.Metadata["ErrorReason"] : "", offerIndexFile.HttpStatusCode);
                }

                this.cachedOfferIndexFile = offerIndexFile;
            }

            IEnumerable <KeyValuePair <string, Offer> > offers = this.cachedOfferIndexFile.OfferIndexFile.Offers.Where(
                x => x.Key.Equals(request.ServiceCode, StringComparison.OrdinalIgnoreCase)
                );

            if (!offers.Any())
            {
                throw new ArgumentException($"No product found matching {request.ServiceCode}.");
            }

            string path = offers.First().Value.CurrentVersionUrl;

            switch (request.Format)
            {
            case Format.CSV:
            {
                path = System.IO.Path.ChangeExtension(path, "csv");
                break;
            }

            case Format.JSON:
            {
                path = System.IO.Path.ChangeExtension(path, "json");
                break;
            }

            default:
            {
                throw new ArgumentException($"Unknown format: {request.Format.ToString()}.");
            }
            }

            return(await ExecuteRequestAsync(path, nameof(path), (x) => new GetProductResponse(x, request.Format, request.ServiceCode)));
        }