Beispiel #1
0
        static void PerformWeatherizationDataTransferJobBatchSubmission()
        {
            // Create a client with the required security information
            ApiClient client = new ApiClient(_csdApiUrl, _azureApplicationId, _secretKeyValue, _azureAdInstance, _azureDomain);

            /*
             * Building a request object is optional, you can also just post the XML directly in the body of the API call:
             * View PostJobBatches(string xmlString) for an example.
             *
             * In this example the XML is deserialized into a request object in order to model a system where the
             * data may have been loaded from a database prior to being posted to the API.
             * */


            WeatherizationDataTransferRequest.WeatherizationDataTransfer request = new WeatherizationDataTransferRequest.WeatherizationDataTransfer
            {
                AgencyCode   = "",
                BatchGUID    = "",
                EmailAddress = "",
                JobRecord    = GetTestJobRecords()
            };

            // Call the API and get your response object
            var response = client.PostJobBatches(request);

            /*
             * From here you are free to perform any business related needs with the response object
             */
            if (response != null)
            {
                Console.WriteLine("Csd Api Call Completed");
                Console.WriteLine("Status:{0} Message: {1}", response.status, response.message);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Post job batches using a request object
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public WeatherizationDataTransferResponse PostJobBatches(WeatherizationDataTransferRequest.WeatherizationDataTransfer request)
        {
            var response = new WeatherizationDataTransferResponse();

            try
            {
                // Serialize the request object into XML
                var xmlString = ToXml <WeatherizationDataTransferRequest.WeatherizationDataTransfer>(request);

                // Get the Azure Authentication Token
                var token = base.GetAccessToken();

                // Create an HttpClient
                HttpClient client  = new HttpClient();
                var        builder = new UriBuilder(new Uri(string.Format("{0}/{1}/{2}", base.csdApiUrl, "weatherization", "jobbatches")));

                // Assign the token to the authorization header
                client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);

                // Create an HttpContent object using the XML from the request (you can also submit JSON, just change the type to application/json)
                var httpContent = new StringContent(xmlString, Encoding.UTF8, "application/xml");

                // Call the API as a HttpPost, put the newly created httpContent object into the body of the post
                HttpResponseMessage responseMessage = client.PostAsync(builder.Uri, httpContent).Result;

                // Check the status code for success
                if (responseMessage.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    // Deserialize the response as a typed object in order to view it's returned properties
                    response = JsonConvert.DeserializeObject <WeatherizationDataTransferResponse>(responseMessage.Content.ReadAsStringAsync().Result);
                }
                else
                {
                    response.status  = responseMessage.StatusCode;
                    response.message = responseMessage.ReasonPhrase;
                }
            }
            catch (Exception ex)
            {
                response.status  = System.Net.HttpStatusCode.InternalServerError;
                response.message = ex.Message;
            }

            return(response);
        }