/// <summary> /// Skapa ett billogram /// </summary> /// <param name="billogram"></param> /// <returns>Uri</returns> static async Task <BillogramResponse> CreateBillogramAsync(BillogramCreate billogram) { BillogramResponse billogramResp = null; var jsonStr = JsonSerializer.Serialize(billogram); Console.WriteLine(jsonStr); Uri respUri = null; try { // skicka på ett Billogram HttpResponseMessage response = await client.PostAsJsonAsync( "api/v2/billogram", billogram); //response.EnsureSuccessStatusCode(); if (response.IsSuccessStatusCode) { respUri = response.Headers.Location; var cont = await response.Content.ReadAsStringAsync(); Console.WriteLine(cont); billogramResp = await response.Content.ReadAsAsync <BillogramResponse>(); } else { Console.WriteLine(jsonStr); Console.WriteLine($"ERROR: StatusCode: {response.StatusCode} ResonPhrese: {response.ReasonPhrase}"); Console.WriteLine(JsonSerializer.Serialize(response)); } } catch (Exception e) { string msg = e.Message; Console.WriteLine($"ERROR: Msg: {msg}"); throw; } // return URI of the created resource. return(billogramResp); }
static async Task RunAsync() { string username = "******"; string password = "******"; string authKey = Convert.ToBase64String(Encoding.Default.GetBytes(username + ":" + password)); // Update port # in the following line. //client.BaseAddress = new Uri("http://*****:*****@test.com", "Goran Engstrom"); //var respCustomer = await CreateCustomerAsync(nyCustomer); //Console.WriteLine(respCustomer); //Get Billogram BillogramResponse billogramResp = new BillogramResponse(); myurl = "api/v2/billogram/"; billogramResp = await GetBillogramAsync(myurl + "wdu4gsU"); ShowBillogram(billogramResp); Console.WriteLine($"GetBillogram is done!!"); //POST Billogram BillogramCreate billogramCreate = new BillogramCreate(); //billogram.Invoice_date = DateTime.Now; //billogram.Due_date = DateTime.Now.AddDays(30); CustomerMini smallCustomer = new CustomerMini(); smallCustomer.customer_no = 12370; billogramCreate.customer = smallCustomer; // Skapa ett item ItemMini item = new ItemMini(); item.item_no = "4"; // Deltagaravg Rätt focus SME item.title = "Produkt försäljning"; item.count = 1; item.vat = 20; item.price = 125; ItemMini item2 = new ItemMini(); item2.title = "Detta kan vara en textrad"; ItemMini item3 = new ItemMini(); item3.title = "Administattionskostnad"; item3.count = 1; item3.price = 45; item3.vat = 12; ItemMini[] items = new ItemMini[3] { item, item2, item3 }; billogramCreate.items = items; //Skapa ett Info Info info = new Info(); info.message = "Skapat via ConsoleAPI_Test"; billogramCreate.info = info; On_success on_Success = new On_success(); on_Success.command = "send"; on_Success.method = "Email"; billogramCreate.on_success = on_Success; // Create Billogram billogramResp = await CreateBillogramAsync(billogramCreate); if (billogramResp != null) { ShowBillogram(billogramResp); } //----------------------------------------------- //// Create a new product //Product product = new Product //{ // Name = "Gizmo", // Price = 100, // Category = "Widgets" //}; //var url = await CreateProductAsync(product); //Console.WriteLine($"Created at {url}"); //// Get the product //product = await GetProductAsync(url.PathAndQuery); //ShowProduct(product); //// Update the product //Console.WriteLine("Updating price..."); //product.Price = 80; //await UpdateProductAsync(product); //// Get the updated product //product = await GetProductAsync(url.PathAndQuery); //ShowProduct(product); //// Delete the product //var statusCode = await DeleteProductAsync(product.Id); //Console.WriteLine($"Deleted (HTTP Status = {(int)statusCode})"); } catch (Exception e) { Console.WriteLine(e.Message); } Console.ReadLine(); }