コード例 #1
0
        /// <summary>
        /// Tests the connection.
        /// <exception cref="ESSessionExpceion">connect faild</exception>
        /// </summary>
        public async Task <Result> TestCredential()
        {
            string url = this.GetFullUrl("rest/openapi/sm/session");

            try
            {
                string payload = string.Format("{{ \"userid\":\"{0}\", \"value\":\"{1}\" }}",
                                               this.Appliance.LoginAccount, this.Appliance.LoginPassword);

                var content = new StringContent(payload, Encoding.UTF8, "application/json");
                HttpResponseMessage response = new HttpClient().PutAsync(url, content).Result;
                string responseContent       = await response.Content.ReadAsStringAsync();

                if (!response.IsSuccessStatusCode)
                {
                    return(Result.Failed(Convert.ToInt32(response.StatusCode), $"Failed to communicate with {Appliance.Host} -> {response.ReasonPhrase}."));
                }

                ESightAPIResult result = ReadAsJsonDataContract <ESightAPIResult>(responseContent);
                if (result.code == 0)
                {
                    this.OpenId = result.data;
                    return(Result.Done("eSight authentication passed", null));
                }
                else
                {
                    return(Result.Failed(result.code, $"Failed to communicate with {Appliance.Host} -> {result.description}."));
                }
            }
            catch (SerializationException ex)
            {
                return(Result.Failed(100, $"Failed to connect communicate with {Appliance.Host} -> {ex.Message}", ex));
            }
            catch (AggregateException e)
            {
                List <string> reasons = GetExceptionReasons(e);
                string        cause   = String.Join(", ", reasons);
                return(Result.Failed(100, $"Failed to connect to {Appliance.Host} -> {cause}", e));
            }
        }
コード例 #2
0
        /// <summary>
        /// Logouts this instance.
        /// </summary>
        /// <exception cref="Exception">Exception</exception>
        /// <exception cref="ESSessionExpceion">HW_LOGIN_AUTH</exception>
        public async Task <Result> Logout()
        {
            if (string.IsNullOrEmpty(this.OpenId))
            {
                return(Result.Done());
            }

            string url = this.GetFullUrl($"rest/openapi/sm/session?openid={this.OpenId}");

            try
            {
                var httpClient = new HttpClient();
                httpClient.DefaultRequestHeaders.Add("openid", this.OpenId);
                var    response        = httpClient.DeleteAsync(url).Result;
                string responseContent = await response.Content.ReadAsStringAsync();

                if (!response.IsSuccessStatusCode)
                {
                    return(Result.Failed(Convert.ToInt32(response.StatusCode), $"Failed to logout from {Appliance.Host} -> {response.ReasonPhrase}."));
                }

                ESightAPIResult result = ReadAsJsonDataContract <ESightAPIResult>(responseContent);
                if (result.code == 0)
                {
                    return(Result.Done());
                }
                else
                {
                    return(Result.Failed(result.code, $"Failed to logout from {Appliance.Host} -> {result.description}."));
                }
            }
            catch (Exception e)
            {
                return(Result.Failed(100, $"Failed to connect to {Appliance.Host} -> {e.Message}.", e));
            }
        }