Example Integration

Note: This example uses the Json.NET library from https://www.newtonsoft.com/json

using System.Text;
using System.Net;
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;

namespace TestClientConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {

            // Set the web service API address here
            const string URL = "http://localhost:56411/Service/wsPDMApi.svc";
            // Set the API key here
            const string API_KEY = "A54C18709701456E904F3FE78712E73E";
            // Set some values here for test purposes
            const int COMPANY_ID = 9001;
            const int CUSTOMER_ID = 1408;



            // Get the session token and store for ongoing calls to the web service API
            string SessionToken = GetSessionToken(URL, API_KEY, COMPANY_ID, true);

            // GetSessionToken
            string getSessionTokenResponse = GetSessionToken(URL, API_KEY, COMPANY_ID);

            // GetCustomer
            string getCustomerResponse = GetCustomer(URL, SessionToken, COMPANY_ID, CUSTOMER_ID, string.Empty);

            // CreateCustomer
            string createCustomerResponse = CreateCustomer(URL, SessionToken, COMPANY_ID);

            // ModifyCustomer
            string modifyCustomerResponse = ModifyCustomer(URL, SessionToken, COMPANY_ID, CUSTOMER_ID);

            // CreateDelivery
            string createDeliveryResponse = CreateDelivery(URL, SessionToken, COMPANY_ID, CUSTOMER_ID);

