Closed MagnusModig closed 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
..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.
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); }
just use newOrder.OrderRows.AddRange(orderRows);
..Oh, I see.. wait a second,
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.
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?
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.
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.
Good news, thanks for the help and nice work with version 4.3 :)
Hi
I version 4 you created a order like this..
And initiated a new Client like this...
Can you show the equivalent for version 4.3?