softwerkab / fortnox-csharp-api-sdk

.NET SDK for Fortnox API.
MIT License
52 stars 64 forks source link

new way of creating a order in 4.3 #227

Closed MagnusModig closed 2 years ago

MagnusModig commented 2 years ago

Hi

I version 4 you created a order like this..

var orderConnector = _Client.Get<OrderConnector>();
var newOrder = new Fortnox.SDK.Entities.Order();
var generatedOrder = new Fortnox.SDK.Entities.Order();
newOrder.OrderRows = new List<Fortnox.SDK.Entities.OrderRow>();
newOrder.OrderRows.Add(orderRows);
generatedOrder = orderConnector.Create(newOrder);
var generatedDocumentId = Convert.ToInt32(generatedOrder.DocumentNumber);

And initiated a new Client like this...

        _Client = new FortnoxClient()
        {
            //Set credentials
            AccessToken = FortnoxAccessToken,
            ClientSecret = FortnoxClientsecret,
            //Set custom http client or other settings
            HttpClient = new HttpClient()
            {
                Timeout = new TimeSpan(0, 0, 0, 10)
            }
        };

Can you show the equivalent for version 4.3?

richardrandak commented 2 years ago

The difference is just slightly how to inject credentials to the client and how to get the connector. You need to wrap the credentials into a StaticTokenAuthorization object. To get the connector, you call directly a property OrderConnector instead of function Get. The final, most relevant change is that all the request are async now.

..so, equivalent to your code would be this.

        var authorization = new StaticTokenAuth(FortnoxAccessToken, FortnoxClientsecret);
        var httpClient = new HttpClient() { Timeout = new TimeSpan(0, 0, 0, 10) };
        var client = new FortnoxClient(authorization, httpClient);

        var orderConnector = client.OrderConnector;
        var newOrder = new Fortnox.SDK.Entities.Order();
        newOrder.OrderRows = new List<Fortnox.SDK.Entities.OrderRow>();
        newOrder.OrderRows.Add(orderRows);
        var generatedOrder = await orderConnector.CreateAsync(newOrder);
        var generatedDocumentId = Convert.ToInt32(generatedOrder.DocumentNumber);

If you for some reason can't use await functionality, you can do this.

var generatedOrder = orderConnector.CreateAsync(newOrder).ConfigureAwait(false).GetAwaiter().GetResult();

This is what the old SDK version was doing behind the scene before, when you call the "synchronized" request. I don't recommend it, however.

MagnusModig commented 2 years ago

Thanks, but the newOrder.OrderRows.Add(orderRows); isnt working, it add one Orderrow not a list of orderrows, do I then nee to iterate through the collection and add each of them like this... foreach (var oRow in orderRows) { newOrder.OrderRows.Add(oRow); }

richardrandak commented 2 years ago

just use newOrder.OrderRows.AddRange(orderRows); ..Oh, I see.. wait a second,

richardrandak commented 2 years ago

So, I see I change the List to IList which does not have the AddRange method. You can create an extension to have it available, to not write the loop every time.

internal static class EnumerableHelpers
{
    public static void AddRange<T>(this IList<T> collection, IEnumerable<T> items)
    {
        foreach (var item in items)
        {
            collection.Add(item);
        }
    }
}

But, in your specific case, however, you can just do this.

newOrder.OrderRows = orderRows.ToList();

Naturally, if there are already some rows in the order, then this would override them. Since you just created an order from scratch, it dos not have any items.

MagnusModig commented 2 years ago

I get an absolete indication on this row.. var authorization = new StaticTokenAuth(FortnoxAccessToken, FortnoxClientsecret);

telling me to use StandardAuth instead. Will that one stop working?

CyrilPorseland commented 2 years ago

I really hope not. We have hundreds of customers who use the old static auth. Only new customers use the standard auth. Fortnox told us that the old static auth and lifetime of the Authorization-Key is 10 years.

Richard, Please don´t remove theese absolete functions. Information that they are absolete is okey.

richardrandak commented 2 years ago

I don't plan to remove them, I am using them as well, so don't worry :) The warning needs to be there for new developers, so that they won't accidentally use the old auth instead of the new one.

@MagnusModig - if you did not notice, Fortnox now has two authentication workflows. Both of them are supported by SDK and both of them are okay to use. You just have to think which is required in your case. If you're just adapting the code to the new SDK version, use the old workflow, since that's the one it was using before.

MagnusModig commented 2 years ago

Good news, thanks for the help and nice work with version 4.3 :)