[+] Implement send request
This commit is contained in:
@@ -2,12 +2,10 @@ package org.hydev.ios.alarmclock
|
|||||||
|
|
||||||
import org.springframework.http.ResponseEntity
|
import org.springframework.http.ResponseEntity
|
||||||
import java.security.SecureRandom
|
import java.security.SecureRandom
|
||||||
import javax.crypto.SecretKeyFactory
|
|
||||||
|
|
||||||
import javax.crypto.spec.PBEKeySpec
|
|
||||||
|
|
||||||
import java.security.spec.KeySpec
|
import java.security.spec.KeySpec
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
import javax.crypto.SecretKeyFactory
|
||||||
|
import javax.crypto.spec.PBEKeySpec
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate "Bad Request" response entity
|
* Generate "Bad Request" response entity
|
||||||
@@ -26,7 +24,7 @@ fun bad(msg: String): ResponseEntity<String> = ResponseEntity.badRequest().body(
|
|||||||
fun randSalt(len: Int = 16): ByteArray
|
fun randSalt(len: Int = 16): ByteArray
|
||||||
{
|
{
|
||||||
val random = SecureRandom()
|
val random = SecureRandom()
|
||||||
val salt = ByteArray(16)
|
val salt = ByteArray(len)
|
||||||
random.nextBytes(salt)
|
random.nextBytes(salt)
|
||||||
return salt
|
return salt
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
struct User
|
struct User: Decodable
|
||||||
{
|
{
|
||||||
var id: Int
|
var id: Int
|
||||||
var name: String
|
var name: String
|
||||||
|
|||||||
+26
-9
@@ -8,6 +8,7 @@ import Foundation
|
|||||||
|
|
||||||
/// Base URL of the HTTP server
|
/// Base URL of the HTTP server
|
||||||
let baseUrl = "http://localhost:8080/api" // TODO: Production settings
|
let baseUrl = "http://localhost:8080/api" // TODO: Production settings
|
||||||
|
let JSON = JSONDecoder()
|
||||||
|
|
||||||
/// API class
|
/// API class
|
||||||
struct API<T>
|
struct API<T>
|
||||||
@@ -57,20 +58,36 @@ func createUrl(_ node: String, _ params: [String: String]? = [:]) -> URL
|
|||||||
return url!.url!
|
return url!.url!
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Send a HTTP request
|
/**
|
||||||
func send<T>(_ api: API<T>, // API Node
|
Send a HTTP request
|
||||||
_ params: [String: String]? = [:], // Parameters to send to the server
|
|
||||||
_ success: @escaping (String) -> Void, // What to do when success
|
- Parameter api: API Node (Eg. APIs.register)
|
||||||
err: @escaping (String) -> Void = {it in} // What to do when errors happen
|
- Parameter params: Parameters to send to the server (Check the documentation of the API node to see which parameters you need)
|
||||||
)
|
- Parameter success: Callback of what to do when the request successfully returned
|
||||||
|
- Parameter err: Callback of what to do when an error happens
|
||||||
|
*/
|
||||||
|
func send<T: Decodable>(_ api: API<T>, _ params: [String: String]? = [:], _ success: @escaping (T) -> Void, err: @escaping (String) -> Void = {it in})
|
||||||
{
|
{
|
||||||
let url = createUrl(api.loc, params)
|
let url = createUrl(api.loc, params)
|
||||||
|
|
||||||
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
|
// Create task
|
||||||
guard let data = data, let body = String(data: data, encoding: .utf8) else { err("Data cannot be parsed"); return }
|
let task = URLSession.shared.dataTask(with: url) { (raw, response, error) in
|
||||||
|
|
||||||
success(body)
|
// Check if raw data exists
|
||||||
|
guard let response = response as? HTTPURLResponse, let raw = raw else { err("Data doesn't exist"); return }
|
||||||
|
|
||||||
|
// If success
|
||||||
|
if (200...299).contains(response.statusCode)
|
||||||
|
{
|
||||||
|
// Parse JSON
|
||||||
|
guard let obj = try? JSON.decode(T.self, from: raw) else { err("JSON cannot be parsed"); return }
|
||||||
|
|
||||||
|
// Call callback
|
||||||
|
success(obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Execute task
|
||||||
task.resume()
|
task.resume()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user