-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finished sign up, working on networking.
- Loading branch information
Elijah Cobb
committed
Jul 9, 2019
1 parent
3017217
commit fc07fa4
Showing
7 changed files
with
393 additions
and
221 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// | ||
// API.swift | ||
// subscribeto | ||
// | ||
// Created by Elijah Cobb on 9/07/19. | ||
// Copyright © 2019 subscribeto. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
struct S2NetworkError { | ||
let code: Int | ||
let msg: String | ||
} | ||
|
||
struct S2Request { | ||
|
||
enum Method: String { | ||
case get = "GET" | ||
case post = "POST" | ||
case delete = "DELETE" | ||
case put = "PUT" | ||
} | ||
|
||
enum Token { | ||
case session | ||
case totp(String) | ||
case email(String) | ||
case sms(String) | ||
} | ||
|
||
static let baseUrl = "http://api.scribe.to" | ||
static var sessionToken: String? | ||
|
||
var method: Method | ||
var endpoint: String | ||
var token: Token | ||
|
||
func makeRequest(_ handler: @escaping (Data?, S2NetworkError?) -> Void) { | ||
|
||
let session = URLSession.init() | ||
guard let url = URL.init(string: S2Request.baseUrl + "/" + endpoint) else { return handler(nil, S2NetworkError(code: -1, msg: "The url created was invalid.")) } | ||
let urlRequest = URLRequest.init(url: url) | ||
|
||
session.dataTask(with: urlRequest) { (data: Data?, response: URLResponse?, error: Error?) in | ||
|
||
if data != nil { handler(data, nil) } | ||
else { | ||
|
||
guard let err = error else { | ||
return handler(nil, S2NetworkError(code: -1, msg: "Both ")) | ||
} | ||
|
||
handler(nil, S2NetworkError(code: -1, msg: err.localizedDescription)) | ||
|
||
} | ||
|
||
} | ||
|
||
} | ||
|
||
} |
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
Oops, something went wrong.