public static async Task Main(string[] args)
        {
            var workOrderForCreate = new WorkOrderCreateRequest {
                Description = "This is the Description", CreationDate = DateTime.Now, CustomerEmail = "*****@*****.**"
            };
            var workOrderSettings = new WorkOrderSettings {
                UserName = "******", Password = "******", BaseUrl = "https://localhost:44373/workorder"
            };

            var workOrderServiceAuthTokenProvider = new WorkOrderServiceAuthTokenProvider();
            var workOrderId = await workOrderServiceAuthTokenProvider.CreateWorkOrder(workOrderForCreate, workOrderSettings).ConfigureAwait(false);

            Console.WriteLine("Original Version");
            Console.WriteLine($"Work Order Id: {workOrderId}");
            Console.ReadLine();
        }
Ejemplo n.º 2
0
        // TODO: PWI-Design: Only the CreateWorkOrder method needs to/should be public.
        // TODO: PWI-Design: Leaky iky - WorkOrderCreateRequest - Don't leak models from a service into the application
        // TODO: PWI-Design: Levels of Abstraction GetAccessTokenAsync and Logout are correct, the rest need to be abstracted to another method
        public async Task <string> CreateWorkOrder(WorkOrderCreateRequest workOrderCreateRequest, WorkOrderSettings workOrderSettings)
        {
            // TODO: PWI: Don't Invent names
            // TODO: PWI: HttpRequestMessage is Disposable. They MUST be disposed
            var req = new HttpRequestMessage();
            // TODO: PWI: Don't Invent names - name this local variable accessToken
            // TODO: PWI-Bug: Need ConfigureAwait(false) in all awaited methods (unless project is .NET Core)
            string access = await GetAccessTokenAsync(workOrderSettings);

            // TODO: PWI-Design: Extract the creation and initialization of HttpRequestMessage to a private static method
            req.Headers.Authorization = new AuthenticationHeaderValue("AR-JWT", access);
            req.RequestUri            = new Uri(workOrderSettings.BaseUrl);
            req.Method = HttpMethod.Post;

            // TODO: PWI: No need to use Newtonsoft to serialize
            // TODO: PWI: Define local const "mediaType" for "application/json"
            // TODO: PWI: Use var when you can
            string createRequest = JsonConvert.SerializeObject(workOrderCreateRequest);

            req.Content = new StringContent(createRequest, Encoding.UTF8, "application/json");
            req.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            // TODO: PWI: Don't Invent names
            // TODO: PWI-Bug: HttpClient should NOT be created each time - Cache instance as class member
            // TODO: PWI-Bug: HttpClient is Disposable and MUST be Disposed
            var httpClient = new HttpClient();
            // TODO: PWI: Don't Invent names
            var response = await httpClient.SendAsync(req);

            // TODO: PWI-Bug: Not checking HttpResponseMessage after SendAsync (in both methods)

            // TODO: PWI: HttpClient define BaseUri property, then use only the trailing part of Url
            await Logout(access, workOrderSettings.BaseUrl + "/logout");

            // TODO: PWI-Bug: EnsureSuccessStatusCode() should be called after SendAsync on line 117
            response.EnsureSuccessStatusCode();

            // TODO: PWI-Bug: Need ConfigureAwait(false) in all awaited methods (unless project is .NET Core)
            return(await response.Content.ReadAsStringAsync());
        }
Ejemplo n.º 3
0
        public async Task <string> CreateWorkOrder(WorkOrderCreateRequest workOrderCreateRequest, WorkOrderSettings workOrderSettings)
        {
            var req = new HttpRequestMessage();

            string access = await GetAccessTokenAsync(workOrderSettings);

            req.Headers.Authorization = new AuthenticationHeaderValue("AR-JWT", access);
            req.RequestUri            = new Uri(workOrderSettings.BaseUrl);
            req.Method = HttpMethod.Post;

            string createRequest = JsonConvert.SerializeObject(workOrderCreateRequest);

            req.Content = new StringContent(createRequest, Encoding.UTF8, "application/json");
            req.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            var httpClient = new HttpClient();
            var response   = await httpClient.SendAsync(req);

            await Logout(access, workOrderSettings.BaseUrl + "/logout");

            response.EnsureSuccessStatusCode();

            return(await response.Content.ReadAsStringAsync());
        }