Dwayne Harris
6 years ago
17 changed files with 259 additions and 109 deletions
-
16Package.resolved
-
28Package.swift
-
3Sources/elpha-ios/elpha_ios.swift
-
7Tests/LinuxMain.swift
-
9Tests/elpha-iosTests/XCTestManifests.swift
-
16Tests/elpha-iosTests/elpha_iosTests.swift
-
10elpha-ios.xcodeproj/project.pbxproj
-
98elpha-ios/APIRequest.swift
-
25elpha-ios/AppDelegate.swift
-
BINelpha-ios/Assets.xcassets/Globe.imageset/globe-white.pdf
-
70elpha-ios/AuthenticateViewController.swift
-
28elpha-ios/Base.lproj/LaunchScreen.storyboard
-
9elpha-ios/Base.lproj/Main.storyboard
-
2elpha-ios/Configuration.swift
-
23elpha-ios/Elpha.xcdatamodeld/Elpha.xcdatamodel/contents
-
11elpha-ios/Info.plist
-
13elpha-ios/InstanceViewController.swift
@ -1,16 +0,0 @@ |
|||
{ |
|||
"object": { |
|||
"pins": [ |
|||
{ |
|||
"package": "Kingfisher", |
|||
"repositoryURL": "https://github.com/onevcat/Kingfisher.git", |
|||
"state": { |
|||
"branch": null, |
|||
"revision": "b6d83f8520a81996738b40d3cbfdad939ceab2b3", |
|||
"version": "4.9.0" |
|||
} |
|||
} |
|||
] |
|||
}, |
|||
"version": 1 |
|||
} |
@ -1,28 +0,0 @@ |
|||
// swift-tools-version:4.0 |
|||
// The swift-tools-version declares the minimum version of Swift required to build this package. |
|||
|
|||
import PackageDescription |
|||
|
|||
let package = Package( |
|||
name: "elpha-ios", |
|||
products: [ |
|||
// Products define the executables and libraries produced by a package, and make them visible to other packages. |
|||
.library( |
|||
name: "elpha-ios", |
|||
targets: ["elpha-ios"]), |
|||
], |
|||
dependencies: [ |
|||
// Dependencies declare other packages that this package depends on. |
|||
// .package(url: /* package url */, from: "1.0.0"), |
|||
], |
|||
targets: [ |
|||
// Targets are the basic building blocks of a package. A target can define a module or a test suite. |
|||
// Targets can depend on other targets in this package, and on products in packages which this package depends on. |
|||
.target( |
|||
name: "elpha-ios", |
|||
dependencies: []), |
|||
.testTarget( |
|||
name: "elpha-iosTests", |
|||
dependencies: ["elpha-ios"]), |
|||
] |
|||
) |
@ -1,3 +0,0 @@ |
|||
struct elpha_ios { |
|||
var text = "Hello, World!" |
|||
} |
@ -1,7 +0,0 @@ |
|||
import XCTest |
|||
|
|||
import elpha_iosTests |
|||
|
|||
var tests = [XCTestCaseEntry]() |
|||
tests += elpha_iosTests.allTests() |
|||
XCTMain(tests) |
@ -1,9 +0,0 @@ |
|||
import XCTest |
|||
|
|||
#if !os(macOS) |
|||
public func allTests() -> [XCTestCaseEntry] { |
|||
return [ |
|||
testCase(elpha_iosTests.allTests), |
|||
] |
|||
} |
|||
#endif |
@ -1,16 +0,0 @@ |
|||
import XCTest |
|||
@testable import elpha_ios |
|||
|
|||
final class elpha_iosTests: XCTestCase { |
|||
func testExample() { |
|||
// This is an example of a functional test case. |
|||
// Use XCTAssert and related functions to verify your tests produce the correct |
|||
// results. |
|||
XCTAssertEqual(elpha_ios().text, "Hello, World!") |
|||
} |
|||
|
|||
|
|||
static var allTests = [ |
|||
("testExample", testExample), |
|||
] |
|||
} |
@ -0,0 +1,98 @@ |
|||
// |
|||
// APIRequest.swift |
|||
// elpha-ios |
|||
// |
|||
// Created by Dwayne Harris on 9/19/18. |
|||
// Copyright © 2018 Elpha. All rights reserved. |
|||
// |
|||
|
|||
import Foundation |
|||
|
|||
extension URLRequest { |
|||
private func percentEscapeString(_ string: String) -> String { |
|||
var characterSet = CharacterSet.alphanumerics |
|||
characterSet.insert(charactersIn: "-._* ") |
|||
|
|||
return string |
|||
.addingPercentEncoding(withAllowedCharacters: characterSet)! |
|||
.replacingOccurrences(of: " ", with: "+") |
|||
} |
|||
|
|||
mutating func encodeParameters(parameters: [String: String]) { |
|||
httpMethod = "POST" |
|||
|
|||
let parameterArray = parameters.map { (arg) -> String in |
|||
let (key, value) = arg |
|||
return "\(key)=\(self.percentEscapeString(value))" |
|||
} |
|||
|
|||
httpBody = parameterArray.joined(separator: "&").data(using: .utf8) |
|||
} |
|||
} |
|||
|
|||
class APIRequest { |
|||
static func get(url: URL, completion: @escaping ([String: Any]?, Error?) -> Void) { |
|||
let request = URLRequest(url: url) |
|||
|
|||
URLSession.shared.dataTask(with: request) { data, response, error in |
|||
guard error == nil else { |
|||
completion(nil, error) |
|||
return |
|||
} |
|||
|
|||
guard let data = data else { |
|||
completion(nil, NSError()) |
|||
return |
|||
} |
|||
|
|||
do { |
|||
completion(try JSONSerialization.jsonObject(with: data) as? [String: Any], nil) |
|||
} catch { |
|||
completion(nil, error) |
|||
} |
|||
} |
|||
} |
|||
|
|||
static func post(url: URL, data: [String: String], completion: @escaping ([String: Any]?, Error?) -> Void) { |
|||
var request = URLRequest(url: url) |
|||
request.setValue("application/json", forHTTPHeaderField: "Accept") |
|||
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type") |
|||
request.encodeParameters(parameters: data) |
|||
|
|||
URLSession.shared.dataTask(with: request) { data, response, error in |
|||
guard error == nil else { |
|||
completion(nil, error) |
|||
return |
|||
} |
|||
|
|||
guard let data = data else { |
|||
completion(nil, NSError()) |
|||
return |
|||
} |
|||
|
|||
completion(try? JSONSerialization.jsonObject(with: data) as! [String: Any], nil) |
|||
} |
|||
} |
|||
|
|||
static func handleOAuthResponse(url: URL) throws { |
|||
|
|||
} |
|||
|
|||
static func registerApp(instance: URL, completion: @escaping ([String: Any]?, Error?) -> Void) { |
|||
let data = [ |
|||
"client_name": Config.clientName, |
|||
"redirect_uris": "elpha:authenticate", |
|||
"scopes": "read write follow", |
|||
"website": Config.clientWebsite |
|||
] |
|||
|
|||
APIRequest.post(url: instance.appendingPathComponent("api/v1/apps"), data: data) { data, error in |
|||
guard error == nil else { |
|||
completion(nil, error) |
|||
return |
|||
} |
|||
|
|||
completion(data, nil) |
|||
} |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue