From 4acf00d707842437949347f0c31bc826c7a73de1 Mon Sep 17 00:00:00 2001 From: Richard Randak Date: Thu, 7 May 2020 20:42:32 +0200 Subject: [PATCH] Fixed actions --- .../GeneratedTests/InvoiceTests.cs | 34 +++++++++++++ FortnoxAPILibrary/EntityConnector.cs | 49 ++++++++++++++++--- .../Generated/Connectors/InvoiceConnector.cs | 14 +++--- .../Generated/Connectors/OfferConnector.cs | 8 +-- .../Generated/Connectors/OrderConnector.cs | 8 +-- .../Generated/Interfaces/IInvoiceConnector.cs | 6 +-- .../Generated/Interfaces/IOfferConnector.cs | 4 +- .../Generated/Interfaces/IOrderConnector.cs | 4 +- 8 files changed, 99 insertions(+), 28 deletions(-) diff --git a/FortnoxAPILibrary.Tests/GeneratedTests/InvoiceTests.cs b/FortnoxAPILibrary.Tests/GeneratedTests/InvoiceTests.cs index 3feff5ba..6e19349e 100644 --- a/FortnoxAPILibrary.Tests/GeneratedTests/InvoiceTests.cs +++ b/FortnoxAPILibrary.Tests/GeneratedTests/InvoiceTests.cs @@ -187,5 +187,39 @@ public void Invoice_DueDate() new ArticleConnector().Delete(tmpArticle.ArticleNumber); #endregion Delete arranged resources } + + [TestMethod] + public void Invoice_Print() + { + #region Arrange + var tmpCustomer = new CustomerConnector().Create(new Customer() { Name = "TmpCustomer", CountryCode = "SE", City = "Testopolis" }); + var tmpArticle = new ArticleConnector().Create(new Article() { Description = "TmpArticle", Type = ArticleType.STOCK, PurchasePrice = 100 }); + #endregion Arrange + + IInvoiceConnector connector = new InvoiceConnector(); + + var newInvoice = new Invoice() + { + CustomerNumber = tmpCustomer.CustomerNumber, + InvoiceDate = new DateTime(2019, 1, 20), //"2019-01-20", + DueDate = new DateTime(2019, 2, 20), //"2019-02-20", + InvoiceType = InvoiceType.CASHINVOICE, + PaymentWay = PaymentWay.CASH, + Comments = "TestInvoice", + InvoiceRows = new List() + { + new InvoiceRow(){ ArticleNumber = tmpArticle.ArticleNumber, DeliveredQuantity = 10, Price = 100}, + new InvoiceRow(){ ArticleNumber = tmpArticle.ArticleNumber, DeliveredQuantity = 20, Price = 100}, + new InvoiceRow(){ ArticleNumber = tmpArticle.ArticleNumber, DeliveredQuantity = 15, Price = 100} + } + }; + + var createdInvoice = connector.Create(newInvoice); + MyAssert.HasNoError(connector); + + var fileData = connector.Print(createdInvoice.DocumentNumber); + MyAssert.HasNoError(connector); + Assert.IsTrue(fileData.Length > 10000); + } } } diff --git a/FortnoxAPILibrary/EntityConnector.cs b/FortnoxAPILibrary/EntityConnector.cs index b83a66c6..aec4f6cd 100644 --- a/FortnoxAPILibrary/EntityConnector.cs +++ b/FortnoxAPILibrary/EntityConnector.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Threading.Tasks; using System.Web; @@ -181,8 +182,36 @@ protected TEntity DoAction(string documentNumber, Action action) return DoActionAsync(documentNumber, action).Result; } + protected byte[] DoDownloadAction(string documentNumber, Action action, string localPath = null) + { + return DoDownloadActionAsync(documentNumber, action, localPath).Result; + } + + protected async Task DoDownloadActionAsync(string documentNumber, Action action, string localPath = null) + { + if (!IsDownloadAction(action)) + throw new Exception("Invalid action type"); + + RequestInfo = new RequestInfo() + { + BaseUrl = BaseUrl, + Resource = Resource, + Indices = new[] { documentNumber, action.GetStringValue() }, + Method = RequestMethod.Get, + ResponseType = RequestResponseType.PDF + }; + + var data = await DoSimpleRequest(); + if (localPath != null) + File.WriteAllBytes(localPath, data); + return data; + } + protected async Task DoActionAsync(string documentNumber, Action action) { + if (IsDownloadAction(action)) + throw new Exception("Invalid action type"); + RequestInfo = new RequestInfo() { BaseUrl = BaseUrl, @@ -192,12 +221,6 @@ protected async Task DoActionAsync(string documentNumber, Action action switch (action) { - case Action.Print: - case Action.Preview: - case Action.EPrint: - RequestInfo.Method = RequestMethod.Get; - RequestInfo.ResponseType = RequestResponseType.PDF; - break; case Action.ExternalPrint: RequestInfo.Method = RequestMethod.Put; RequestInfo.ResponseType = RequestResponseType.JSON; @@ -214,6 +237,20 @@ protected async Task DoActionAsync(string documentNumber, Action action var result = await DoEntityRequest>(); return result?.Entity; } + + private static bool IsDownloadAction(Action action) + { + switch (action) + { + case Action.Print: + case Action.PrintReminder: + case Action.Preview: + case Action.EPrint: + return true; + default: + return false; + } + } } public enum Action diff --git a/FortnoxAPILibrary/Generated/Connectors/InvoiceConnector.cs b/FortnoxAPILibrary/Generated/Connectors/InvoiceConnector.cs index 0208492d..a7174078 100644 --- a/FortnoxAPILibrary/Generated/Connectors/InvoiceConnector.cs +++ b/FortnoxAPILibrary/Generated/Connectors/InvoiceConnector.cs @@ -211,11 +211,11 @@ public Invoice EInvoice(int? id) /// /// This action returns a PDF document with the current template that is used by the specific document. Note that this action also sets the property Sent as true. /// - /// + /// /// - public Invoice Print(int? id) + public byte[] Print(int? id) { - return DoAction(id.ToString(), Action.Print); + return DoDownloadAction(id.ToString(), Action.Print); } /// @@ -223,9 +223,9 @@ public Invoice Print(int? id) /// /// /// - public Invoice PrintReminder(int? id) + public byte[] PrintReminder(int? id) { - return DoAction(id.ToString(), Action.PrintReminder); + return DoDownloadAction(id.ToString(), Action.PrintReminder); } /// @@ -243,9 +243,9 @@ public Invoice ExternalPrint(int? id) /// /// /// - public Invoice Preview(int? id) + public byte[] Preview(int? id) { - return DoAction(id.ToString(), Action.Preview); + return DoDownloadAction(id.ToString(), Action.Preview); } public async Task> FindAsync() diff --git a/FortnoxAPILibrary/Generated/Connectors/OfferConnector.cs b/FortnoxAPILibrary/Generated/Connectors/OfferConnector.cs index e1698cea..73d31385 100644 --- a/FortnoxAPILibrary/Generated/Connectors/OfferConnector.cs +++ b/FortnoxAPILibrary/Generated/Connectors/OfferConnector.cs @@ -145,9 +145,9 @@ public Offer Email(int? id) /// /// /// - public Offer Print(int? id) + public byte[] Print(int? id) { - return DoAction(id.ToString(), Action.Print); + return DoDownloadAction(id.ToString(), Action.Print); } /// @@ -165,9 +165,9 @@ public Offer ExternalPrint(int? id) /// /// /// - public Offer Preview(int? id) + public byte[] Preview(int? id) { - return DoAction(id.ToString(), Action.Preview); + return DoDownloadAction(id.ToString(), Action.Preview); } public async Task> FindAsync() diff --git a/FortnoxAPILibrary/Generated/Connectors/OrderConnector.cs b/FortnoxAPILibrary/Generated/Connectors/OrderConnector.cs index acc46f9e..a5af9f24 100644 --- a/FortnoxAPILibrary/Generated/Connectors/OrderConnector.cs +++ b/FortnoxAPILibrary/Generated/Connectors/OrderConnector.cs @@ -163,9 +163,9 @@ public Order Email(int? id) /// /// /// - public Order Print(int? id) + public byte[] Print(int? id) { - return DoAction(id.ToString(), Action.Print); + return DoDownloadAction(id.ToString(), Action.Print); } /// @@ -183,9 +183,9 @@ public Order ExternalPrint(int? id) /// /// /// - public Order Preview(int? id) + public byte[] Preview(int? id) { - return DoAction(id.ToString(), Action.Preview); + return DoDownloadAction(id.ToString(), Action.Preview); } public async Task> FindAsync() diff --git a/FortnoxAPILibrary/Generated/Interfaces/IInvoiceConnector.cs b/FortnoxAPILibrary/Generated/Interfaces/IInvoiceConnector.cs index 0b73df6b..514375d8 100644 --- a/FortnoxAPILibrary/Generated/Interfaces/IInvoiceConnector.cs +++ b/FortnoxAPILibrary/Generated/Interfaces/IInvoiceConnector.cs @@ -38,10 +38,10 @@ public interface IInvoiceConnector : IConnector Invoice CreditInvoice(int? id); Invoice Email(int? id); Invoice EInvoice(int? id); - Invoice Print(int? id); - Invoice PrintReminder(int? id); + byte[] Print(int? id); + byte[] PrintReminder(int? id); Invoice ExternalPrint(int? id); - Invoice Preview(int? id); + byte[] Preview(int? id); Task UpdateAsync(Invoice invoice); Task CreateAsync(Invoice invoice); diff --git a/FortnoxAPILibrary/Generated/Interfaces/IOfferConnector.cs b/FortnoxAPILibrary/Generated/Interfaces/IOfferConnector.cs index e4265812..80775e29 100644 --- a/FortnoxAPILibrary/Generated/Interfaces/IOfferConnector.cs +++ b/FortnoxAPILibrary/Generated/Interfaces/IOfferConnector.cs @@ -28,9 +28,9 @@ public interface IOfferConnector : IConnector Offer CreateOrder(int? id); Offer Cancel(int? id); Offer Email(int? id); - Offer Print(int? id); + byte[] Print(int? id); Offer ExternalPrint(int? id); - Offer Preview(int? id); + byte[] Preview(int? id); Task UpdateAsync(Offer offer); Task CreateAsync(Offer offer); diff --git a/FortnoxAPILibrary/Generated/Interfaces/IOrderConnector.cs b/FortnoxAPILibrary/Generated/Interfaces/IOrderConnector.cs index 551b3015..85c5f06a 100644 --- a/FortnoxAPILibrary/Generated/Interfaces/IOrderConnector.cs +++ b/FortnoxAPILibrary/Generated/Interfaces/IOrderConnector.cs @@ -31,9 +31,9 @@ public interface IOrderConnector : IConnector Order CreateInvoice(int? id); Order Cancel(int? id); Order Email(int? id); - Order Print(int? id); + byte[] Print(int? id); Order ExternalPrint(int? id); - Order Preview(int? id); + byte[] Preview(int? id); Task UpdateAsync(Order order); Task CreateAsync(Order order);