/// <summary>
        /// Initializes a new instance of the <see cref="CustomerApiException"/> class.
        /// </summary>
        /// <param name="errorCode">HTTP status code.</param>
        /// <param name="message">Error message.</param>
        /// <param name="errorContent">Error content.</param>
        /// <param name="headers">HTTP headers.</param>
        /// <param name="cookies">HTTP cookies.</param>
        public CustomApiException(int errorCode, string message, dynamic errorContent, Multimap <string, string> headers, List <Cookie> cookies) : base(errorCode, message, (object)errorContent)
        {
            Headers = headers;
            Cookies = cookies;
            Limits  = new XeroLimits(Headers);
            switch (errorCode)
            {
            case 401:
                TokenError = true;
                break;

            case 429:
                LimitReached = true;
                break;
            }
        }
        public void Run(string name)
        {
            try
            {
                Console.WriteLine(name + ": Starting ");
                var clientId = _config["ClientId"];

                XeroClient client = null;
                IXeroToken token  = null;
                lock (_lockObj)
                {
                    client = new XeroClient(new XeroConfiguration {
                        ClientId = clientId
                    });

                    if (System.IO.File.Exists("Token.json"))
                    {
                        var savedJson = System.IO.File.ReadAllText("Token.json");
                        token = JsonConvert.DeserializeObject <XeroOAuth2Token>(savedJson);
                    }

                    if (token == null)
                    {
                        token = new XeroOAuth2Token {
                            RefreshToken = _config["RefreshToken"]
                        }
                    }
                    ;

                    IXeroToken newToken = Task.Run(() => client.RefreshAccessTokenAsync(token)).GetAwaiter().GetResult();
                    if (newToken != null)
                    {
                        token = newToken;
                    }

                    var json = JsonConvert.SerializeObject(token, Formatting.Indented);
                    System.IO.File.WriteAllText("Token.json", json);
                }

                Console.WriteLine(name + ": Token refreshed");
                var accessToken = token.AccessToken;

                var tenant = "";
                try
                {
                    var conRes = Task.Run(() => client.GetConnectionsAsync(token)).GetAwaiter().GetResult();
                    tenant = conRes[0].TenantId.ToString();
                    Console.WriteLine(name + ": Tenants");
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    SerializeException(e);
                    return;
                }

                var api = new AccountingApi {
                    ExceptionFactory = CustomExceptionFactory
                };

                Console.WriteLine(name + ": New token process");

                var response     = Task.Run(() => api.GetInvoicesAsyncWithHttpInfo(accessToken, tenant)).GetAwaiter().GetResult();
                var respInvoices = Task.Run(() => api.GetInvoicesAsyncWithHttpInfo(accessToken, tenant, page: 1, where : "")).GetAwaiter().GetResult();
                var invDate      = respInvoices.Data._Invoices[0].Date;

                var recurring = Task.Run(() => api.GetRepeatingInvoicesAsyncWithHttpInfo(accessToken, tenant)).GetAwaiter().GetResult();
                var status    = recurring.Data._RepeatingInvoices[0].Status;
                var schedule  = recurring.Data._RepeatingInvoices[0].Schedule;

                Console.WriteLine(name + ": Complete");
            }
            catch (CustomApiException ce)
            {
                Console.WriteLine(name + ": Failed: " + ce.Message);
                SerializeException(ce);
                Console.WriteLine(ce);
                var limits = new XeroLimits(ce.Headers);
            }
            catch (Exception e)
            {
                Console.WriteLine(name + ": Failed: " + e.Message);
                SerializeException(e);
            }
        }