Skip to content

Instantly share code, notes, and snippets.

@bessem
bessem / rest_call_use_case.swift
Created May 14, 2019 12:51
/// Rest Call use case
fileprivate func handleResponse(_ result: Result<NearEarthListCall.T, GenericError>) {
stopLoading()
switch result {
case .success(_):
do {
try self.nearEarthListVM = result.get()
}
catch (let err) {
print(err) /// popup or whatever you use !! beware of cancel ;)
@bessem
bessem / rest_call_use_case.swift
Created May 14, 2019 12:51
/// Rest Call use case
fileprivate func handleResponse(_ result: Result<NearEarthListCall.T, GenericError>) {
stopLoading()
switch result {
case .success(_):
do {
try self.nearEarthListVM = result.get()
}
catch (let err) {
print(err) /// popup or whatever you use !! beware of cancel ;)
@bessem
bessem / NearEarthListService.swift
Created May 14, 2019 12:43
/// Service example NearEarthListService
class NearEarthListService {
static let shared = NearEarthListService()
//restricted, yep singleton
private init(){}
/// lazy var & lazy me
lazy var urlSession: URLSession = {
@bessem
bessem / NearEarthListCall.swift
Created May 14, 2019 12:41
/// example of RestCall
class NearEarthListCall : RestCall {
var mimeTypeData: [MimeType]? = nil
var httpMethod: HttpMethod = .GET
var contentType: ContentType = .json
var callRoot: CallRoot {
return CallRoot(scheme: "https",
@bessem
bessem / ex_RestCall.swift
Last active May 14, 2019 12:18
/// Execution Abstract
/// Execution
extension RestCall {
/**
This method execute and return the task back, developper can use the task ref to cancel it, pause it, resume it back...
- Remark: The result back to the user on main thread
- Parameter session: **URLSession** the connection that will create the task that will be performed
- Parameter completion: **Result** The result of the execution an enum <success, fail>.
- Returns: The URLSessionDataTask to be executed
@bessem
bessem / RestCall.swift
Created May 14, 2019 09:41
/** protocol RestCall serve as a base Http https calls execution wrapper */
protocol RestCall {
///responseBodyType `T` must be `Codable` , it will be used when parsing and creating web service response from json
associatedtype T: Codable
///callInterceptor `RestCallInterceptor` used to intercept the url request
var callInterceptor : RestCallInterceptor { get }
/// The call `method`, le swift : -"all, what are u ?"
var httpMethod : HttpMethod { get }
@bessem
bessem / MimeType.swift
Created May 14, 2019 09:33
/** # An easy to use MimeType wrapper ## MimeType 1. filename image.jpg , image.png ... 2. mimeType image/jpg , image/png ... 3. data it can be some UIImageJPEGRepresentation or any other data ... */
struct MimeType {
let filename: String
let mimeType : String
let data: Data
}
@bessem
bessem / ContentType.swift
Created May 14, 2019 09:29
/** # Enum for basic content type ## ContentType - application/json - multipart/form-data - application/x-www-form-urlencoded */
enum ContentType: String {
case json = "application/json"
case multipart = "multipart/form-data"
case urlencoded = "application/x-www-form-urlencoded"
}
@bessem
bessem / CallRoot.swift
Last active May 14, 2019 09:25
/** CallRoot 1. Scheme (Http, Https...) 2. Host also know as base url 3. Path also called end point 4. QueryItems (i.e) : ?q=search&detaild=true... */
struct CallRoot {
let scheme: String
let host : String
let path: String
let queryItems: [URLQueryItem]?
}
@bessem
bessem / HttpMethod.swift
Created May 14, 2019 09:22
/** # Enum for basic http methods ## HttpMethod - GET - POST - UPDATE - DELETE */
enum HttpMethod: String {
case GET
case POST
case UPDATE
case DELETE
}