예제 #1
0
        /// <summary>
        /// Create an Address with optional verifications.
        /// </summary>
        /// <param name="address">Address to create</param>
        /// <param name="verify">Verification flags to to control verification. You can verify the delivery address or the
        /// extended zip4 value. If you use the strict versions an HttpException to be raised if unsucessful.
        /// </param>
        /// <returns>Address instance.</returns>
        public Task <Address> CreateAddress(
            Address address,
            VerificationFlags verify = VerificationFlags.None)
        {
            var request = new EasyPostRequest("addresses", Method.POST);

            request.AddBody(address.AsDictionary(), "address");

            if ((verify & VerificationFlags.Delivery) != 0)
            {
                request.AddParameter("verify[]", "delivery", ParameterType.QueryString);
            }
            if ((verify & VerificationFlags.Zip4) != 0)
            {
                request.AddParameter("verify[]", "zip4", ParameterType.QueryString);
            }
            if ((verify & VerificationFlags.DeliveryStrict) != 0)
            {
                request.AddParameter("verify_strict[]", "delivery", ParameterType.QueryString);
            }
            if ((verify & VerificationFlags.Zip4Strict) != 0)
            {
                request.AddParameter("verify_strict[]", "zip4", ParameterType.QueryString);
            }

            return(Execute <Address>(request));
        }
예제 #2
0
        /// <summary>
        /// Internal function to execute a typed request
        /// </summary>
        /// <typeparam name="TResponse">Type of the JSON response we are expecting</typeparam>
        /// <param name="request">EasyPost request to execute</param>
        /// <returns>Response for the request</returns>
        private async Task <TResponse> Execute <TResponse>(
            EasyPostRequest request) where TResponse : new()
        {
            IRestResponse <TResponse> response;

            if (ExecuteNonAsync)
            {
                response = RestClient.Execute <TResponse>(PrepareRequest(request));
            }
            else
            {
                response = await RestClient.ExecuteTaskAsync <TResponse>(PrepareRequest(request));
            }
            var statusCode = response.StatusCode;
            var data       = response.Data;

            if (data == null || statusCode >= HttpStatusCode.BadRequest)
            {
                // Bail early if this is not an EasyPost object
                var          result = data as EasyPostObject;
                RequestError requestError;
                if (result == null)
                {
                    // Return the RestSharp error message if we can
                    data   = new TResponse();
                    result = data as EasyPostObject;
                    if (response.ErrorMessage == null || result == null)
                    {
                        return(default(TResponse));
                    }
                    requestError = new RequestError {
                        Code    = "RESPONSE.ERROR",
                        Message = response.ErrorMessage,
                        Errors  = new List <Error>(),
                    };
                }
                else
                {
                    // Try to parse any generic EasyPost request errors first
                    var deserializer = new JsonDeserializer {
                        RootElement = "error",
                    };
                    requestError = deserializer.Deserialize <RequestError>(response);
                    if (requestError.Code == null)
                    {
                        // Can't make sense of the error so return a general one
                        requestError = new RequestError {
                            Code    = "RESPONSE.PARSE_ERROR",
                            Message = "Unknown request error or unable to parse response",
                            Errors  = new List <Error>(),
                        };
                    }
                }
                requestError.StatusCode = statusCode;
                requestError.Content    = response.Content;
                result.RequestError     = requestError;
            }

            return(data);
        }
        /// <summary>
        /// Remove this CarrierAccount from your account.
        /// </summary>
        /// <param name="id">Carrier account id</param>
        public Task DestroyCarrierAccount(
            string id)
        {
            var request = new EasyPostRequest("carrier_accounts/{id}", Method.DELETE);

            request.AddUrlSegment("id", id);
            return(Execute(request));
        }
예제 #4
0
        /// <summary>
        /// Destroys a user
        /// </summary>
        /// <param name="id">ID of the user</param>
        public Task DestroyUser(
            string id)
        {
            var request = new EasyPostRequest("users/{id}", Method.DELETE);

            request.AddUrlSegment("id", id);
            return(Execute(request));
        }
