- Unwrap SSOAuthToken. Else, handle error.
- Create request from webservice and SSOAuthToken.
- Make a GET request.
- Async-wait for response
- Unwrap response data.
- If GET request returned 200-299, request was success. If failed, handle.
- When successful, convert the return JSON data. If failed, handle.
- Get Enterprise Names and enterpriseGUUID string from JSON data.
- Add the enterpriseGUUID string to a Dictionary using the Enterprise Names as the Key
- Call completion callback meothd with parameter "Success"
public func getListOfEnterprisesAvailable(currentSessionInfo: SessionInfo, completion: @escaping (_ response: ReturnInfoWithDictionary) -> Void){
let SSOAuthToken = currentSessionInfo.SSOAuthToken!
var returnInfo = ReturnInfoWithDictionary()
var enterpriseDictionary = Dictionary<String, String>()
let GetSSOEnterprisesService = "https://services.etosoftware.com/API/Security.svc/GetSSOEnterprises/\(SSOAuthToken)"
let request = Alamofire.request(GetSSOEnterprisesService, method: .get, parameters: nil, encoding: URLEncoding.default).validate().responseJSON { response in
switch(response.result){
case .success:
if let jsonData = response.data {
let enterprisesList = JSON(data: jsonData)
for i in 0..<enterprisesList.count {
let key = enterprisesList[i]["Key"].string!
let value = enterprisesList[i]["Value"].string!
enterpriseDictionary.updateValue(key, forKey: value)
}
returnInfo.dictionary = enterpriseDictionary
returnInfo.callback = self.prepareResponse(targetResponse: .Success)
completion(returnInfo)
} else {
returnInfo.callback = self.prepareResponse(targetResponse: .FailedToGetEnterpriseList)
completion(returnInfo)
}
break;
case .failure:
returnInfo.callback = self.prepareResponse(targetResponse: .HTTPRequestFail)
completion(returnInfo)
break;
}
}
if(DebugMode){
debugPrint(request)
}
}
public func setEnterprise(currentSessionInfo: SessionInfo, selectedEnterprise: String){
let etoUserDefaults = ETOUserDefaults()
etoUserDefaults.setUserDefault(key: .enterpriseID, value: selectedEnterprise)
currentSessionInfo.setEnterprise(chosenEnterprise: selectedEnterprise)
}
- Unwrap SSOAuthToken. Else, handle error.
- Unwrap selectedEnterpriseGuuid. Else, handle error.
- Create request from webservice URL, SSOAuthToken, Enterprise GUUID.
- Make a GET request.
- Async-wait for response
- Unwrap response data.
- If GET request returned 200-299, request was success. If failed, handle.
- When successful, convert the return JSON data. If failed, handle.
- Get Site Name and Site Number from JSON data.
- Add the Site Number to a Dictionary using the Site Name as the Key
- Call completion callback meothd with parameter "Success"
public func getListOfSites(currentSessionInfo: SessionInfo, completion: @escaping (_ response: ReturnInfoWithDictionary) -> Void){
var returnInfo = ReturnInfoWithDictionary()
let SSOAuthToken = currentSessionInfo.SSOAuthToken ?? ""
let selectedEnterpriseGuuid = currentSessionInfo.selectedEnterprise()
if "" == SSOAuthToken {
returnInfo.callback = prepareResponse(targetResponse: .NoSSOAuthToken)
completion(returnInfo)
} else if "" == selectedEnterpriseGuuid {
returnInfo.callback = prepareResponse(targetResponse: .NoEnterpriseIDSelected)
completion(returnInfo)
} else {
let GetSSOSitesService = "https://services.etosoftware.com/API/Security.svc/GetSSOSites/\(SSOAuthToken)/\(selectedEnterpriseGuuid)"
let request = Alamofire.request(GetSSOSitesService, method: .get, parameters: nil, encoding: URLEncoding.default).validate().responseJSON { response in
switch(response.result){
case .success:
if let jsonData = response.data {
let siteList = JSON(data: jsonData)
for i in 0..<siteList.count {
let keyInt = siteList[i]["Key"].int!
let key = String(keyInt)
let value = siteList[i]["Value"].string!
returnInfo.dictionary.updateValue(key, forKey: value)
}
returnInfo.callback = self.prepareResponse(targetResponse: .Success)
completion(returnInfo)
} else {
returnInfo.callback = self.prepareResponse(targetResponse: .FailedToGetSiteList)
completion(returnInfo)
}
break;
case .failure:
returnInfo.callback = self.prepareResponse(targetResponse: .HTTPRequestFail)
completion(returnInfo)
break;
}
}
if(DebugMode){
debugPrint(request)
}
}
}
- Unwrap SSOAuthToken. Else, handle error.
- Unwrap selectedEnterpriseGuuid. Else, handle error.
- Create request from webservice, SSOAuthToken, and Enterprise GUUID.
- Make a GET request.
- Async-wait for response
- Unwrap response data.
- If GET request returned 200-299, request was success. If failed, handle.
- When successful, convert the returned JSON data. If failed, handle.
- Assign the Site sessionSecurityToken from the token in JSON data.
- Call completion callback method with parameter "Success"
public func setSelectedSite(currentSessionInfo: SessionInfo, completion: @escaping (_ response: ReturnInfoWithString)-> Void){
let etoUserDefault = ETOUserDefaults()
var returnInfo = ReturnInfoWithString()
let SSOAuthToken = currentSessionInfo.SSOAuthToken ?? ""
let selectedEnterpriseGuuid = currentSessionInfo.selectedEnterprise()
let selectedSite = currentSessionInfo.selectedSite()
etoUserDefault.setUserDefault(key: .siteID, value: selectedSite)
var secondsFromGMT: Int { return NSTimeZone.local.secondsFromGMT() }
let utcOffsetInHours = String(secondsFromGMT / 60)
if "" == SSOAuthToken {
returnInfo.callback = prepareResponse(targetResponse: .NoSSOAuthToken)
completion(returnInfo)
} else if "" == selectedEnterpriseGuuid {
returnInfo.callback = prepareResponse(targetResponse: .NoEnterpriseIDSelected)
completion(returnInfo)
} else if "" == utcOffsetInHours {
returnInfo.callback = prepareResponse(targetResponse: .InvalidUTC)
completion(returnInfo)
} else {
let body = "https://services.etosoftware.com/API/Security.svc/SSOSiteLogin/\(selectedSite)/\(selectedEnterpriseGuuid)/\(SSOAuthToken)/"
+ utcOffsetInHours
let request = Alamofire.request(body, method: .get, encoding: URLEncoding.default).validate().responseJSON { response in
switch(response.result){
case .success:
if let jsonData = response.data {
let sessionSecurityToken = JSON(data: jsonData)
currentSessionInfo.sessionSecurityToken = sessionSecurityToken.string!
returnInfo.value = sessionSecurityToken.string!
returnInfo.callback = self.prepareResponse(targetResponse: .Success)
completion(returnInfo)
} else {
returnInfo.callback = self.prepareResponse(targetResponse: .FailedToGetSiteList)
completion(returnInfo)
}
break;
case .failure:
returnInfo.callback = self.prepareResponse(targetResponse: .HTTPRequestFail)
completion(returnInfo)
break;
}
}
if(DebugMode){
debugPrint(request)
}
}
}
- Unwrap selectedEnterpriseGuuid. Else, handle error.
- Create request from webservice URL, sessionSecurityToken, and EnterpriseGUUID
- Make a GET request.
- Async-wait for response
- Unwrap response data.
- If GET request returned 200-299, request was success. If failed, handle.
- When successful, convert the return JSON data. If failed, handle.
- Get Program Name and Program Number from JSON data.
- Add the Program Number to a Dictionary using the Program Name as the Key
- Call completion callback meothd with parameter "Success"
public func getListOfPrograms(currentSessionInfo: SessionInfo, completion: @escaping (_ response: ReturnInfoWithDictionary) -> Void){
var returnInfo = ReturnInfoWithDictionary()
let headers = headersWithEnterpriseIDAndSecurityToken(currentSessionInfo: sessionInfo)
let selectedSite = currentSessionInfo.selectedSite()
if "" == currentSessionInfo.SSOAuthToken {
returnInfo.callback = prepareResponse(targetResponse: .NoSSOAuthToken)
completion(returnInfo)
} else if "" == currentSessionInfo.selectedEnterprise() {
returnInfo.callback = prepareResponse(targetResponse: .NoEnterpriseIDSelected)
completion(returnInfo)
} else if "" == selectedSite {
returnInfo.callback = prepareResponse(targetResponse: .NoSiteIDSelected)
completion(returnInfo)
} else {
let body = "https://services.etosoftware.com/API/Form.svc/Forms/Program/GetPrograms/" + selectedSite
let request = Alamofire.request(body, method: .get, encoding: URLEncoding.default, headers: headers).validate().responseJSON { response in
switch(response.result){
case .success:
if let jsonData = response.data {
let programList = JSON(data: jsonData).arrayValue
for i in 0..<programList.count {
let programIDInt = programList[i]["ID"].int
let disabled = programList[i]["Disabled"].boolValue //TODO: Make an option.
if !disabled {
if let programIDInt = programIDInt {
returnInfo.dictionary.updateValue(String(programIDInt), forKey: programList[i]["Name"].string!)
}
}
}
currentSessionInfo.programsDictionary = returnInfo.dictionary
returnInfo.callback = self.prepareResponse(targetResponse: .Success)
completion(returnInfo)
} else {
returnInfo.callback = self.prepareResponse(targetResponse: .FailedToGetSiteList)
}
break;
case .failure:
returnInfo.callback = self.prepareResponse(targetResponse: .HTTPRequestFail)
completion(returnInfo)
break;
}
}
if(DebugMode){
debugPrint(request)
}
}
}
- Unwrap selectedProgram. Else, handle error.
- Unwrap selectedEnterpriseGuuid. Else, handle error.
- Create request from webservice, selectedProgram, SSOAuthToken, and Enterprise GUUID.
- Make a GET request.
- Async-wait for response
- Unwrap response data.
- If GET request returned 200-299, request was success. If failed, handle.
-
When successful, convert the returned JSON data. If failed, handle.
-
Call completion callback method with parameter "Success"
public func setSelectedProgram(currentSessionInfo: SessionInfo, setToProgramID: String,completion: @escaping (_ response: ReturnInfoWithJSON) -> Void){
var etoUserDefaults = ETOUserDefaults()
var returnInfo: ReturnInfoWithJSON = ReturnInfoWithJSON()
etoUserDefaults.setUserDefault(key: .programID, value: setToProgramID)
if "" == currentSessionInfo.SSOAuthToken {
returnInfo.callback = prepareResponse(targetResponse: .NoSSOAuthToken)
completion(returnInfo)
} else if "" == currentSessionInfo.selectedEnterprise() {
returnInfo.callback = prepareResponse(targetResponse: .NoEnterpriseIDSelected)
completion(returnInfo)
} else if "" == setToProgramID {
returnInfo.callback = prepareResponse(targetResponse: .NoProgramIDSelected)
completion(returnInfo)
} else {
let parameters: Parameters = [
"ProgramID": setToProgramID
];
let headers = headersWithEnterpriseIDAndSecurityToken(currentSessionInfo: currentSessionInfo)
let body = "https://services.etosoftware.com/API/Security.svc/UpdateCurrentProgram/"
let request = Alamofire.request(body, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).validate().responseJSON { response in
switch(response.result){
case .success:
if let responseData = response.data {
let jsonData = JSON(data: responseData)
currentSessionInfo.selectedProgramInfo = jsonData
currentSessionInfo.setProgram(chosenProgram: setToProgramID)
returnInfo.json = jsonData
returnInfo.callback = self.prepareResponse(targetResponse: .Success)
completion(returnInfo)
} else {
returnInfo.callback = self.prepareResponse(targetResponse: .FailedToGetSiteList)
}
break;
case .failure:
returnInfo.callback = self.prepareResponse(targetResponse: .HTTPRequestFail)
completion(returnInfo)
break;
}
}
if(DebugMode){
debugPrint(request)
}
}
}
public func headersWithEnterpriseIDAndSecurityToken(currentSessionInfo: SessionInfo) -> HTTPHeaders{
let selectedEnterpriseGuuid = currentSessionInfo.selectedEnterprise()
let sessionSecurityToken = currentSessionInfo.sessionSecurityToken
let headers: HTTPHeaders = [
"Content-Type": "application/json",
"Accept": "application/json",
"enterpriseGuid": selectedEnterpriseGuuid,
"securityToken": sessionSecurityToken,
]
return headers
}
public func sortDictionary(dictionaryToSort: Dictionary<String, String>) -> Dictionary<String, String>{
var dictionaryToReturn = Dictionary<String, String>()
let sortedKeys = Array(dictionaryToSort.keys).sorted()
for key in sortedKeys {
if let valueForThisKey = dictionaryToSort[key] {
dictionaryToReturn.updateValue(valueForThisKey, forKey: key)
}
}
return dictionaryToReturn
}
private func prepareResponse(targetResponse: AuthenticationServiceResponseTypes) -> AuthenticationServiceResponse {
var candidateResponse: AuthenticationServiceResponse = AuthenticationServiceResponse()
candidateResponse.responseType = targetResponse
candidateResponse.responseMessage = AuthenticationServiceResponseMessages[targetResponse]!
return candidateResponse
}
public struct AuthenticationServiceResponse {
var responseType: AuthenticationServiceResponseTypes
var responseMessage: String
init(){
self.responseType = .UnknownFailure
self.responseMessage = AuthenticationServiceResponseMessages[.UnknownFailure]!
}
}
public struct ReturnInfoWithString {
var value: String
var callback: AuthenticationServiceResponse
init(){
self.value = ""
self.callback = AuthenticationServiceResponse()
}
}
public struct ReturnInfoWithDictionary {
var dictionary: Dictionary<String, String> = Dictionary<String, String>()
var callback: AuthenticationServiceResponse
init(){
self.dictionary = Dictionary<String, String>()
self.callback = AuthenticationServiceResponse()
}
}
public struct ReturnInfoWithJSON {
var json: JSON?
var callback: AuthenticationServiceResponse
init(){
self.json = JSON(["":""])
self.callback = AuthenticationServiceResponse()
}
}
public enum AuthenticationServiceResponseTypes {
case
Success,
NoSSOAuthToken,
HTTPRequestFail,
FailedToGetEnterpriseList,
FailedToGetSiteList,
InvalidUTC,
NoEnterpriseIDSelected,
UnknownFailure,
NoSiteIDSelected,
NoProgramIDSelected,
failedToConnectToServer
init(){
self = .UnknownFailure
}
}
public let AuthenticationServiceResponseMessages: Dictionary<AuthenticationServiceResponseTypes, String> = [
.Success : "Success",
.NoSSOAuthToken : "Authorization denied.",
.HTTPRequestFail : "Failed to get information from server.",
.FailedToGetEnterpriseList : "Failed to get enterprise list.",
.FailedToGetSiteList : "Failed to get site list.",
.InvalidUTC : "UTC Offset invalid.",
.NoEnterpriseIDSelected : "No enterprise selected.",
.UnknownFailure : "An unknown failure has occurred.",
.NoSiteIDSelected: "No site selected.",
.NoProgramIDSelected: "No program selected.",
.failedToConnectToServer : "Failled to connect to server."
]