Пример #1
0
        /// <inheritdoc />
        /// <summary>
        /// Gets the data required to be displayed on the login screen,
        /// including field labels, etc.
        /// <para>
        /// Calls "https://*/api/v1/login/pageData/{electionId}" (See: API's
        /// LoginController)
        /// </para>
        /// </summary>
        /// <param name="electionId">The election identifier.</param>
        /// <returns>
        /// A hydrated instance of the <see cref="LoginViewData"/> class.
        /// </returns>
        public async Task <LoginViewData> GetDataForLoginScreenAsync(int electionId)
        {
            var httpClient = _httpClientProvider.GetHttpClientInstance();

            try
            {
                var callUrl = //_webConfigReaderService.GetAppSetting<string>("BaseApiUrl");
                              _webConfigContainer.BaseApiUrl;

                if (!callUrl.EndsWith("/"))
                {
                    callUrl = callUrl.TrimEnd() + "/";
                }

                callUrl += $"login/pageData/{electionId}";

                // build the Authorization header value
                httpClient.DefaultRequestHeaders.Authorization =
                    new AuthenticationHeaderValue(
                        _webConfigContainer.AuthScheme,
                        _webConfigContainer.ApiKey);

                HttpResponseMessage response = await httpClient.GetAsync(callUrl);

                LoginViewData loginViewData;
                if (response.IsSuccessStatusCode && (response.StatusCode == HttpStatusCode.OK))
                {
                    var data = await response.Content.ReadAsStringAsync();

                    loginViewData = JsonConvert.DeserializeObject <LoginViewData>(data);
                }
                else
                {
                    throw new Exception("The attempted API call apparently failed. (in GetDataForLoginScreenAsync())");
                }

                return(loginViewData ?? new LoginViewData());
            }
            catch (HttpRequestException httpReqException)
            {
                // TODO: Add real logging
                Debug.WriteLine(
                    "EXCEPTION in: Task<LoginViewModel> GetDataForLoginScreenAsync(int electionId) ***\r\n" +
                    httpReqException.Message + "\r\n" +
                    httpReqException.StackTrace);
                throw;
            }
            catch (Exception oEx)
            {
                // TODO: Add real logging
                Debug.WriteLine(
                    "EXCEPTION in: Task<LoginViewModel> GetDataForLoginScreenAsync(int electionId) ***\r\n" +
                    oEx.Message + "\r\n" +
                    oEx.StackTrace);
                throw;
            }
        }
Пример #2
0
        /// <summary>
        /// Gets the public repository(s) of/by the specified username.
        /// </summary>
        /// <param name="ghUsername">
        /// The GitHub username.
        /// </param>
        /// <returns>
        /// A Task&lt;List&lt;GhUserRepo&gt;&gt;
        /// </returns>
        /// <exception cref="ArgumentNullException">ghUsername</exception>
        /// <exception cref="Exception">Error creating HttpClient instance.</exception>
        public async Task <List <GhUserRepo> > GetPublicGhUserReposByUsername(
            string ghUsername)
        {
            if (string.IsNullOrWhiteSpace(ghUsername))
            {
                throw new ArgumentNullException(nameof(ghUsername));
            }

            // Get the HttpClient
            var httpClient = _httpClientProvider.GetHttpClientInstance();

            // Set the Authentication stuff into the HttpClient instance
            if (!_httpClientAuthPrvdr.AddBasicAuthorizationHeaderValue(
                    httpClient,
                    _apiCredentialsReader.GetUsername(),
                    _apiCredentialsReader.GetPassword()))
            {
                throw new Exception("Error creating HttpClient instance.");
            }

            List <GhUserRepo> rawRepoData = null;

            try
            {
                // Call the 'repo' layer to make the actual call to GitHub
                rawRepoData = await _ghRepos.GetGitHubReposAsync(httpClient, ghUsername);
            }
            catch (HttpRequestException httpReqException)
            {
                Debug.WriteLine(
                    "*** Task<List<GhUserRepo>> GetPublicGhUserReposByUsername() EXCEPTION: ***\r\n" +
                    httpReqException.Message + "\r\n" +
                    httpReqException.StackTrace);
                throw;
            }
            catch (Exception oEx)
            {
                Console.WriteLine(oEx);
                throw;
            }

            return(rawRepoData ?? new List <GhUserRepo>());
        }
Пример #3
0
        /// <inheritdoc />
        /// <summary>
        /// Checks the credentials entered by the user against the database.
        /// <para>
        /// Calls "https://*/api/v1/login/{userCredentialsModel}" (See: API's
        /// LoginController)
        /// </para>
        /// </summary>
        /// <param name="userCredentialsModel">
        /// An instance of the <see cref="UserCredentialsModel"/> class
        /// containing the username/password entered by the user.
        /// </param>
        /// <returns>
        /// A Task&lt;bool&gt; that indicates whether (true) or not (false)
        /// the credentials entered by the user are valid.
        /// </returns>
        public async Task <bool> ValidateUserCredentialsAsync(
            UserCredentialsModel userCredentialsModel)
        {
            const string exceptionHeadText =
                "EXCEPTION in: Task<LoginViewModel> ValidateUserCredentialsAsync(int electionId) ***\r\n";

            var httpClient = _httpClientProvider.GetHttpClientInstance();

            try
            {
                var callUrl = _webConfigContainer.BaseApiUrl;
                if (!callUrl.EndsWith("/"))
                {
                    callUrl = callUrl.TrimEnd() + "/";
                }

                callUrl += "login/";

                var myContent   = JsonConvert.SerializeObject(userCredentialsModel);
                var buffer      = System.Text.Encoding.UTF8.GetBytes(myContent);
                var byteContent = new ByteArrayContent(buffer);
                byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                // build the Authorization header value
                httpClient.DefaultRequestHeaders.Authorization =
                    new AuthenticationHeaderValue(
                        _webConfigContainer.AuthScheme,
                        _webConfigContainer.ApiKey);

                HttpResponseMessage response = await httpClient.PostAsync(callUrl, byteContent);

                bool callResponse = false;
                if (response.IsSuccessStatusCode && (response.StatusCode == HttpStatusCode.OK))
                {
                    //var testReturnValue = response.Content.
                    var data = await response.Content.ReadAsStringAsync();

                    callResponse = JsonConvert.DeserializeObject <bool>(data);
                }
                else
                {
                    throw new Exception("The attempted call of the API apparently failed. (in ValidateUserCredentialsAsync())");
                }

                return(callResponse);
            }
            catch (HttpRequestException httpReqException)
            {
                // TODO: Add real logging
                Debug.WriteLine(
                    exceptionHeadText +
                    httpReqException.Message + "\r\n" +
                    httpReqException.StackTrace);
                throw;
            }
            catch (Exception oEx)
            {
                // TODO: Add real logging
                Debug.WriteLine(
                    exceptionHeadText +
                    oEx.Message + "\r\n" +
                    oEx.StackTrace);
                throw;
            }
        }