Example #1
0
        /// <summary>
        /// Create a new PullZone pointing to an origin or a StorageZone
        /// </summary>
        /// <param name="name">Name of the new PullZone</param>
        /// <param name="originUrl">Origin url to pull data from</param>
        /// <param name="storageZoneId">Storage Zone identifier (If using StorageZone)</param>
        /// <returns>The added PullZone</returns>
        public async Task <PullZone> AddPullZone(string name, string originUrl, long?storageZoneId)
        {
            if (!Regexes.PullZoneName.IsMatch(name))
            {
                throw new BunnyBadRequestException("Invalid Zone name provided.");
            }

            Uri uriString;

            if (!Uri.TryCreate(originUrl, UriKind.Absolute, out uriString))
            {
                throw new BunnyBadRequestException("Invalid Origin Url provided.");
            }


            PullZone pullZone = new PullZone()
            {
                Name      = name,
                OriginUrl = uriString.ToString()
            };

            if (storageZoneId.HasValue)
            {
                if (storageZoneId.Value <= 0)
                {
                    throw new BunnyBadRequestException("Invalid Storage Zone Identifier provided.");
                }

                pullZone.StorageZoneId = storageZoneId.Value;
            }

            using (HttpContent httpContent = new StringContent(JsonWrapper.Serialize(pullZone)))
            {
                HttpResponseMessage httpResponse = await this.AccountKey.Client.PostAsync(GetPath("pullzone"), httpContent);

                switch (httpResponse.StatusCode)
                {
                case HttpStatusCode.Created:
                    return(await JsonWrapper.Deserialize <PullZone>(httpResponse));

                case HttpStatusCode.Unauthorized:
                    throw new BunnyUnauthorizedException();

                default:
                    throw new BunnyInvalidResponseException("Unexpected/unhandled response retrieved");
                }
            }
        }
Example #2
0
        /// <summary>
        /// Update a PullZone
        /// </summary>
        /// <param name="zoneId">PullZone identifier</param>
        /// <param name="pullZoneChages">PullZone to apply</param>
        /// <returns>Success state, throws if failed</returns>
        public async Task <bool> UpdatePullZone(long zoneId, PullZone pullZoneChages)
        {
            if (zoneId <= 0)
            {
                throw new BunnyBadRequestException("Invalid pullzone identifier provided.");
            }

            if (!Regexes.PullZoneName.IsMatch(pullZoneChages.Name))
            {
                throw new BunnyBadRequestException("Invalid pullzone name provided.");
            }

            Uri originUrl;

            if (!Uri.TryCreate(pullZoneChages.OriginUrl, UriKind.Absolute, out originUrl))
            {
                throw new BunnyBadRequestException("Invalid Origin Url provided.");
            }

            using (HttpContent httpContent = new StringContent(JsonWrapper.Serialize(pullZoneChages)))
            {
                HttpResponseMessage httpResponse = await this.AccountKey.Client.PostAsync(GetPath("pullzone/" + zoneId), httpContent);

                switch (httpResponse.StatusCode)
                {
                case HttpStatusCode.OK:
                    return(true);

                case HttpStatusCode.Unauthorized:
                    throw new BunnyUnauthorizedException();

                case HttpStatusCode.BadRequest:
                    ErrorMessage error = await JsonWrapper.Deserialize <ErrorMessage>(httpResponse);

                    throw new BunnyBadRequestException(error);

                default:
                    throw new BunnyInvalidResponseException("Unexpected/unhandled response retrieved");
                }
            }
        }