예제 #5
0
        /// <summary>
        /// Retrieve a Shipment from its id.
        /// </summary>
        /// <param name="id">String representing a Shipment. Starts with "shp_".</param>
        /// <returns>Shipment instance.</returns>
        public Task <Shipment> GetShipment(
            string id)
        {
            var request = new EasyPostRequest("shipments/{id}");

            request.AddUrlSegment("id", id);

            return(Execute <Shipment>(request));
        }
예제 #6
0
        /// <summary>
        /// Create a Parcel.
        /// </summary>
        /// <param name="parcel">Parcel to create</param>
        /// <returns>Parcel instance.</returns>
        public async Task <Parcel> CreateParcel(
            Parcel parcel)
        {
            var request = new EasyPostRequest("parcels", Method.POST);

            request.AddBody(parcel.AsDictionary(), "parcel");

            return(await Execute <Parcel>(request));
        }
예제 #7
0
        /// <summary>
        /// Retrieve a Parcel from its id.
        /// </summary>
        /// <param name="id">String representing a Parcel. Starts with "prcl_".</param>
        /// <returns>Parcel instance.</returns>
        public async Task <Parcel> GetParcel(
            string id)
        {
            var request = new EasyPostRequest("parcels/{id}");

            request.AddUrlSegment("id", id);

            return(await Execute <Parcel>(request));
        }
예제 #8
0
        /// <summary>
        /// Retrieve a Tracker from its id.
        /// </summary>
        /// <param name="id">String representing a Tracker. Starts with "trk_".</param>
        /// <returns>Tracker instance.</returns>
        public async Task <Tracker> GetTracker(
            string id)
        {
            var request = new EasyPostRequest("trackers/{id}");

            request.AddUrlSegment("id", id);

            return(await Execute <Tracker>(request));
        }
예제 #9
0
        /// <summary>
        /// Cancel this pickup.
        /// </summary>
        /// <param name="id">Pickup id to cancel</param>
        /// <returns>Pickup instance.</returns>
        public async Task <Pickup> CancelPickp(
            string id)
        {
            var request = new EasyPostRequest("pickups/{id}/cancel", Method.POST);

            request.AddUrlSegment("id", id);

            return(await Execute <Pickup>(request));
        }
예제 #10
0
        /// <summary>
        /// Retrieve a Pickup from its id.
        /// </summary>
        /// <param name="id">String representing a Pickup. Starts with "pickup_".</param>
        /// <returns>Pickup instance.</returns>
        public async Task <Pickup> GetPickup(
            string id)
        {
            var request = new EasyPostRequest("pickups/{id}");

            request.AddUrlSegment("id", id);

            return(await Execute <Pickup>(request));
        }
예제 #11
0
        /// <summary>
        /// Retrieve a Event from its id.
        /// </summary>
        /// <param name="id">String representing a Event. Starts with "evt_".</param>
        /// <returns>EasyPost.Event instance.</returns>
        public async Task <Event> GetEvent(
            string id)
        {
            var request = new EasyPostRequest("events/{id}");

            request.AddUrlSegment("id", id);

            return(await Execute <Event>(request));
        }
        /// <summary>
        /// Create a CarrierAccount.
        /// </summary>
        /// <param name="carrierAccount">Carriern account details to create</param>
        /// <returns>CarrierAccount instance.</returns>
        public async Task <CarrierAccount> CreateCarrierAccount(
            CarrierAccount carrierAccount)
        {
            var request = new EasyPostRequest("carrier_accounts", Method.POST);

            request.AddBody(carrierAccount.AsDictionary(), "carrier_account");

            return(await Execute <CarrierAccount>(request));
        }
        /// <summary>
        /// Retrieve a CarrierAccount from its id.
        /// </summary>
        /// <param name="id">String representing a carrier account. Starts with "ca_".</param>
        /// <returns>CarrierAccount instance.</returns>
        public async Task <CarrierAccount> GetCarrierAccount(
            string id)
        {
            var request = new EasyPostRequest("carrier_accounts/{id}");

            request.AddUrlSegment("id", id);

            return(await Execute <CarrierAccount>(request));
        }
