示例#1
0
        // TODO: PWI: Public methods should not be virtual
        // TODO: PWI: Don't Invent names - settings parameter should be called workOrderSettings
        // TODO: PWI: Need to know Basis - No need to ask for the entire WorOrderSettings class, just username and password
        // TODO: PWI-Design: Only the CreateWorkOrder method needs to/should be public.
        // TODO: PWI-Design: No need for virtual methods in this class
        // TODO: PWI-Design: GetAccessToken - Isn't this method essentially Log in?
        public virtual async Task <string> GetAccessTokenAsync(WorkOrderSettings settings)
        {
            var credentials = new List <KeyValuePair <string, string> >
            {
                new KeyValuePair <string, string>("username", settings.UserName),
                new KeyValuePair <string, string>("password", settings.Password)
            };
            // TODO: PWI: Leave blanks lines after lines that end in "};"
            // TODO: PWI: Don't Invent names - Rename client to httpClient
            // 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 client = new HttpClient();
            // TODO: PWI: HttpClient define BaseUri property, then use only the trailing part of Url
            // TODO: PWI: Don't Invent names - Rename req to httpRequestMessage
            // TODO: PWI: HttpRequestMessage is Disposable. They MUST be disposed
            var req = new HttpRequestMessage(HttpMethod.Post, settings.BaseUrl + "/login")
            {
                Content = new FormUrlEncodedContent(credentials)
            };
            // TODO: PWI: Leave blanks lines after lines that end in "};"
            // TODO: PWI: Don't Invent names - Rename response to httpResponseMessage
            // TODO: PWI-Bug: Need ConfigureAwait(false) in all awaited methods (unless project is .NET Core)
            var response = await client.SendAsync(req);

            // TODO: PWI-Bug: Not checking HttpResponseMessage after SendAsync (in both methods)
            // TODO: PWI-Bug: Need ConfigureAwait(false) in all awaited methods (unless project is .NET Core)
            var contents = await response.Content.ReadAsStringAsync();

            return(contents);
        }
        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();
        }
示例#3
0
        public virtual async Task <string> GetAccessTokenAsync(WorkOrderSettings settings)
        {
            var credentials = new List <KeyValuePair <string, string> >
            {
                new KeyValuePair <string, string>("username", settings.UserName),
                new KeyValuePair <string, string>("password", settings.Password)
            };
            var client = new HttpClient();
            var req    = new HttpRequestMessage(HttpMethod.Post, settings.BaseUrl + "/login")
            {
                Content = new FormUrlEncodedContent(credentials)
            };
            var response = await client.SendAsync(req);

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

            return(contents);
        }
示例#4
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());
        }
示例#5
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());
        }