ShipEngine / shipengine-dotnet

The official ShipEngine SDK for C# and the .NET Framework
https://www.shipengine.com/docs/
Apache License 2.0
11 stars 11 forks source link

getRatesWithShipmentDetails #63

Open wentao709 opened 1 year ago

wentao709 commented 1 year ago

Hi Devs,

I'm having trouble with this API: getRatesWithShipmentDetails

I'm calling it like this:

const shipEngineResults = await shipengine.getRatesWithShipmentDetails(shippingInfo);

where shippingInfo = { rateOptions: { carrierIds: [ 'se-2884143', 'se-2884144', 'se-2884146' ], serviceCodes: [] }, shipment: { validateAddress: 'validate_only', confirmation: 'none', shipFrom: { name: 'Tom Hanks', phone: '7146129169', postalCode: '93306', companyName: 'Test Company', countryCode: 'US', addressLine1: '3218 Kaibab Avenue', addressLine2: '', cityLocality: 'Bakersfield', stateProvince: 'CA', addressResidentialIndicator: 'no' }, shipTo: { name: 'Nxtlives Dev', phone: null, addressLine1: '800 W 6th St', addressLine2: null, cityLocality: 'Los Angeles', stateProvince: 'CA', countryCode: 'US', postalCode: '90017' }, packages: [ [Object] ] } }