예제 #14
0
        /// <summary>
        /// Create a Order.
        /// </summary>
        /// <param name="order">Order details</param>
        /// <returns>Order instance.</returns>
        public async Task <Order> CreateOrder(
            Order order)
        {
            var request = new EasyPostRequest("orders", Method.POST);

            request.AddBody(order.AsDictionary(), "order");

            return(await Execute <Order>(request));
        }
예제 #15
0
        /// <summary>
        /// Create a Container.
        /// </summary>
        /// <param name="container">Container parameters</param>
        /// <returns>EasyPost.Container instance.</returns>
        public async Task <Container> CreateContainer(
            Container container)
        {
            var request = new EasyPostRequest("containers", Method.POST);

            request.AddBody(container.AsDictionary(), "container");

            return(await Execute <Container>(request));
        }
예제 #16
0
        /// <summary>
        /// Retrieve an Address from its id.
        /// </summary>
        /// <param name="id">String representing an Address. Starts with "adr_".</param>
        /// <returns>EasyPost.Address instance.</returns>
        public Task <Address> GetAddress(
            string id)
        {
            var request = new EasyPostRequest("addresses/{id}");

            request.AddUrlSegment("id", id);

            return(Execute <Address>(request));
        }
        /// <summary>
        /// Create a CustomsItem.
        /// </summary>
        /// <param name="customsItem">Customs item parameters</param>
        /// <returns>EasyPost.CustomsItem instance.</returns>
        public async Task <CustomsItem> CreateCustomsItem(
            CustomsItem customsItem)
        {
            var request = new EasyPostRequest("customs_items", Method.POST);

            request.AddBody(customsItem.AsDictionary(), "customs_item");

            return(await Execute <CustomsItem>(request));
        }
        /// <summary>
        /// Internal function to prepare the request to be executed
        /// </summary>
        /// <param name="request">EasyPost request to be executed</param>
        /// <returns>RestSharp request to execute</returns>
        internal RestRequest PrepareRequest(
            EasyPostRequest request)
        {
            var restRequest = RestClient.AddAuthorizationToRequest(request);

            restRequest.AddHeader("user_agent", string.Concat("EasyPost/CSharpASync/", Version));
            restRequest.AddHeader("content_type", "application/x-www-form-urlencoded");
            return(restRequest);
        }
        /// <summary>
        /// Retrieve a CustomsItem from its id.
        /// </summary>
        /// <param name="id">String representing a CustomsItem. Starts with "cstitem_".</param>
        /// <returns>CustomsItem instance.</returns>
        public async Task <CustomsItem> GetCustomsItem(
            string id)
        {
            var request = new EasyPostRequest("customs_items/{id}");

            request.AddUrlSegment("id", id);

            return(await Execute <CustomsItem>(request));
        }
        /// <summary>
        /// Create a Shipment.
        /// </summary>
        /// <param name="shipment">Shipment details</param>
        /// <returns>Shipment instance.</returns>
        public async Task <Shipment> CreateShipment(
            Shipment shipment)
        {
            var request = new EasyPostRequest("shipments", Method.POST);

            request.AddBody(shipment.AsDictionary(), "shipment");

            return(await Execute <Shipment>(request));
        }
        /// <summary>
        /// Send a refund request to the carrier the shipment was purchased from.
        /// </summary>
        /// <param name="id">The id of the shipment to refund</param>
        /// <returns>Shipment instance.</returns>
        public async Task <Shipment> RefundShipment(
            string id)
        {
            var request = new EasyPostRequest("shipments/{id}/refund");

            request.AddUrlSegment("id", id);

            return(await Execute <Shipment>(request));
        }
예제 #22
0
        /// <summary>
        /// Asychronously generate a scan form for the batch.
        /// </summary>
        /// <param name="id">Batch id to generate the label for</param>
        /// <returns>Batch instance.</returns>
        public async Task <Batch> GenerateScanFormForBatch(
            string id)
        {
            var request = new EasyPostRequest("batches/{id}/scan_form", Method.POST);

            request.AddUrlSegment("id", id);

            return(await Execute <Batch>(request));
        }
