|
1 | | -using System.Net; |
| 1 | +using HAC.API.Data; |
2 | 2 | using HAC.API.Data.Objects; |
3 | 3 | using Microsoft.AspNetCore.Http; |
4 | 4 | using Microsoft.Extensions.Logging; |
5 | 5 |
|
6 | | -namespace HAC.API.Controllers |
7 | | -{ |
8 | | - public static class Utils |
9 | | - { |
10 | | - public static Response GetResponse<T>(HttpContext context, ILogger<T> logger) |
11 | | - { |
| 6 | +namespace HAC.API.Controllers { |
| 7 | + public static class Utils { |
| 8 | + public static Response GetResponse<T>(HttpContext context, ILogger<T> logger) { |
12 | 9 | string hacLink = context.Request.Query["hacLink"]; |
13 | 10 | string username = context.Request.Query["username"]; |
14 | 11 | string password = context.Request.Query["password"]; |
15 | | - |
16 | | - if (string.IsNullOrEmpty(hacLink)) |
17 | | - return new Response { Message = "Error 404: Empty hacLink parameter." }; |
18 | | - if (string.IsNullOrEmpty(username)) |
19 | | - return new Response { Message = "Error 404: Empty username parameter." }; |
| 12 | + |
| 13 | + if (string.IsNullOrEmpty(hacLink)) |
| 14 | + return new Response {Message = "Error 404: Empty hacLink parameter."}; |
| 15 | + if (string.IsNullOrEmpty(username)) |
| 16 | + return new Response {Message = "Error 404: Empty username parameter."}; |
20 | 17 | if (string.IsNullOrEmpty(password)) |
21 | | - return new Response { Message = "Error 404: Empty password parameter." }; |
22 | | - |
| 18 | + return new Response {Message = "Error 404: Empty password parameter."}; |
| 19 | + |
23 | 20 | logger.LogInformation("Received a request.\n" + |
24 | | - $"Link: {hacLink}\n" + |
25 | | - $"Username: {username}\n" + |
26 | | - $"Password: {password}"); |
27 | | - |
28 | | - var hac = new Data.Hac(); |
29 | | - HttpWebResponse response = hac.Login(hacLink, username, password, out var container); |
| 21 | + $"Link: {hacLink}\n" + |
| 22 | + $"Username: {username}\n" + |
| 23 | + $"Password: {password}"); |
| 24 | + |
| 25 | + var response = Hac.Login(hacLink, username, password); |
| 26 | + var container = response.Result.CookieContainer; |
| 27 | + var uri = response.Result.RequestUri; |
30 | 28 |
|
31 | | - if (!hac.IsValidLogin(response)) //checks if login creds are true |
| 29 | + if (!Hac.IsValidLogin(response.Result.ResponseBody)) //checks if login credentials are true |
32 | 30 | { |
33 | 31 | const string errorText = "Either the HAC username or password is incorrect."; |
34 | | - return new Response |
35 | | - { |
| 32 | + return new Response { |
36 | 33 | Message = errorText |
37 | 34 | }; |
38 | 35 | } |
39 | 36 |
|
40 | 37 | var result = new Response(); |
41 | 38 | var type = logger.GetType().GenericTypeArguments[0]; |
42 | | - |
| 39 | + |
43 | 40 | if (type == typeof(HacController)) |
44 | | - { |
45 | | - result = hac.GetAll(container, response.ResponseUri, hacLink); |
46 | | - } |
| 41 | + result = Hac.GetAll(container, uri, hacLink); |
47 | 42 | else if (type == typeof(StudentController)) |
48 | | - { |
49 | | - result = hac.GetStudentInfo(container, response.ResponseUri, hacLink); |
50 | | - } |
| 43 | + result = Hac.GetStudentInfo(container, uri, hacLink); |
51 | 44 | else if (type == typeof(CourseController)) |
52 | | - { |
53 | | - result = hac.GetCourses(container, response.ResponseUri, hacLink); |
54 | | - } |
| 45 | + result = Hac.GetCourses(container, uri, hacLink); |
55 | 46 | else if (type == typeof(IprController)) |
56 | | - { |
57 | | - result = hac.GetIpr(container, response.ResponseUri, hacLink); |
58 | | - } |
| 47 | + result = Hac.GetIpr(container, uri, hacLink); |
59 | 48 | else if (type == typeof(ReportCardController)) |
60 | | - { |
61 | | - result = hac.GetReportCard(container, response.ResponseUri, hacLink); |
62 | | - } |
| 49 | + result = Hac.GetReportCard(container, uri, hacLink); |
63 | 50 | else if (type == typeof(TranscriptController)) |
64 | | - { |
65 | | - result = hac.GetTranscript(container, response.ResponseUri, hacLink); |
66 | | - } |
67 | | - else if (type == typeof(AttendanceController)) |
68 | | - { |
69 | | - result = hac.GetAttendance(container, response.ResponseUri, hacLink); |
70 | | - } |
| 51 | + result = Hac.GetTranscript(container, uri, hacLink); |
| 52 | + else if (type == typeof(AttendanceController)) result = Hac.GetAttendance(container, uri, hacLink); |
71 | 53 |
|
72 | 54 | return result; |
73 | 55 | } |
|
0 commit comments