Skip to content

Commit fdc4108

Browse files
committed
Made getDistrictList static
1 parent a96b335 commit fdc4108

File tree

4 files changed

+96
-49
lines changed

4 files changed

+96
-49
lines changed

Sources/SwiftVue/DataProvider/DataProvider.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ public protocol DataProvider {
2020
func getReportCard(documentGUID: String) async throws -> String
2121
func listDocuments() async throws -> String
2222
func getDocument(documentGUID: String) async throws -> String
23-
func getDistrictList(zip: String) async throws -> [DistrictInfo]
2423
func getMailInboxCount() async throws -> String
2524
func verifyCredentials() async throws -> Bool
25+
26+
static func getDistrictList(zip: String) async throws -> [DistrictInfo]
2627
}

Sources/SwiftVue/DataProvider/PreviewDataProvider.swift

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,15 @@
88
import Foundation
99

1010
public class PreviewDataProvider: DataProvider {
11+
public static var delayNanoseconds = UInt64(2e9)
1112
private let validCredentials: Bool
12-
public var delayNanoseconds: UInt64
1313

14-
public init(credentials: Credentials = Credentials.preview, delayNanoseconds: UInt64 = UInt64(2e9)) {
14+
public init(credentials: Credentials = Credentials.preview) {
1515
self.validCredentials = credentials == Credentials.preview
16-
self.delayNanoseconds = delayNanoseconds
1716
}
1817

19-
public init(username: String, password: String, districtURL: String, delayNanoseconds: UInt64 = UInt64(2e9)) {
18+
public init(username: String, password: String, districtURL: String) {
2019
self.validCredentials = Credentials(username: username, password: password, districtURL: districtURL) == Credentials.preview
21-
self.delayNanoseconds = delayNanoseconds
2220
}
2321

2422
public func getMessages() async throws -> String {
@@ -38,7 +36,7 @@ public class PreviewDataProvider: DataProvider {
3836

3937
public func getGradebook(reportPeriod: Int?) async throws -> Gradebook {
4038
guard validCredentials else { throw SwiftVueError.invalidCredentials }
41-
try await delay()
39+
try await Self.delay()
4240
switch reportPeriod {
4341
case 0: return PreviewData.gradebook1
4442
case 1: return PreviewData.gradebook2
@@ -56,13 +54,13 @@ public class PreviewDataProvider: DataProvider {
5654

5755
public func getStudentInfo() async throws -> StudentInfo {
5856
guard validCredentials else { throw SwiftVueError.invalidCredentials }
59-
try await delay()
57+
try await Self.delay()
6058
return PreviewData.student1
6159
}
6260

6361
public func getSchedule(termIndex: Int?) async throws -> Schedule {
6462
guard validCredentials else { throw SwiftVueError.invalidCredentials }
65-
try await delay()
63+
try await Self.delay()
6664
switch termIndex {
6765
case 0: return PreviewData.schedule1
6866
case 1: return PreviewData.schedule2
@@ -96,12 +94,6 @@ public class PreviewDataProvider: DataProvider {
9694
throw SwiftVueError.notImplemented("PreviewDataProvider.getDocument")
9795
}
9896

99-
public func getDistrictList(zip: String) async throws -> [DistrictInfo] {
100-
guard validCredentials else { throw SwiftVueError.invalidCredentials }
101-
try await delay()
102-
return PreviewData.districtList1
103-
}
104-
10597
public func getMailInboxCount() async throws -> String {
10698
guard validCredentials else { throw SwiftVueError.invalidCredentials }
10799
throw SwiftVueError.notImplemented("PreviewDataProvider.getMailInboxCount")
@@ -111,7 +103,12 @@ public class PreviewDataProvider: DataProvider {
111103
return validCredentials
112104
}
113105

114-
private func delay() async throws {
115-
try await Task.sleep(nanoseconds: delayNanoseconds)
106+
public static func getDistrictList(zip: String) async throws -> [DistrictInfo] {
107+
try await delay()
108+
return PreviewData.districtList1
109+
}
110+
111+
private static func delay() async throws {
112+
try await Task.sleep(nanoseconds: Self.delayNanoseconds)
116113
}
117114
}

Sources/SwiftVue/DataProvider/RealDataProvider.swift

Lines changed: 79 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,77 @@ public class RealDataProvider: DataProvider {
2020
self.credentials = Credentials(username: username, password: password, districtURL: districtURL)
2121
}
2222

23-
private func processRequest(method: String, paramString: String, handle: String, user: Bool) async throws -> String {
24-
let username: String = if user { credentials.username } else { Self.edupointCredentials.username }
25-
let password: String = if user { credentials.password } else { Self.edupointCredentials.password }
23+
private func processRequest(method: String, paramString: String) async throws -> String {
24+
let body: String =
25+
"""
26+
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
27+
<soap:Body>
28+
<ProcessWebServiceRequest xmlns="http://edupoint.com/webservices/">
29+
<userID>\(credentials.username)</userID>
30+
<password>\(credentials.password)</password>
31+
<skipLoginLog>1</skipLoginLog>
32+
<parent>0</parent>
33+
<webServiceHandleName>PXPWebServices</webServiceHandleName>
34+
<methodName>\(method)</methodName>
35+
<paramStr>\(paramString)</paramStr>
36+
</ProcessWebServiceRequest>
37+
</soap:Body>
38+
</soap:Envelope>
39+
"""
40+
41+
guard let url = URL(string: credentials.districtURL + "/Service/PXPCommunication.asmx") else {
42+
throw SwiftVueError.invalidURL
43+
}
44+
45+
var request: URLRequest = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData)
46+
request.addValue("text/xml", forHTTPHeaderField: "Content-Type")
47+
request.addValue("http://edupoint.com/webservices/ProcessWebServiceRequest", forHTTPHeaderField: "SOAPAction")
48+
request.httpMethod = "POST"
49+
request.httpBody = body.data(using: .utf8)
50+
51+
let session = URLSession(configuration: URLSessionConfiguration.ephemeral)
52+
session.configuration.requestCachePolicy = .reloadIgnoringLocalAndRemoteCacheData
53+
54+
let (data, response) = try await session.data(for: request)
55+
56+
guard let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 else {
57+
throw SwiftVueError.invalidResponse
58+
}
59+
60+
guard let result = String(data: data, encoding: .utf8) else {
61+
throw SwiftVueError.couldNotDecodeResponse
62+
}
2663

64+
return result
65+
}
66+
67+
public func makeRequest(method: String, params: [String: String] = [:]) async throws -> String {
68+
let paramStr = "&lt;Parms&gt;" + params.reduce("") { accumulation, pair in
69+
accumulation + "&lt;\(pair.key)&gt;\(pair.value)&lt;/\(pair.key)&gt;"
70+
} + "&lt;/Parms&gt;"
71+
let processedRequest = try await processRequest(method: method, paramString: paramStr)
72+
return processedRequest.replacingEscapements()
73+
}
74+
75+
private static func processRequest(method: String, paramString: String) async throws -> String {
2776
let body: String =
2877
"""
2978
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
3079
<soap:Body>
3180
<ProcessWebServiceRequest xmlns="http://edupoint.com/webservices/">
32-
<userID>\(username)</userID>
33-
<password>\(password)</password>
81+
<userID>\(edupointCredentials.username)</userID>
82+
<password>\(edupointCredentials.password)</password>
3483
<skipLoginLog>1</skipLoginLog>
3584
<parent>0</parent>
36-
<webServiceHandleName>\(handle)</webServiceHandleName>
85+
<webServiceHandleName>HDInfoServices</webServiceHandleName>
3786
<methodName>\(method)</methodName>
3887
<paramStr>\(paramString)</paramStr>
3988
</ProcessWebServiceRequest>
4089
</soap:Body>
4190
</soap:Envelope>
4291
"""
4392

44-
guard let url = URL(string: (user ? credentials.districtURL : Self.edupointCredentials.districtURL) + (user ? "/Service/PXPCommunication.asmx" : "/Service/HDInfoCommunication.asmx")) else {
93+
guard let url = URL(string: edupointCredentials.districtURL + "/Service/HDInfoCommunication.asmx") else {
4594
throw SwiftVueError.invalidURL
4695
}
4796

@@ -67,24 +116,24 @@ public class RealDataProvider: DataProvider {
67116
return result
68117
}
69118

70-
public func makeRequest(method: String, params: [String: String] = [:], handle: String, user: Bool = true) async throws -> String {
119+
public static func makeRequest(method: String, params: [String: String] = [:]) async throws -> String {
71120
let paramStr = "&lt;Parms&gt;" + params.reduce("") { accumulation, pair in
72121
accumulation + "&lt;\(pair.key)&gt;\(pair.value)&lt;/\(pair.key)&gt;"
73122
} + "&lt;/Parms&gt;"
74-
let processedRequest = try await processRequest(method: method, paramString: paramStr, handle: handle, user: user)
123+
let processedRequest = try await processRequest(method: method, paramString: paramStr)
75124
return processedRequest.replacingEscapements()
76125
}
77126

78127
public func getMessages() async throws -> String {
79-
return try await makeRequest(method: "GetPXPMessages", handle: "PXPWebServices")
128+
return try await makeRequest(method: "GetPXPMessages")
80129
}
81130

82131
public func getCalendar() async throws -> String {
83-
return try await makeRequest(method: "StudentCalendar", handle: "PXPWebServices")
132+
return try await makeRequest(method: "StudentCalendar")
84133
}
85134

86135
public func getAttendance() async throws -> Attendance {
87-
let result = try await makeRequest(method: "Attendance", handle: "PXPWebServices")
136+
let result = try await makeRequest(method: "Attendance")
88137

89138
if !result.contains("Attendance") {
90139
throw SwiftVueError.invalidCredentials
@@ -98,7 +147,7 @@ public class RealDataProvider: DataProvider {
98147
if let period = reportPeriod {
99148
params = ["ReportPeriod": "\(period)"]
100149
}
101-
let result = try await makeRequest(method: "Gradebook", params: params, handle: "PXPWebServices")
150+
let result = try await makeRequest(method: "Gradebook", params: params)
102151

103152
if !result.contains("Gradebook") {
104153
throw SwiftVueError.invalidCredentials
@@ -108,11 +157,11 @@ public class RealDataProvider: DataProvider {
108157
}
109158

110159
public func getClassNotes() async throws -> String {
111-
return try await makeRequest(method: "StudentHWNotes", handle: "PXPWebServices")
160+
return try await makeRequest(method: "StudentHWNotes")
112161
}
113162

114163
public func getStudentInfo() async throws -> StudentInfo {
115-
let string = try await makeRequest(method: "StudentInfo", handle: "PXPWebServices")
164+
let string = try await makeRequest(method: "StudentInfo")
116165
if !string.contains("StudentInfo") {
117166
throw SwiftVueError.invalidCredentials
118167
}
@@ -124,47 +173,47 @@ public class RealDataProvider: DataProvider {
124173
if let term = termIndex {
125174
params = ["TermIndex": "\(term)"]
126175
}
127-
let string = try await makeRequest(method: "StudentClassList", params: params, handle: "PXPWebServices")
176+
let string = try await makeRequest(method: "StudentClassList", params: params)
128177
if !string.contains("StudentClassSchedule") {
129178
throw SwiftVueError.invalidCredentials
130179
}
131180
return try ScheduleParser(string: string).parse()
132181
}
133182

134183
public func getSchoolInfo() async throws -> String {
135-
return try await makeRequest(method: "StudentSchoolInfo", handle: "PXPWebServices")
184+
return try await makeRequest(method: "StudentSchoolInfo")
136185
}
137186

138187
public func listReportCards() async throws -> String {
139-
return try await makeRequest(method: "GetReportCardInitialData", handle: "PXPWebServices")
188+
return try await makeRequest(method: "GetReportCardInitialData")
140189
}
141190

142191
public func getReportCard(documentGUID: String) async throws -> String {
143-
return try await makeRequest(method: "GetReportCardDocumentData", params: ["DocumentGU": documentGUID], handle: "PXPWebServices")
192+
return try await makeRequest(method: "GetReportCardDocumentData", params: ["DocumentGU": documentGUID])
144193
}
145194

146195
public func listDocuments() async throws -> String {
147-
return try await makeRequest(method: "GetStudentDocumentInitialData", handle: "PXPWebServices")
196+
return try await makeRequest(method: "GetStudentDocumentInitialData")
148197
}
149198

150199
public func getDocument(documentGUID: String) async throws -> String {
151-
return try await makeRequest(method: "GetContentOfAttachedDoc", params: ["DocumentGU": documentGUID], handle: "PXPWebServices")
152-
}
153-
154-
public func getDistrictList(zip: String) async throws -> [DistrictInfo] {
155-
let string = try await makeRequest(method: "GetMatchingDistrictList", params: ["Key":"5E4B7859-B805-474B-A833-FDB15D205D40", "MatchToDistrictZipCode":"\(zip)"], handle: "HDInfoServices", user: false)
156-
if !string.contains("DistrictList") {
157-
throw SwiftVueError.invalidCredentials
158-
}
159-
return try DistrictInfoParser(string: string).parse()
200+
return try await makeRequest(method: "GetContentOfAttachedDoc", params: ["DocumentGU": documentGUID])
160201
}
161202

162203
public func getMailInboxCount() async throws -> String {
163-
return try await makeRequest(method: "SynergyMailGetInboxCount", handle: "PXPWebServices")
204+
return try await makeRequest(method: "SynergyMailGetInboxCount")
164205
}
165206

166207
public func verifyCredentials() async throws -> Bool {
167208
let string = try await getMailInboxCount()
168209
return string.contains("SynergyMailInboxCountXML")
169210
}
211+
212+
public static func getDistrictList(zip: String) async throws -> [DistrictInfo] {
213+
let string = try await makeRequest(method: "GetMatchingDistrictList", params: ["Key":"5E4B7859-B805-474B-A833-FDB15D205D40", "MatchToDistrictZipCode":"\(zip)"])
214+
if !string.contains("DistrictList") {
215+
throw SwiftVueError.invalidCredentials
216+
}
217+
return try DistrictInfoParser(string: string).parse()
218+
}
170219
}

Sources/SwiftVue/StudentVue/StudentVue.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ extension StudentVue {
7373
return try await dataProvider.getDocument(documentGUID: documentGUID)
7474
}
7575

76-
public func getDistrictList(zip: String) async throws -> [DistrictInfo] {
77-
return try await dataProvider.getDistrictList(zip: zip)
76+
public static func getDistrictList(zip: String) async throws -> [DistrictInfo] {
77+
return try await RealDataProvider.getDistrictList(zip: zip)
7878
}
7979

8080
public func getMailInboxCount() async throws -> String {

0 commit comments

Comments
 (0)