예제 #23
0
        /// <summary>
        /// Retrieve a Batch from its id.
        /// </summary>
        /// <param name="id">String representing a Batch. Starts with "batch_".</param>
        /// <returns>Batch instance.</returns>
        public async Task <Batch> GetBatch(
            string id)
        {
            var request = new EasyPostRequest("batches/{id}");

            request.AddUrlSegment("id", id);

            return(await Execute <Batch>(request));
        }
예제 #24
0
        /// <summary>
        /// Buy labels for all shipments within a batch. The Batch's state must be "created" before purchasing.
        /// </summary>
        /// <param name="id">Batch id to add the shipments to</param>
        /// <returns>Batch instance.</returns>
        public async Task <Batch> BuyLabelsForBatch(
            string id)
        {
            var request = new EasyPostRequest("batches/{id}/buy", Method.POST);

            request.AddUrlSegment("id", id);

            return(await Execute <Batch>(request));
        }
        /// <summary>
        /// Create a CustomsInfo.
        /// </summary>
        /// <param name="customsInfo">Customs info to create</param>
        /// <returns>EasyPost.CustomsInfo instance.</returns>
        public async Task <CustomsInfo> CreateCustomsInfo(
            CustomsInfo customsInfo)
        {
            var request = new EasyPostRequest("customs_infos", Method.POST);

            request.AddBody(customsInfo.AsDictionary(), "customs_info");

            return(await Execute <CustomsInfo>(request));
        }
예제 #26
0
        /// <summary>
        /// Retrieve a Rate from its id.
        /// </summary>
        /// <param name="id">String representing a Rate. Starts with "rate_".</param>
        /// <returns>CarrierRate instance.</returns>
        public async Task <CarrierRate> GetRate(
            string id)
        {
            var request = new EasyPostRequest("rates/{id}");

            request.AddUrlSegment("id", id);

            return(await Execute <CarrierRate>(request));
        }
예제 #27
0
        /// <summary>
        /// Create an Item.
        /// </summary>
        /// <param name="item">Item to create</param>
        /// <returns>EasyPost.Item instance.</returns>
        public async Task <Item> CreateItem(
            Item item)
        {
            var request = new EasyPostRequest("items", Method.POST);

            request.AddBody(item.AsDictionary(), "item");

            return(await Execute <Item>(request));
        }
예제 #28
0
        /// <summary>
        /// Update the User associated with the api_key specified.
        /// </summary>
        /// <param name="user">User parameters to update</param>
        public async Task <User> UpdateUser(
            User user)
        {
            var request = new EasyPostRequest("users/{id}", Method.PUT);

            request.AddUrlSegment("id", user.Id);
            request.AddBody(user.AsDictionary(), "user");

            return(await Execute <User>(request));
        }
        /// <summary>
        /// Update this CarrierAccount.
        /// </summary>
        /// <param name="carrierAccount">Carrier account details</param>
        /// <returns>CarrierAccount instance.</returns>
        public async Task <CarrierAccount> UpdateCarrierAccount(
            CarrierAccount carrierAccount)
        {
            var request = new EasyPostRequest("carrier_accounts/{id}", Method.PUT);

            request.AddUrlSegment("id", carrierAccount.Id);
            request.AddBody(carrierAccount.AsDictionary(), "carrier_account");

            return(await Execute <CarrierAccount>(request));
        }
예제 #30
0
        /// <summary>
        /// Internal function to prepate the request to be executed
        /// </summary>
        /// <param name="request">EasyPost request to be executed</param>
        /// <returns>RestSharp request to execute</returns>
        internal RestRequest PrepareRequest(
            EasyPostRequest request)
        {
            var restRequest = request.RestRequest;

            restRequest.AddHeader("user_agent", string.Concat("EasyPost/CSharpASync/", Version));
            restRequest.AddHeader("authorization", "Bearer " + Configuration.ApiKey);
            restRequest.AddHeader("content_type", "application/x-www-form-urlencoded");

            return(restRequest);
        }