Refactor
This commit is contained in:
parent
db20f861a9
commit
321a62a650
8 changed files with 162 additions and 123 deletions
|
@ -6,7 +6,48 @@
|
|||
//
|
||||
|
||||
import Foundation
|
||||
import KeychainAccess
|
||||
import SwiftyJSON
|
||||
|
||||
func apiRequest(endpointURL: URL) {
|
||||
print("dupa")
|
||||
@available (iOS 14, macOS 11, watchOS 7, tvOS 14, *)
|
||||
public func apiRequest(endpointURL: String) -> URLRequest {
|
||||
var request = URLRequest(url: URL(string: endpointURL)!)
|
||||
request.httpMethod = "GET"
|
||||
|
||||
let keychain = Keychain()
|
||||
let id = keychain["acctualStudentId"] ?? ""
|
||||
|
||||
let account = keychain[id] ?? "[]"
|
||||
let data = Data(account.utf8)
|
||||
let accountJSON = try! JSON(data: data)
|
||||
|
||||
let fingerprint: String = "\(accountJSON["fingerprint"])"
|
||||
let privateKeyStringString: String = "\(accountJSON["privateKeyString"])"
|
||||
let privateKeyString: Data = Data(privateKeyStringString.utf8)
|
||||
let deviceModel: String = "\(accountJSON["deviceModel"])"
|
||||
|
||||
let now = Date()
|
||||
var vDate: String {
|
||||
let dateFormatter = DateFormatter()
|
||||
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss"
|
||||
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
|
||||
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
|
||||
|
||||
return "\(dateFormatter.string(from: now)) GMT"
|
||||
}
|
||||
|
||||
let signatures = getSignatures(request: request, fingerprint: fingerprint, privateKeyString: privateKeyString)
|
||||
request.setValue("\(signatures)", forHTTPHeaderField: "Signature")
|
||||
|
||||
request.allHTTPHeaderFields = [
|
||||
"User-Agent": "wulkanowy/1 CFNetwork/1220.1 Darwin/20.1.0",
|
||||
"vOS": "iOS",
|
||||
"vDeviceModel": deviceModel,
|
||||
"vAPI": "1",
|
||||
"vDate": vDate,
|
||||
"vCanonicalUrl": "api%2fmobile%2fregister%2fhebe"
|
||||
]
|
||||
|
||||
return request
|
||||
}
|
||||
|
||||
|
|
|
@ -1,69 +0,0 @@
|
|||
//
|
||||
// hebeRequest.swift
|
||||
//
|
||||
//
|
||||
// Created by Tomasz on 05/03/2021.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import KeychainAccess
|
||||
import SwiftyJSON
|
||||
|
||||
@available (iOS 14, macOS 11, watchOS 7, tvOS 14, *)
|
||||
func hebeRequest(id: String) {
|
||||
let keychain = Keychain()
|
||||
let account = keychain[id] ?? "[]"
|
||||
let data = Data(account.utf8)
|
||||
let accountJSON = try! JSON(data: data)
|
||||
|
||||
let fingerprint: String = "\(accountJSON["fingerprint"])"
|
||||
let privateKeyStringString: String = "\(accountJSON["privateKeyString"])"
|
||||
let privateKeyString: Data = Data(privateKeyStringString.utf8)
|
||||
let deviceModel: String = "\(accountJSON["deviceModel"])"
|
||||
|
||||
let url = "\(accountJSON["account"]["RestURL"])api/mobile/register/hebe"
|
||||
var request = URLRequest(url: URL(string: url)!)
|
||||
request.httpMethod = "GET"
|
||||
|
||||
let now = Date()
|
||||
var vDate: String {
|
||||
let dateFormatter = DateFormatter()
|
||||
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss"
|
||||
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
|
||||
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
|
||||
|
||||
return "\(dateFormatter.string(from: now)) GMT"
|
||||
}
|
||||
|
||||
let signatures = getSignatures(request: request, fingerprint: fingerprint, privateKeyString: privateKeyString)
|
||||
request.setValue("\(signatures)", forHTTPHeaderField: "Signature")
|
||||
|
||||
request.allHTTPHeaderFields = [
|
||||
"User-Agent": "wulkanowy/1 CFNetwork/1220.1 Darwin/20.1.0",
|
||||
"vOS": "iOS",
|
||||
"vDeviceModel": deviceModel,
|
||||
"vAPI": "1",
|
||||
"vDate": vDate,
|
||||
"vCanonicalUrl": "api%2fmobile%2fregister%2fhebe"
|
||||
]
|
||||
|
||||
let session = URLSession.shared
|
||||
let task = session.dataTask(with: request) { (data, response, error) in
|
||||
if let error = error {
|
||||
// Handle HTTP request error
|
||||
print(error)
|
||||
} else if let data = data {
|
||||
// Handle HTTP request response
|
||||
let responseBody = String(data: data, encoding: String.Encoding.utf8)
|
||||
|
||||
let keychain = Keychain()
|
||||
keychain["students"] = responseBody!
|
||||
|
||||
} else {
|
||||
// Handle unexpected error
|
||||
}
|
||||
}
|
||||
|
||||
task.resume()
|
||||
}
|
||||
|
|
@ -161,6 +161,7 @@ public class Sdk {
|
|||
|
||||
let saveAccount = """
|
||||
{
|
||||
"acctualStudent": "0",
|
||||
"privateKeyString": "\(privateKeyStringString)",
|
||||
"fingerprint": "\(fingerprint)",
|
||||
"deviceModel": "\(deviceModel)",
|
||||
|
@ -172,6 +173,7 @@ public class Sdk {
|
|||
}
|
||||
}
|
||||
"""
|
||||
|
||||
keychain["\(receiveValueJSON["Envelope"]["LoginId"])"] = "\(saveAccount)"
|
||||
let allAccounts = keychain["allAccounts"] ?? "[]"
|
||||
let data = Data(allAccounts.utf8)
|
||||
|
@ -182,7 +184,27 @@ public class Sdk {
|
|||
} catch {
|
||||
print(error)
|
||||
}
|
||||
hebeRequest(id: "\(receiveValueJSON["Envelope"]["LoginId"])")
|
||||
|
||||
keychain["acctualStudentId"] = "\(receiveValueJSON["Envelope"]["LoginId"])"
|
||||
|
||||
let endpointURL: String = "\(receiveValueJSON["Envelope"]["RestURL"])api/mobile/register/hebe"
|
||||
|
||||
let apiResponseRequest = apiRequest(endpointURL: endpointURL)
|
||||
let session = URLSession.shared
|
||||
session.dataTask(with: apiResponseRequest) { (data, response, error) in
|
||||
if let error = error {
|
||||
// Handle HTTP request error
|
||||
print(error)
|
||||
} else if let data = data {
|
||||
// Handle HTTP request response
|
||||
let responseBody = String(data: data, encoding: String.Encoding.utf8)
|
||||
|
||||
keychain["acctualStudentHebe"] = "\(responseBody!)"
|
||||
|
||||
} else {
|
||||
// Handle unexpected error
|
||||
}
|
||||
}.resume()
|
||||
completionHandler(nil)
|
||||
}
|
||||
})
|
||||
|
|
|
@ -7,10 +7,40 @@
|
|||
|
||||
import Foundation
|
||||
import KeychainAccess
|
||||
import SwiftyJSON
|
||||
|
||||
@available (iOS 14, macOS 11, watchOS 7, tvOS 14, *)
|
||||
public func getLuckyNumber() -> Int {
|
||||
/*let keychain = Keychain()
|
||||
endpointURL = keychain["endpointURL"]
|
||||
luckyNumber = apiRequest(endpointURL: endpointURL)*/
|
||||
let keychain = Keychain()
|
||||
let acctualStudent = keychain["acctualStudent"] ?? "{}"
|
||||
let data: Data = Data(acctualStudent.utf8)
|
||||
let acctualStudentJSON = try! JSON(data: data)
|
||||
let Id = "\(acctualStudentJSON["ConstituentUnit"]["Id"])"
|
||||
|
||||
let date = Date()
|
||||
let formatter = DateFormatter()
|
||||
formatter.dateFormat = "yyyy-MM-dd"
|
||||
let day = formatter.string(from: date)
|
||||
|
||||
let RestURL = "\(acctualStudentJSON["Unit"]["RestURL"])/mobile/school/lucky?constituentId=\(Id)&day=\(day)"
|
||||
|
||||
let apiResponseRequest = apiRequest(endpointURL: RestURL)
|
||||
|
||||
let session = URLSession.shared
|
||||
session.dataTask(with: apiResponseRequest) { (data, response, error) in
|
||||
if let error = error {
|
||||
// Handle HTTP request error
|
||||
print(error)
|
||||
} else if let data = data {
|
||||
// Handle HTTP request response
|
||||
let responseBody = String(data: data, encoding: String.Encoding.utf8)
|
||||
|
||||
print(responseBody!)
|
||||
|
||||
} else {
|
||||
// Handle unexpected error
|
||||
}
|
||||
}.resume()
|
||||
|
||||
return 7
|
||||
}
|
||||
|
|
|
@ -8,9 +8,40 @@
|
|||
import SwiftUI
|
||||
import Sdk
|
||||
import Combine
|
||||
import SwiftyJSON
|
||||
import KeychainAccess
|
||||
|
||||
@main
|
||||
struct wulkanowyApp: App {
|
||||
@AppStorage("isLogged") private var isLogged: Bool = false
|
||||
|
||||
init() {
|
||||
if(isLogged == true) {
|
||||
let keychain = Keychain()
|
||||
let acctualStudentId = "\(keychain["acctualStudentId"] ?? "0")"
|
||||
let accountString = keychain[acctualStudentId]
|
||||
let data: Data = Data(accountString!.utf8)
|
||||
let account = try! JSON(data: data)
|
||||
|
||||
let endpointURL: String = "\(account["account"]["RestURL"])api/mobile/register/hebe"
|
||||
|
||||
let apiResponseRequest = apiRequest(endpointURL: endpointURL)
|
||||
let session = URLSession.shared
|
||||
session.dataTask(with: apiResponseRequest) { (data, response, error) in
|
||||
if let error = error {
|
||||
// Handle HTTP request error
|
||||
print(error)
|
||||
} else if let data = data {
|
||||
// Handle HTTP request response
|
||||
let responseBody = String(data: data, encoding: String.Encoding.utf8)
|
||||
keychain["acctualStudentHebe"] = "\(responseBody!)"
|
||||
} else {
|
||||
// Handle unexpected error
|
||||
}
|
||||
}.resume()
|
||||
}
|
||||
}
|
||||
|
||||
var body: some Scene {
|
||||
WindowGroup {
|
||||
NavigationBarView()
|
||||
|
|
|
@ -16,28 +16,28 @@ struct AccountManagerView: View {
|
|||
|
||||
|
||||
private func getStudentsNames() -> [String] {
|
||||
//getting allStudentsKeys
|
||||
//getting all accounts
|
||||
let keychain = Keychain()
|
||||
let allStudentsKeys: String! = keychain["allStudentsKeys"] ?? "[]"
|
||||
let allAccounts: String! = keychain["allAccounts"] ?? "[]"
|
||||
|
||||
//parsing allStudentsKeys to array
|
||||
var allStudents: [String] = []
|
||||
if(allStudentsKeys != "[]"){
|
||||
let data = Data(allStudentsKeys.utf8)
|
||||
//parsing allAccounts to array
|
||||
var allAccountsArray: [String] = []
|
||||
if(allAccounts != "[]"){
|
||||
let data = Data(allAccounts.utf8)
|
||||
do {
|
||||
let keys = try JSONSerialization.jsonObject(with: data) as! [String]
|
||||
for key in keys {
|
||||
let student = keychain["student-\(key)"]
|
||||
let ids = try JSONSerialization.jsonObject(with: data) as! [String]
|
||||
for id in ids {
|
||||
let student = keychain[id]
|
||||
let data = Data(student!.utf8)
|
||||
let studentParsed = try! JSON(data: data)
|
||||
allStudents.append("\(studentParsed["Login"]["DisplayName"])")
|
||||
let accountParsed = try! JSON(data: data)
|
||||
allAccountsArray.append("\(accountParsed["account"]["UserLogin"])")
|
||||
}
|
||||
} catch {
|
||||
print(error)
|
||||
}
|
||||
}
|
||||
|
||||
return allStudents
|
||||
return allAccountsArray
|
||||
}
|
||||
|
||||
private func addAccount() {
|
||||
|
@ -66,11 +66,11 @@ struct AccountManagerView: View {
|
|||
Spacer()
|
||||
let image = Image(systemName: "pencil")
|
||||
Button("\(image)") { openEditAccount() }
|
||||
.sheet(isPresented: $showEditAccountModal, onDismiss: {
|
||||
print(self.showEditAccountModal)
|
||||
}) {
|
||||
EditAccountView()
|
||||
}
|
||||
.sheet(isPresented: $showEditAccountModal, onDismiss: {
|
||||
print(self.showEditAccountModal)
|
||||
}) {
|
||||
EditAccountView()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,17 +10,20 @@ import KeychainAccess
|
|||
import Sdk
|
||||
|
||||
struct DashboardView: View {
|
||||
init() {
|
||||
let keychain = Keychain()
|
||||
let key = keychain["privateKey"]
|
||||
|
||||
let luckyNumber = getLuckyNumber()
|
||||
print(luckyNumber)
|
||||
|
||||
}
|
||||
@State private var showModal = false
|
||||
@AppStorage("isLogged") private var isLogged: Bool = false
|
||||
|
||||
init() {
|
||||
let keychain = Keychain()
|
||||
|
||||
if(isLogged == true){
|
||||
let luckyNumber = getLuckyNumber()
|
||||
|
||||
print(luckyNumber)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
if(isLogged == false){
|
||||
VStack {
|
||||
|
|
|
@ -20,9 +20,9 @@ struct ChooseStudentView: View {
|
|||
@State private var selectedStudent: String = ""
|
||||
|
||||
init() {
|
||||
var responseBody = keychain["students"]
|
||||
var responseBody = keychain["acctualStudentHebe"]
|
||||
while responseBody == nil {
|
||||
responseBody = keychain["students"]
|
||||
responseBody = keychain["acctualStudentHebe"]
|
||||
}
|
||||
|
||||
let data = Data(responseBody!.utf8)
|
||||
|
@ -43,7 +43,7 @@ struct ChooseStudentView: View {
|
|||
|
||||
|
||||
private func saveStudent() {
|
||||
let responseBody = keychain["students"]
|
||||
let responseBody = keychain["acctualStudentHebe"]
|
||||
|
||||
let data = Data(responseBody!.utf8)
|
||||
let json = try! JSON(data: data)
|
||||
|
@ -55,27 +55,8 @@ struct ChooseStudentView: View {
|
|||
while true {
|
||||
let student = "\(json["Envelope"][i]["Login"]["DisplayName"])"
|
||||
if(student == selectedStudent) {
|
||||
|
||||
//adding student key to allStudentsKeys
|
||||
let keyFingerprint: String! = keychain["keyFingerprint"]
|
||||
let allStudentsKeys: String! = keychain["allStudentsKeys"] ?? ""
|
||||
|
||||
//parsing allStudentsKeys to array
|
||||
var allStudents: [String] = []
|
||||
let data = Data(allStudentsKeys!.utf8)
|
||||
do {
|
||||
let array = try JSONSerialization.jsonObject(with: data) as! [String]
|
||||
allStudents = array
|
||||
} catch {
|
||||
print(error)
|
||||
}
|
||||
allStudents.append(keyFingerprint!)
|
||||
keychain["allStudentsKeys"] = "\(allStudents)"
|
||||
|
||||
|
||||
//saving student
|
||||
keychain["student-\(String(describing: keyFingerprint!))"] = "\(json["Envelope"][i])"
|
||||
|
||||
keychain["acctualStudent"] = "\(json["Envelope"][i])"
|
||||
break
|
||||
}
|
||||
i += 1
|
||||
|
|
Loading…
Reference in a new issue