Umgeschrieben auf Sevdesk
This commit is contained in:
101
ConsoleApp3/SevdeskService.cs
Normal file
101
ConsoleApp3/SevdeskService.cs
Normal file
@@ -0,0 +1,101 @@
|
||||
using System.Runtime.Serialization;
|
||||
using System.Text;
|
||||
using ConsoleApp3.DataContracts;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
|
||||
namespace CardmarketBot
|
||||
{
|
||||
class SevdeskService
|
||||
{
|
||||
private readonly HttpClient _client;
|
||||
private readonly Random _random = new();
|
||||
|
||||
public SevdeskService(string key)
|
||||
{
|
||||
_client = new HttpClient
|
||||
{
|
||||
DefaultRequestHeaders =
|
||||
{
|
||||
{ "Authorization", "7251554968610b78ca865b2b774b4134" },
|
||||
{ "Accept", "application/json" }
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
public async void Create(ModelRechnung rechnung)
|
||||
{
|
||||
|
||||
rechnung.Invoice.InvoiceNumber = string.Format("RE-{0}", await GetNextInvoiceNumber());
|
||||
await WriteInv(rechnung);
|
||||
}
|
||||
|
||||
|
||||
private async Task<bool> WriteInv(ModelRechnung invoice)
|
||||
{
|
||||
var uri = "https://my.sevdesk.de/api/v1/Invoice/Factory/saveInvoice";
|
||||
|
||||
var t = JsonConvert.SerializeObject(invoice);
|
||||
var content = new StringContent(JsonConvert.SerializeObject(invoice),Encoding.UTF8, "application/json");
|
||||
|
||||
var resp = await _client.PostAsync(uri, content);
|
||||
//Console.WriteLine(resp.EnsureSuccessStatusCode());
|
||||
var contents = await resp.Content.ReadAsStringAsync();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private async Task<Invoices> GetInvoiceAsync(string id)
|
||||
{
|
||||
//var uri = LexofficeApiAddressesBuilder.InvoiceUri(id);
|
||||
var uri = "https://my.sevdesk.de/api/v1/Invoice/56501411/getPositions";
|
||||
var response = await _client.GetAsync(uri).ConfigureAwait(false);
|
||||
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var contents = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
|
||||
|
||||
var jsonSerializerSettings = new JsonSerializerSettings
|
||||
{
|
||||
MissingMemberHandling = MissingMemberHandling.Ignore
|
||||
};
|
||||
return JsonConvert.DeserializeObject<Invoices>(contents, jsonSerializerSettings);
|
||||
}
|
||||
|
||||
private async Task<string> GetNextInvoiceNumber()
|
||||
{
|
||||
var uri = "https://my.sevdesk.de/api/v1/SevSequence/Factory/getByType?objectType=Invoice&type=RE";
|
||||
var response = await _client.GetAsync(uri).ConfigureAwait(false);
|
||||
response.EnsureSuccessStatusCode();
|
||||
var contents = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
|
||||
|
||||
var jsonSerializerSettings = new JsonSerializerSettings
|
||||
{
|
||||
MissingMemberHandling = MissingMemberHandling.Ignore
|
||||
};
|
||||
var ob = JsonConvert.DeserializeObject<RNumbers>(contents, jsonSerializerSettings);
|
||||
|
||||
return ob.ConfigRechnungsnummer.NextNumber;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
[DataContract]
|
||||
class RNumbers
|
||||
{
|
||||
[JsonProperty("objects")] public ConfigRechnungnummer ConfigRechnungsnummer { get; set; }
|
||||
}
|
||||
|
||||
[DataContract]
|
||||
class ConfigRechnungnummer
|
||||
{
|
||||
[DataMember(Name = "format", EmitDefaultValue = false)]
|
||||
[JsonProperty(PropertyName = "format")]
|
||||
public string Format { get; set; } = "";
|
||||
|
||||
[DataMember(Name = "nextSequence", EmitDefaultValue = false)]
|
||||
[JsonProperty(PropertyName = "nextSequence")]
|
||||
public string NextNumber { get; set; } = "";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user