protected virtual string GetApiResponse() { Client = HttpsClientFactory.NewClient(); int responseCode; string responseString; try { if (Request == null) { Request = GetClientRequest(); } } catch (Exception ex) { CrestronConsole.PrintLine("SimplTeslaMaster.Results.BaseResult.GetApiResponse()::Failed to get client request " + ex.ToString()); } Request.Url.Parse(ApiUrl); Client.Url = Request.Url; try { Request.FinalizeHeader(); } catch (Exception ex) { CrestronConsole.PrintLine("SimplTeslaMaster.Results.BaseResult.GetApiResponse()::Failed to finalize header " + ex.ToString()); } Response = Client.Dispatch(Request); responseCode = Response.Code; responseString = Response.ContentString; if (responseCode != 200) { CrestronConsole.PrintLine("SimplTeslaMaster.Results.BaseResult.GetApiResponse()::Request was not successful!"); CrestronConsole.PrintLine("SimplTeslaMaster.Results.BaseResult>GetApiResponse()::Response code: " + responseCode.ToString()); CrestronConsole.PrintLine("SimplTeslaMaster.Results.BaseResult>GetApiResponse()::Response content: " + responseString); } Request = null; Client.Dispose(); Client = null; return(Response.ContentString); }
/// <summary> /// Retrieves a token from Tesla's authorization API /// </summary> public static Token GetToken() { HttpsClient client; HttpsClientRequest request; HttpsClientResponse response = null; Token result = null; // Create a new web client client = HttpsClientFactory.NewClient(); // Create the reqeuest object request = GetRequest(); // Set the URL to communicate with client.Url = request.Url; // Finalize the HTTP request header that was geerated in GetRequest() request.FinalizeHeader(); try { // Send the request to Tesla's server response = client.Dispatch(request); // Covert Tesla's respose to a Token object by deserializing the JSON response result = (Token)JsonConvert.DeserializeObject(response.ContentString, typeof(Token)); } catch (Exception ex) { // If there's a error, make note of it in the Crestron console. This will appear both in the Toolbox's log, and in the SSH/Command line if logged in CrestronConsole.PrintLine("SimplTeslaCar.Tokens.TokenGenerator.GetToken()::Error in Tesla API (TokenGenerator.GetToken): Unable to get response. " + ex.ToString()); ErrorLog.Exception("SimplTeslaCar.Tokens.TokenGenerator.GetToken()::Error in Tesla API (TokenGenerator.GetToken): Unable to get response.", ex); } // Clear the objects that were used from memory request = null; client.Dispose(); client = null; return(result); }
/// <summary> /// Sends the web request to the API /// </summary> /// <returns>The API's response</returns> protected virtual string GetApiResponse() { int responseCode; string responseString; Client = HttpsClientFactory.NewClient(); if (Request == null) { Request = GetClientRequest(); } Request.Url.Parse(ApiUrl); Client.Url = Request.Url; Request.FinalizeHeader(); Response = Client.Dispatch(Request); // Retrieving the response code serves 2 purposes: // 1) It ensures that the thread has waited for the Dispatch request to have completed before proceeding, preventing null results; and // 2) It allows verification of success (200) responseCode = Response.Code; responseString = Response.ContentString; if (responseCode != 200) { CrestronConsole.PrintLine("SimplTeslaCar.Results.BaseResult.GetApiResponse()::Request was not successful!"); CrestronConsole.PrintLine("SimplTeslaCar.Results.BaseResult>GetApiResponse()::Response code: " + responseCode.ToString()); CrestronConsole.PrintLine("SimplTeslaCar.Results.BaseResult>GetApiResponse()::Response content: " + responseString); } Request = null; Client.Dispose(); Client = null; return(Response.ContentString); }
/// <summary> /// Requests an authorization token from the Tesla servers /// </summary> public static Token GetToken() { HttpsClient client; HttpsClientRequest request; HttpsClientResponse response = null; Token result = null; client = HttpsClientFactory.NewClient(); // Create the Client request request = GetRequest(); // Set the client's endpoint and finalize the HTTP header client.Url = request.Url; request.FinalizeHeader(); try { // Send the request to Tesla's server, and get the response back response = client.Dispatch(request); // Convert the JSON to a token object result = (Token)JsonConvert.DeserializeObject(response.ContentString, typeof(Token)); } catch (Exception ex) { CrestronConsole.PrintLine("SimplTeslaMaster.Tokens.TokenGenerator.GetToken()::Error in Tesla API (TokenGenerator.GetToken): Unable to get response. " + ex.ToString()); ErrorLog.Exception("Error in Tesla API (TokenGenerator.GetToken): Unable to get response.", ex); } // Clean up request = null; client.Dispose(); client = null; return(result); }