Example #1
0
        /// <summary>
        /// Sets up a session with the Tado API based on the refresh token
        /// </summary>
        /// <returns>Session instance</returns>
        private async Task <Entities.Session> GetRefreshedSession()
        {
            // Build the POST body with the authentication arguments
            var queryBuilder = new Helpers.QueryStringBuilder();

            queryBuilder.Add("client_id", ClientId);
            queryBuilder.Add("grant_type", "refresh_token");
            queryBuilder.Add("client_secret", ClientSecret);
            queryBuilder.Add("refresh_token", AuthenticatedSession.RefreshToken);
            queryBuilder.Add("scope", "home.user");

            return(await PostMessageGetResponse <Entities.Session>(TadoApiAuthUrl, queryBuilder, false));
        }
Example #2
0
        /// <summary>
        /// Sets up a new session with the Tado API
        /// </summary>
        /// <returns>Session instance</returns>
        private async Task <Entities.Session> GetNewSession()
        {
            // Build the POST body with the authentication arguments
            var queryBuilder = new Helpers.QueryStringBuilder();

            queryBuilder.Add("client_id", ClientId);
            queryBuilder.Add("grant_type", "password");
            queryBuilder.Add("client_secret", ClientSecret);
            queryBuilder.Add("password", Password);
            queryBuilder.Add("scope", "home.user");
            queryBuilder.Add("username", Username);

            return(await PostMessageGetResponse <Entities.Session>(TadoApiAuthUrl, queryBuilder, false));
        }
Example #3
0
        /// <summary>
        /// Sends a HTTP POST to the provided uri
        /// </summary>
        /// <param name="queryBuilder">The querystring parameters to send in the POST body</param>
        /// <typeparam name="T">Type of object to try to parse the response JSON into</typeparam>
        /// <param name="uri">Uri of the webservice to send the message to</param>
        /// <param name="requiresAuthenticatedSession">True to indicate that this request must have a valid oAuth token</param>
        /// <returns>Object of type T with the parsed response</returns>
        private async Task <T> PostMessageGetResponse <T>(Uri uri, Helpers.QueryStringBuilder queryBuilder, bool requiresAuthenticatedSession)
        {
            if (uri == null)
            {
                throw new ArgumentNullException("Uri has not been provided");
            }

            if (requiresAuthenticatedSession)
            {
                // Ensure a valid OAuth token is set on the HttpClient if possible
                await EnsureAccessToken();
            }

            // Prepare the content to POST
            using (var content = new StringContent(queryBuilder.ToString(), Encoding.UTF8, "application/x-www-form-urlencoded"))
            {
                // Construct the message towards the webservice
                using (var request = new HttpRequestMessage(HttpMethod.Post, uri))
                {
                    // Set the content to send along in the message body with the request
                    request.Content = content;

                    try
                    {
                        // Request the response from the webservice
                        var response = await _httpClient.SendAsync(request);

                        var responseBody = await response.Content.ReadAsStringAsync();

                        // Verify if the request was successful (response status 200-299)
                        if (response.IsSuccessStatusCode)
                        {
                            // Request was successful
                            var responseEntity = JsonConvert.DeserializeObject <T>(responseBody);
                            return(responseEntity);
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exceptions.RequestFailedException(uri, ex);
                    }

                    // Request was not successful. throw an exception
                    throw new Exceptions.RequestFailedException(uri);
                }
            }
        }