using Economy;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var test = new TestApi();
test.TestCustomerApi("your@email.com", "yourpassword");
Console.ReadKey();
}
}
class TestApi
{
public async void TestCustomerApi(string username, string password)
{
// Login
var api = new Economy.Api();
if (!await api.Login(username, password))
{
Console.WriteLine(api.LastErrorMessage);
return;
}
// Make sure we have a company-key
await api.GetCompanies();
// Define a customer
var cust = new Customer
{
Info = new BusinessRelation
{
Name = "Uni Micro as",
InvoiceAddress = new Address
{
AddressLine1 = "Øvre Helland",
PostalCode = "5729",
City = "Modalen"
},
DefaultPhone = new Phone
{
Number = "+47 56 59 91 00"
}
}
};
// Check if customer exist, if not we create it
var match = await GetCustomerByName(api, cust.Info.Name);
if (match == null)
{
match = await CreateCustomer(api, cust);
}
Console.WriteLine(match);
}
// Helpers to find and create customer
async Task<Customer> GetCustomerByName(Api api, string name)
{
var list = await api.Get<List<Customer>>($"api/biz/customers?expand=Info,Info.InvoiceAddress,Info.DefaultPhone&filter=Info.Name eq '{name}'");
return list != null && list.Count > 0 ? list[0] : null;
}
async Task<Customer> CreateCustomer(Api api, Customer cust)
{
return await api.Send<Customer>("api/biz/customers", "POST", api.DeParse(cust));
}
// Simple Poco's to represent subset of actual properties
class Customer
{
public int ID { get; set; }
public int CustomerNumber { get; set; }
public BusinessRelation Info { get; set; }
public override string ToString()
{
return $"ID: {ID} / CustomerNumber: {CustomerNumber} - {Info.Name}, {Info.InvoiceAddress?.City}";
}
}
class BusinessRelation
{
public int ID { get; set; }
public string Name { get; set; }
public Address InvoiceAddress { get; set; }
public Phone DefaultPhone { get; set; }
}
class Address
{
public int ID { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string AddressLine3 { get; set; }
public string PostalCode { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string CountryCode { get; set; }
}
class Phone
{
public int ID { get; set; }
public string Number { get; set; }
}
}
}