This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct User: ResponseObjectSerializable, ResponseCollectionSerializable, CustomStringConvertible { | |
let username: String | |
let name: String | |
var description: String { | |
return "User: { username: \(username), name: \(name) }" | |
} | |
init?(response: HTTPURLResponse, representation: Any) { | |
guard |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension DataRequest{ | |
/// @Returns - DataRequest | |
/// completionHandler handles JSON Object T | |
@discardableResult func responseObject<T: Decodable> ( | |
queue: DispatchQueue? = nil , | |
completionHandler: @escaping (DataResponse<T>) -> Void ) -> Self{ | |
let responseSerializer = DataResponseSerializer<T> { request, response, data, error in | |
guard error == nil else {return .failure(BackendError.network(error: error!))} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct User: Decodable, CustomStringConvertible { | |
let username: String | |
let name: String | |
/// This is the key part | |
/// If parameters and variable name differ | |
/// you can specify custom key for mapping "eg. 'user_name'" | |
enum CodingKeys: String, CodingKey { | |
case username = "user_name" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1 | |
Alamofire.request(Router.readUser("mattt")).responseObject{ (response: DataResponse<User>) in | |
// Process userResponse, of type DataResponse<User>: | |
if let user = response.value { | |
print("User: { username: \(user.username), name: \(user.name) }") | |
} | |
} | |
// 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.com; | |
import com.android.volley.AuthFailureError; | |
import com.android.volley.NetworkResponse; | |
import com.android.volley.ParseError; | |
import com.android.volley.Request; | |
import com.android.volley.Response; | |
import com.android.volley.VolleyError; | |
import com.android.volley.toolbox.HttpHeaderParser; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.android | |
import android.content.Context | |
import android.graphics.* | |
import android.graphics.drawable.BitmapDrawable | |
import android.graphics.drawable.ColorDrawable | |
import android.graphics.drawable.Drawable | |
import android.net.Uri | |
import android.os.Build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension DataRequest { | |
private func decodableResponseSerializer<T: Decodable>() -> DataResponseSerializer<T> { | |
return DataResponseSerializer { _, response, data, error in | |
guard error == nil else { return .failure(error!) } | |
guard let data = data else { | |
return .failure(AFError.responseSerializationFailed(reason: .inputDataNil)) | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Date { | |
// Convert local time to UTC (or GMT) | |
func toGlobalTime() -> Date { | |
let timezone = TimeZone.current | |
let seconds = -TimeInterval(timezone.secondsFromGMT(for: self)) | |
return Date(timeInterval: seconds, since: self) | |
} | |
// Convert UTC (or GMT) to local time |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// swift-tools-version:4.2 | |
// The swift-tools-version declares the minimum version of Swift required to build this package. | |
import PackageDescription | |
let package = Package( | |
name: "Swift-Blockchain", | |
products: [ | |
// Products define the executables and libraries produced by a package, and make them visible to other packages. | |
.executable( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Block: Codable { | |
var index: Int64 | |
var timestamp: Date | |
var transactions: [Transaction] | |
var proof: Int64 | |
var previous_hash: String | |
} | |
struct Transaction: Codable { | |
var sender: String |