janssenr / SendCloudApi.Net

A C#/.net wrapper for the SendCloud API
MIT License
6 stars 5 forks source link

Error in GetShipment #18

Closed davidkdb closed 2 years ago

davidkdb commented 2 years ago

Hi,

When there is an error in missing country of origian or hs code the returned error message triggers an exception instead of just populating the error property.

We use this to get the shipment, and then we can populate parcel items with correct values.

BTW. You made a GetShipmentsWithHttpInfo call to see the payload, but if json serialization fails, it will also trigger an exception. This function should be able to return the payload even if json error.

This happens on export from EU, but the error issue will be the same for other things.

But now this triggers an exception, and we cannot get the shipment object:

{ "next": null, "previous": null, "results": [ { "address": "43", "address_2": "", "allowed_shipping_methods": [], "barcode": "PSxxxx", "city": "XX", "company_name": "", "country": "CH", "created_at": "2022-05-12T17:34:09.391873Z", "currency": "EUR", "customs_invoice_nr": "XX", "customs_shipment_type": 2, "email": "ys@gmail.com", "external_order_id": "000001", "external_shipment_id": "", "house_number": "", "integration": 323323, "name": "My name", "order_number": "1234", "order_status": { "id": "unfulfilled", "message": "Unfulfilled" }, "parcel_items": [ { "description": "Product 1", "hs_code": "", "origin_country": null, "product_id": "6721039466649", "properties": {}, "quantity": 1, "sku": "sku1", "value": "240.00", "weight": "1.600" } ], "payment_status": { "id": "paid", "message": "Paid" }, "postal_code": "0001", "sender_address": 1234, "shipment_uuid": "xxxxx", "shipping_method": 2255, "shipping_method_checkout_name": "DHL Express : home delivery", "telephone": "911", "to_post_number": "", "to_service_point": null, "to_state": null, "total_order_value": "10.00", "updated_at": "2022-05-12T17:34:09.391879Z", "weight": "1.600", "width": null, "height": null, "length": null, "errors": { "parcel_items": [ { "hs_code": [ "This field is required." ], "origin_country": [ "This field is required." ] } ] } } ] }

janssenr commented 2 years ago

Do you need the Errors property? Because I can just not Deserialize it. Or I can change it to:

    [DataMember(Name = "errors", EmitDefaultValue = false, IsRequired = false)]
    public Dictionary<string, List<object>> Errors { get; set; }
davidkdb commented 2 years ago

Then just remove it so it dont create an exception.

We can read the GetShipmentsWithHttpInfo if we need to debug the payload.

Can you add the shipping_rules parameter like this?

public async Task<Shipment[]> GetShipments(int integrationId, int[] externalOrderIds = null, int[]externalShipmentIds = null, string orderNumber = null, DateTime? startDate = null, DateTime? endDate = null, int? senderAddressId = null, bool? shipping_rules = null) { var apiResponse = await GetShipmentsWithHttpInfo(integrationId, externalOrderIds, externalShipmentIds, orderNumber, startDate, endDate, senderAddressId, shipping_rules:shipping_rules); return apiResponse.Data; }

    public async Task<ApiResponse<Shipment[]>> GetShipmentsWithHttpInfo(int integrationId, int[] externalOrderIds = null, int[] externalShipmentIds = null, string orderNumber = null, DateTime? startDate = null, DateTime? endDate = null, int? senderAddressId = null, bool? shipping_rules = null)
    {
        var parameters = new Dictionary<string, string>();
        if (externalOrderIds != null && externalOrderIds.Length > 0)
            parameters.Add("external_order_ids", string.Join(",", externalOrderIds));
        if (externalShipmentIds != null && externalShipmentIds.Length > 0)
            parameters.Add("external_shipment_ids", string.Join(",", externalShipmentIds));
        if (!string.IsNullOrWhiteSpace(orderNumber))
            parameters.Add("order_number", orderNumber);
        if (startDate.HasValue)
            parameters.Add("start_date", startDate.Value.ToString("yyyy-MM-dd"));
        if (endDate.HasValue)
            parameters.Add("end_date", endDate.Value.ToString("yyyy-MM-dd"));
        if (senderAddressId.HasValue)
            parameters.Add("sender_address ", senderAddressId.Value.ToString());
        if (shipping_rules.HasValue)
            parameters.Add("shipping_rules ", shipping_rules.Value.ToString().ToLowerInvariant());

        string url = $"{HostUrl}integrations/{integrationId}/shipments";
        var apiResponse = await Client.Get<Shipment[]>(url, Authorization, parameters, "results", "yyyy-MM-ddTHH:mm:ss.FFFFFFZ");
        return apiResponse;
    }
janssenr commented 2 years ago

Removed the Shipment.Errors property and added ShippingRules parameter for GetShipments