protected async Task <EurekaHttpResponse <Applications> > DoGetApplicationsAsync(string path, ISet <string> regions)
        {
            string regionParams = CommaDelimit(regions);

            var queryArgs = new Dictionary <string, string>();

            if (regionParams != null)
            {
                queryArgs.Add("regions", regionParams);
            }

            HttpClient client     = GetHttpClient(_config);
            var        requestUri = GetRequestUri(_serviceUrl + path, queryArgs);
            var        request    = GetRequestMessage(HttpMethod.Get, requestUri);



            try
            {
                using (HttpResponseMessage response = await client.SendAsync(request))
                {
                    Stream stream = await response.Content.ReadAsStreamAsync();

                    JsonApplicationsRoot jroot = JsonApplicationsRoot.Deserialize(stream);

                    Applications appsResp = null;
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        if (jroot != null)
                        {
                            appsResp = Applications.FromJsonApplications(jroot.Applications);
                        }
                    }

                    Trace.TraceInformation("DoGetApplicationsAsync {0}, status: {1}, applications: {2}", requestUri.ToString(), (appsResp != null) ? appsResp.ToString() : "null");
                    EurekaHttpResponse <Applications> resp = new EurekaHttpResponse <Applications>(response.StatusCode, appsResp)
                    {
                        Headers = response.Headers
                    };
                    return(resp);
                }
            }
            catch (Exception e)
            {
                Trace.TraceError("DoGetApplicationsAsync Exception: {0}", e);
                throw;
            }
            finally
            {
                DisposeHttpClient(client);
            }
        }
        public async Task <EurekaHttpResponse> StatusUpdateAsync(string appName, string id, InstanceStatus newStatus, InstanceInfo info)
        {
            if (string.IsNullOrEmpty(appName))
            {
                throw new ArgumentException(nameof(appName));
            }

            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentException(nameof(id));
            }

            if (info == null)
            {
                throw new ArgumentNullException(nameof(info));
            }

            var queryArgs = new Dictionary <string, string>()
            {
                { "value", newStatus.ToString() },
                { "lastDirtyTimestamp", DateTimeConversions.ToJavaMillis(new DateTime(info.LastDirtyTimestamp, DateTimeKind.Utc)).ToString() }
            };

            HttpClient client     = GetHttpClient(_config);
            var        requestUri = GetRequestUri(_serviceUrl + "apps/" + Uri.EscapeDataString(appName) + "/" + Uri.EscapeDataString(id) + "/status", queryArgs);
            var        request    = GetRequestMessage(HttpMethod.Put, requestUri);


            try
            {
                using (HttpResponseMessage response = await client.SendAsync(request))
                {
                    Trace.TraceInformation("StatusUpdateAsync {0}, status: {1}", requestUri.ToString(), response.StatusCode);
                    EurekaHttpResponse resp = new EurekaHttpResponse(response.StatusCode)
                    {
                        Headers = response.Headers
                    };
                    return(resp);
                }
            }
            catch (Exception e)
            {
                Trace.TraceError("StatusUpdateAsync Exception: {0}", e);
                throw;
            }
            finally
            {
                DisposeHttpClient(client);
            }
        }
        public async Task <EurekaHttpResponse <Application> > GetApplicationAsync(string appName)
        {
            if (string.IsNullOrEmpty(appName))
            {
                throw new ArgumentException(nameof(appName));
            }

            HttpClient client     = GetHttpClient(_config);
            var        requestUri = GetRequestUri(_serviceUrl + "apps/" + Uri.EscapeDataString(appName));
            var        request    = GetRequestMessage(HttpMethod.Get, requestUri);

            try
            {
                using (HttpResponseMessage response = await client.SendAsync(request))
                {
                    Stream stream = await response.Content.ReadAsStreamAsync();

                    JsonApplicationRoot jroot = JsonApplicationRoot.Deserialize(stream);

                    Application appResp = null;
                    if (jroot != null)
                    {
                        appResp = Application.FromJsonApplication(jroot.Application);
                    }

                    Trace.TraceInformation(
                        "GetApplicationAsync {0}, status: {1}, application: {2}",
                        requestUri.ToString(),
                        response.StatusCode,
                        (appResp != null) ? appResp.ToString() : "null");
                    EurekaHttpResponse <Application> resp = new EurekaHttpResponse <Application>(response.StatusCode, appResp)
                    {
                        Headers = response.Headers
                    };
                    return(resp);
                }
            }
            catch (Exception e)
            {
                Trace.TraceError("GetApplicationAsync Exception: {0}", e);
                throw;
            }
            finally
            {
                DisposeHttpClient(client);
            }
        }
        public async Task <EurekaHttpResponse> CancelAsync(string appName, string id)
        {
            if (string.IsNullOrEmpty(appName))
            {
                throw new ArgumentException(nameof(appName));
            }

            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentException(nameof(id));
            }

            HttpClient client     = GetHttpClient(_config);
            var        requestUri = GetRequestUri(_serviceUrl + "apps/" + Uri.EscapeDataString(appName) + "/" + Uri.EscapeDataString(id));
            var        request    = GetRequestMessage(HttpMethod.Delete, requestUri);

            try
            {
                using (HttpResponseMessage response = await client.SendAsync(request))
                {
                    Trace.TraceInformation("CancelAsync {0}, status: {1}", requestUri.ToString(), response.StatusCode);
                    EurekaHttpResponse resp = new EurekaHttpResponse(response.StatusCode)
                    {
                        Headers = response.Headers
                    };
                    return(resp);
                }
            }
            catch (Exception e)
            {
                Trace.TraceError("CancelAsync Exception: {0}", e);
                throw;
            }
            finally
            {
                DisposeHttpClient(client);
            }
        }
        protected async Task <EurekaHttpResponse <InstanceInfo> > DoGetInstanceAsync(string path)
        {
            var        requestUri = GetRequestUri(_serviceUrl + path);
            var        request    = GetRequestMessage(HttpMethod.Get, requestUri);
            HttpClient client     = GetHttpClient(_config);

            try
            {
                using (HttpResponseMessage response = await client.SendAsync(request))
                {
                    Stream stream = await response.Content.ReadAsStreamAsync();

                    JsonInstanceInfoRoot jroot = JsonInstanceInfoRoot.Deserialize(stream);

                    InstanceInfo infoResp = null;
                    if (jroot != null)
                    {
                        infoResp = InstanceInfo.FromJsonInstance(jroot.Instance);
                    }

                    Trace.TraceInformation("DoGetInstanceAsync {0}, status: {1}, instanceInfo: {2}", requestUri.ToString(), response.StatusCode, (infoResp != null) ? infoResp.ToString() : "null");
                    EurekaHttpResponse <InstanceInfo> resp = new EurekaHttpResponse <InstanceInfo>(response.StatusCode, infoResp)
                    {
                        Headers = response.Headers
                    };
                    return(resp);
                }
            }
            catch (Exception e)
            {
                Trace.TraceError("DoGetInstanceAsync Exception: {0}", e);
                throw;
            }
            finally
            {
                DisposeHttpClient(client);
            }
        }
        public async Task <EurekaHttpResponse <InstanceInfo> > SendHeartBeatAsync(string appName, string id, InstanceInfo info, InstanceStatus overriddenStatus)
        {
            if (info == null)
            {
                throw new ArgumentNullException(nameof(info));
            }

            if (string.IsNullOrEmpty(appName))
            {
                throw new ArgumentException(nameof(appName));
            }

            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentException(nameof(id));
            }

            var queryArgs = new Dictionary <string, string>()
            {
                { "status", info.Status.ToString() },
                { "lastDirtyTimestamp", DateTimeConversions.ToJavaMillis(new DateTime(info.LastDirtyTimestamp, DateTimeKind.Utc)).ToString() }
            };

            if (overriddenStatus != InstanceStatus.UNKNOWN)
            {
                queryArgs.Add("overriddenstatus", overriddenStatus.ToString());
            }

            HttpClient client     = GetHttpClient(_config);
            var        requestUri = GetRequestUri(_serviceUrl + "apps/" + Uri.EscapeDataString(info.AppName) + "/" + Uri.EscapeDataString(id), queryArgs);
            var        request    = GetRequestMessage(HttpMethod.Put, requestUri);


            try
            {
                using (HttpResponseMessage response = await client.SendAsync(request))
                {
                    Stream stream = await response.Content.ReadAsStreamAsync();

                    JsonInstanceInfo jinfo = JsonInstanceInfo.Deserialize(stream);

                    InstanceInfo infoResp = null;
                    if (jinfo != null)
                    {
                        infoResp = InstanceInfo.FromJsonInstance(jinfo);
                    }

                    Trace.TraceInformation(
                        "SendHeartbeatAsync {0}, status: {1}, instanceInfo: {2}",
                        requestUri.ToString(),
                        response.StatusCode,
                        (infoResp != null) ? infoResp.ToString() : "null");
                    EurekaHttpResponse <InstanceInfo> resp = new EurekaHttpResponse <InstanceInfo>(response.StatusCode, infoResp)
                    {
                        Headers = response.Headers
                    };
                    return(resp);
                }
            }
            catch (Exception e)
            {
                Trace.TraceError("SendHeartbeatAsync Exception: {0}", e);
                throw;
            }
            finally
            {
                DisposeHttpClient(client);
            }
        }