janssenr / SendCloudApi.Net

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

Integrations.GetShipments() errors #31

Open jpp-skynet opened 3 months ago

jpp-skynet commented 3 months ago

Sorry to bother you again ;(

Shipment[] pTest= await SendcloudClient.Integrations.GetShipments(366789, orderNumber: "405-0794075-2815560"); pTest= await SendcloudClient.Integrations.GetShipments(366789, shippingRules: true);

First line works, but second doesn't. Using other filters like startDate also return the same error: System.Runtime.Serialization.SerializationException

"Expected 'Element'.. 'Text' was found with name '',namespace ''. " (translated, it comes in my pc language)

Maybe they changed the returned element?

janssenr commented 3 months ago

I cannot replicate the error. When I use shippingRules: true, i do not get an error. Which integration do you use? Or is it possible you sent me your credentials for SendCloud?

jpp-skynet commented 3 months ago

I'm doing more tests, it seems to be related with the amount of shipments involved

pTest= await Global.SendcloudClient.Integrations.GetShipments(366789, shippingRules: true, startDate: DateTime.Today.AddDays(0)); //39 elem ok pTest= await Global.SendcloudClient.Integrations.GetShipments(366789, shippingRules: true, startDate: DateTime.Today.AddDays(-1)); //Exception

What XML library are you using? Could you try invoking GetShipments with +100 shipments returned?

Btw I'm using verbose: true when creating new SendCloudApi.Net.V2.SendCloudApi(...)

jpp-skynet commented 3 months ago
"next": "https://panel.sendcloud.sc/api/v2/integrations/366789/shipments?cursor=cD0yMDI0LTA3LTE1KzE5JTNBNTIlM0EzMyUyQjAwJTNBMDA%3D&start_date=2024-07-13",
"previous": null,

It returns 50 per page, is the cursor thing working in this project v2?

janssenr commented 3 months ago

I only have the API integration. So I cannot test this with my own account.

jpp-skynet commented 3 months ago

I don't understand, next/previous is part of the API endpoint

https://api.sendcloud.dev/docs/sendcloud-public-api/integrations/operations/list-integration-shipments

I wrote my own API call and it's working:

    public static async Task<Shipment[]> GetShipsSendCloudAsyncFixed(int mpId, DateTime startDate)
    {
        List<Shipment> res = null;

        using (var client = new System.Net.Http.HttpClient())
        {
            string token = $"Basic {Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(SendCloudApiPublic + ":" + SendCloudApiPrivate))}";
            client.DefaultRequestHeaders.Add("Authorization", token);

            string since = startDate.ToString("yyyy-MM-dd");
            string url = "https://panel.sendcloud.sc/api/v2/integrations/" + mpId + "/shipments?start_date=" + since + "&shipping_rules=true";

            //50 items by page
            while (url != null)
            {
                var resp = await client.GetAsync(url);

                if (resp.IsSuccessStatusCode)
                {
                    var jsonResult = await resp.Content.ReadAsStringAsync().ConfigureAwait(false);

                    if (res == null)
                        res = new List<Shipment>();
                    var model = JsonConvert.DeserializeObject<Wrapper>(jsonResult);
                    res.AddRange(model.results);
                    url = model.next;
                }
            }

            return res.ToArray();
        }
    }
    public class Wrapper
    {
        public string next { get; set; }
        public string previous { get; set; }
        public SendCloudApi.Net.V2.Models.Shipment[] results { get; set; }
    }