janssenr / SendCloudApi.Net

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

Using verbose on Create parcel #22

Closed davidkdb closed 2 years ago

davidkdb commented 2 years ago

Hi,

We would like that you implement the use of verbose in Create parcel. This gives more detailed information from the carrier when it fails. So instead of getting "Announcement failed" exception that means nothing to the user, it will give more details like: non_field_errors: "Service error: Die angegebene Straße kann nicht gefunden werden."

This example message comes from a shipment with DHL Germany.

From the Sendcloud API documentation:

Developers If you are using this API for development purposes, you might want to use the errors query string in the URL. That allows you to visualize the errors from the carrier.

GET https://panel.sendcloud.sc/api/v2/parcels?errors=verbose-carrier

We have tested it, and the code below works.

public abstract class SendCloudApiAbstractResource { protected readonly SendCloudApi Client; protected string HostUrl = "https://panel.sendcloud.sc/api/v2/"; protected string Authorization; protected bool CreateRequest = true; protected bool GetRequest = true; protected bool UpdateRequest = true; protected bool DeleteRequest = false; protected string SingleResource = string.Empty; protected string ListResource = string.Empty; protected string CreateResource = string.Empty; protected string UpdateResource = string.Empty; protected string Resource = string.Empty; protected string DateTimeFormat = "yyyy-MM-dd HH:mm:ss"; protected string Verbose = "?errors=verbose-carrier";

    protected SendCloudApiAbstractResource(SendCloudApi client)
    {
        Client = client;
        Authorization = client.GetBasicAuth();
    }

    protected async Task<ApiResponse<T>> Create<T>(string data)
    {
        if (CreateRequest)
        {
            return await Client.Create<T>($"{HostUrl}{CreateResource}{Verbose}", Authorization, data, SingleResource, DateTimeFormat);
        }
        return new ApiResponse<T>(System.Net.HttpStatusCode.MethodNotAllowed, default(T));
    }
janssenr commented 2 years ago

Verbose mode for developers added