            // CreateCollection
            string createCollectionResponse = CreateCollection(URL, SessionToken, COMPANY_ID, CUSTOMER_ID);

        }

        public static string GetSessionToken(string _url, string _apiKey, int _companyId, bool _tokenStringOnly = false)
        {
            // Create the request object
            object request = new
            {
                key = _apiKey,
                company_id = _companyId
            };

            JObject response = WebServiceRequest(_url, "GetSessionToken", request);

            if (_tokenStringOnly)
            {
                JToken session = response.GetValue("session");
                JToken sessionToken = session.SelectToken("session_token");
                return sessionToken.ToString();
            }

            System.Diagnostics.Debug.Print("Response: " + response.ToString());
            return response.ToString();


        }

        public static string GetCustomer(string _url, string _sessionToken, int _companyId, int _customerId, string _externalRef)
        {
            // Create the request object
            object request = new
            {
                company_id = _companyId,
                session_token = _sessionToken,
                customer_id = _customerId,
                external_customer_ref = _externalRef
            };

            JObject response = WebServiceRequest(_url, "GetCustomer", request);
            System.Diagnostics.Debug.Print("Response: " + response.ToString());
            return response.ToString();

        }

        public static string CreateCustomer(string _url, string _sessionToken, int _companyId)
        {
            // Create the request object
            object customer = CreateCustomerObject(CreateAddressObject());
            object request = new
            {
                company_id = _companyId,
                session_token = _sessionToken,
                customer = customer
            };

            JObject response = WebServiceRequest(_url, "CreateCustomer", request);
            System.Diagnostics.Debug.Print("Response: " + response.ToString());
            return response.ToString();

        }

        public static string ModifyCustomer(string _url, string _sessionToken, int _companyId, int _customerId)
        {
            // Create the request object
            object customer = ModifyCustomerObject(_customerId, CreateAddressObject());
            object request = new
            {
                company_id = _companyId,
                session_token = _sessionToken,
                customer = customer
            };

            JObject response = WebServiceRequest(_url, "ModifyCustomer", request);
            System.Diagnostics.Debug.Print("Response: " + response.ToString());
            return response.ToString();

        }

        public static string CreateDelivery(string _url, string _sessionToken, int _companyId, int _customerId)
        {
            List items = new List();

            items.Add(CreateDeliveryCollectionItemObject("123", "Item1", 1, "Medium", "Fragile"));
            items.Add(CreateDeliveryCollectionItemObject("456", "Item2", 5, "Small", ""));


            object delivery = CreateDeliveryObject(_customerId, items);

            object request = new
            {
                company_id = _companyId,
                session_token = _sessionToken,
                delivery = delivery
            };

            JObject response = WebServiceRequest(_url, "CreateDelivery", request);
            System.Diagnostics.Debug.Print("Response: " + response.ToString());
            return response.ToString();
        }

        public static string CreateCollection(string _url, string _sessionToken, int _companyId, int _customerId)
        {
            List items = new List();

            items.Add(CreateDeliveryCollectionItemObject("789", "CollectionItem1", 1, "Medium", "Fragile"));
            items.Add(CreateDeliveryCollectionItemObject("101", "CollectionItem2", 5, "Small", ""));


            object collection = CreateCollectionObject(_customerId, items);

            object request = new
            {
                company_id = _companyId,
                session_token = _sessionToken,
                collection = collection,
                check_collection = false
            };

            JObject response = WebServiceRequest(_url, "CreateCollection", request);
            System.Diagnostics.Debug.Print("Response: " + response.ToString());
            return response.ToString();
        }

        // Function to handle all web service API requests
        // Return object is the type: dynamic
        public static JObject WebServiceRequest(string _url, string _method, object _request)
        {

            // Serialize the request object into JSON format
            string jsonDataObject = JsonConvert.SerializeObject(_request);
            System.Diagnostics.Debug.Print("Request: " + _method + " " + JObject.Parse(jsonDataObject).ToString());
            WebClient webClient = new WebClient();
            string response = string.Empty;

            try
            {
                webClient.Headers["Content-type"] = "application/json";
                webClient.Encoding = Encoding.UTF8;
                response = webClient.UploadString(_url + "/" + _method, "POST", jsonDataObject);

                return JObject.Parse(response);
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static object CreateAddressObject()
        {
            return new
            {
                line_1 = "Test Address Line 1",
                line_2 = "Test Address Line 2",
                line_3 = "",
                city = "Test City",
                county = "Test County",
                postal_code = "TT11 1TT"
            };
        }

        public static object CreateCustomerObject(object _address)
        {
            return new
            {
                external_customer_ref = "EXTREF" + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString(),
                title = "Mr",
                first_name = "Joe",
                last_name = "Bloggs",
                gender = "Male",
                dob = "1968-11-06",
                address = _address,
                email_address = "joe@blogs.com",
                tel_number_1 = "",
                tel_number_2 = "",
                active = 1
            };
        }

        public static object ModifyCustomerObject(int _customer_id, object _address)
        {
            return new
            {
                customer_id = _customer_id,
                external_customer_ref = "EXTREF" + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString(),
                title = "Mr",
                first_name = "Joe",
                last_name = "Bloggs",
                gender = "Male",
                dob = "1968-11-06",
                address = _address,
                email_address = "joe@blogs.com",
                tel_number_1 = "",
                tel_number_2 = "",
                active = 1
            };
        }

        public static object CreateDeliveryCollectionItemObject(string _item_ref, string _description, int _quantity, string _packSize, string _notes)
        {
            return new
            {
                item_ref = _item_ref,
                description = _description,
                quantity = _quantity,
                pack_size = _packSize,
                notes = _notes
            };

        }

        public static object CreateDeliveryObject(int _customerId, List _items)
        {

            return new
            {
                customer_id = _customerId,
                external_customer_ref = "CUSTEXTREF" + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString(),
                external_delivery_ref = "DELEXTREF" + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString(),
                fridge = true,
                controlled_delivery = true,
                boxes = _items.Count,
                items = _items,
                check_delivery = false
            };
        }

        public static object CreateCollectionObject(int _customerId, List _items)
        {

            return new
            {
                customer_id = _customerId,
                external_customer_ref = "CUSTEXTREF" + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString(),
                external_collection_ref = "COLEXTREF" + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString(),
                fridge = true,
                controlled_collection = true,
                boxes = _items.Count,
                items = _items,
                location = "Test Location"
            };
        }
        
    }

}