Sometimes I get the error saying { err: ShipEngineError: The ShipEngine /v1/rates API timed out. at sendRequestWithRetry (/Users/wentaozhou/Documents/GitHub/nextsite_server/node_modules/shipengine/cjs/client/index.js:44:23) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async Object.post (/Users/wentaozhou/Documents/GitHub/nextsite_server/node_modules/shipengine/cjs/client/index.js:14:12) at async Object.getRatesWithShipmentDetails (/Users/wentaozhou/Documents/GitHub/nextsite_server/node_modules/shipengine/cjs/get-rates-with-shipment-details/index.js:17:22) at async itemShipping (file:///Users/wentaozhou/Documents/GitHub/nextsite_server/utils/order.js:1109:31) at async file:///Users/wentaozhou/Documents/GitHub/nextsite_server/controllers/orderController.js:1579:26 at async Promise.all (index 0) at async get_cart_shipping (file:///Users/wentaozhou/Documents/GitHub/nextsite_server/controllers/orderController.js:1569:23) { source: 'shipengine', type: 'system', code: 'timeout', requestID: undefined, url: URL { href: 'https://www.shipengine.com/docs/errors/codes/', origin: 'https://www.shipengine.com/', protocol: 'https:', username: '', password: '', host: 'www.shipengine.com', hostname: 'www.shipengine.com', port: '', pathname: '/docs/errors/codes/', search: '', searchParams: URLSearchParams {}, hash: '' } } }

Please note that this error does not happen every time I call it using the same json input. I would say there's a 50% chance that the error occurs, and other times the call is successful. do you know why this occurs?

Thanks, Wentao Zhou

AnneOReilly commented 1 year ago

Hey @wentao709 ! It looks like you're running the default timeout frame of 5 seconds. In Sandbox, it's common for carriers not to meet this timeframe. You can modify the timeout for the SDK as a configuration variable. 60 seconds will be the maximum.

https://github.com/ShipEngine/shipengine-dotnet/blob/9158d2467c15f23660fe687e7542586566af5a53/ShipEngine/Config.cs#L31

wentao709 commented 1 year ago

hi, what is the right way to change the timeout?

AnneOReilly commented 1 year ago

@wentao709 You can set timeout as a parameter at the same place in your code where you are setting the API key.

For example, if you are instantiating your config object like this: var config = new Config(apiKey: apiKeyString); Then you can make this adjustment: var config = new Config(apiKey: apiKeyString, timeout: 60);

wentao709 commented 1 year ago

sorry I guess my question is how do I import ShipEngineConfig from the node module? I can import shipengine as import Shipengine from 'shipengine';

not sure how to import ShipEngineConfig to my node.js server

brandonraphael commented 1 year ago

Hey @AnneOReilly, I'm also working with @wentao709 on this, thanks for the assistance. We have configured shipEngine to have a 60 second timeout now in js:

const shipEngineConfig = {
  apiKey: config.shipengineKey,
  timeout: 60,
};
const shipengine = new ShipEngine(shipEngineConfig);

Below is the ship engine config (JSON stringified) at the time of call to /v1/rates using the the function: const shipEngineResults = await shipengine.getRatesWithShipmentDetails(shipment);

Ship Engine config:

 ShipEngine {
   config: NormalizedConfig {
     apiKey: 'XXXXXXXXXXXXXXX',
     baseURL: URL {
       href: 'https://api.shipengine.com/',
       origin: 'https://api.shipengine.com',
       protocol: 'https:',
       username: '',
       password: '',
       host: 'api.shipengine.com',
       hostname: 'api.shipengine.com',
       port: '',
       pathname: '/',
       search: '',
       searchParams: URLSearchParams {},
       hash: ''
     },
     pageSize: 50,
     retries: 1,
     timeout: 60
   }
 }

We are still seeing the same timeout error with that config:

 itemShipping Error  ShipEngineError: The ShipEngine /v1/rates API timed out.
     at sendRequestWithRetry (/home/ec2-user/NxtLives/nextsite_server/node_modules/shipengine/cjs/client/index.js:44:23)
     at processTicksAndRejections (node:internal/process/task_queues:96:5)
     at async Object.post (/home/ec2-user/NxtLives/nextsite_server/node_modules/shipengine/cjs/client/index.js:14:12)
     at async Object.getRatesWithShipmentDetails (/home/ec2-user/NxtLives/nextsite_server/node_modules/shipengine/cjs/get-rates-with-shipment-details/index.js:17:22)
     at async itemShipping (file:///home/ec2-user/NxtLives/nextsite_server/utils/order.js:1129:31)
     at async file:///home/ec2-user/NxtLives/nextsite_server/controllers/orderController.js:1646:26
     at async Promise.all (index 0)
     at async get_cart_shipping (file:///home/ec2-user/NxtLives/nextsite_server/controllers/orderController.js:1636:23) {
   source: 'shipengine',
   type: 'system',
   code: 'timeout',
   requestID: undefined,
   url: URL {
     href: 'https://www.shipengine.com/docs/errors/codes/',
     origin: 'https://www.shipengine.com',
     protocol: 'https:',
     username: '',
     password: '',
     host: 'www.shipengine.com',
     hostname: 'www.shipengine.com',
     port: '',
     pathname: '/docs/errors/codes/',
     search: '',
     searchParams: URLSearchParams {},
     hash: ''
   }
 }

I also confirmed it was not the shipping info used that is causing the problem, as I created and tested the cUrl manually using the same shipping parameters and was able to get a successful response:

curl --location 'https://api.shipengine.com/v1/rates' \
--header 'Api-Key: XXXXXXXXXXXX' \
--header 'Content-Type: application/json' \
--data '{
    "rate_options": {
        "carrier_ids": [
            "se-4994286",
            "se-4994020"
        ],
        "service_codes": []
    },
    "shipment": {
        "validate_address": "validate_only",
        "confirmation": "none",
        "ship_from": {
            "name": "Jane Smith",
            "phone": "2135370158",
            "postal_code": "90017",
            "company_name": "Test Company",
            "country_code": "US",
            "address_line1": "1111 Wilshire Blvd",
            "address_line2": "",
            "city_locality": "Los Angeles",
            "state_province": "CA",
            "address_residential_indicator": "no"
        },
        "ship_to": {
            "name": "John Doe",
            "phone": null,
            "address_line1": "1833 Via Quinto",
            "address_line2": null,
            "city_locality": "Oceanside",
            "state_province": "CA",
            "country_code": "US",
            "postal_code": "92056"
        },
        "packages": [
            {
                "weight": {
                    "unit": "pound",
                    "value": 5
                },
                "dimensions": {
                    "unit": "inch",
                    "width": 15,
                    "height": 15,
                    "length": 23
                }
            }
        ]
    }
}'

Results in 200 OK with rate response.

Do you have any other ideas what could be causing this issue?

wentao709 commented 1 year ago

Hey @brandonraphael I stopped seeing the error without any custom config. can you try that please?