Skip to content

Commit

Permalink
Merge pull request #47 from futuredapp/feature/adapter-removal-rewrite
Browse files Browse the repository at this point in the history
Adapter removal rewrite
  • Loading branch information
mkj-is authored Feb 11, 2020
2 parents 99258ca + 44c1acc commit ae8cdfd
Show file tree
Hide file tree
Showing 38 changed files with 934 additions and 1,078 deletions.
55 changes: 24 additions & 31 deletions FTAPIKit.podspec
Original file line number Diff line number Diff line change
@@ -1,36 +1,29 @@
Pod::Spec.new do |s|
s.name = "FTAPIKit"
s.version = "0.6.0"
s.summary = "Declarative, generic REST API framework using URLSession and Codable"
s.description = <<-DESC
Protocol-oriented REST API library for communication with REST API.
APIEndpoint protocols allow description of the API access points
and the requests/responses codable types. APIAdapter handles execution
of calls to this endpoints.
s.name = "FTAPIKit"
s.version = "1.0.0"
s.summary = "Declarative, generic and protocol-orented REST API framework using URLSession and Codable"
s.description = <<-DESC
Protocol-oriented framework for communication with REST APIs.
Endpoint protocols describe the API resource access points
and the requests/responses codable types. Server protocol describes web services
and enables the user to call endoints in a type-safe manner.
DESC
s.homepage = "https://github.com/futuredapp/FTAPIKit"
s.license = { :type => "MIT", :file => "LICENSE" }
s.author = { "Matěj Kašpar Jirásek" => "[email protected]" }
s.social_media_url = "https://twitter.com/Futuredapps"
s.default_subspec = 'Core'
s.swift_version = "5.0"
s.ios.deployment_target = "8.0"
s.osx.deployment_target = "10.10"
s.watchos.deployment_target = "2.0"
s.tvos.deployment_target = "9.0"
s.source = { :git => "https://github.com/futuredapp/FTAPIKit.git", :tag => s.version.to_s }
s.homepage = "https://github.com/futuredapp/FTAPIKit"
s.license = { type: "MIT", file: "LICENSE" }
s.author = { "Matěj Kašpar Jirásek": "[email protected]" }
s.social_media_url = "https://twitter.com/Futuredapps"

s.subspec 'Core' do |ss|
ss.source_files = "Sources/FTAPIKit/*"
ss.framework = "Foundation"
ss.ios.framework = "MobileCoreServices"
ss.tvos.framework = "MobileCoreServices"
ss.watchos.framework = "MobileCoreServices"
end
s.source = { git: "https://github.com/futuredapp/FTAPIKit.git", tag: s.version.to_s }
s.source_files = "Sources/FTAPIKit/*"

s.subspec 'PromiseKit' do |ss|
ss.source_files = Dir['Sources/FTAPIKitPromiseKit/*']
ss.dependency 'PromiseKit', '~> 6.0'
ss.dependency 'FTAPIKit/Core'
end
s.framework = "Foundation"
s.ios.framework = "MobileCoreServices"
s.tvos.framework = "MobileCoreServices"
s.watchos.framework = "MobileCoreServices"

s.swift_version = "5.0"
s.ios.deployment_target = "8.0"
s.osx.deployment_target = "10.10"
s.watchos.deployment_target = "2.0"
s.tvos.deployment_target = "9.0"
end
13 changes: 2 additions & 11 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.0
// swift-tools-version:5.1

import PackageDescription

Expand All @@ -7,21 +7,12 @@ let package = Package(
products: [
.library(
name: "FTAPIKit",
targets: ["FTAPIKit"]),
.library(
name: "FTAPIKitPromiseKit",
targets: ["FTAPIKitPromiseKit"])
],
dependencies: [
.package(url: "https://github.com/mxcl/PromiseKit.git", from: "6.8.4")
targets: ["FTAPIKit"])
],
targets: [
.target(
name: "FTAPIKit",
dependencies: []),
.target(
name: "FTAPIKitPromiseKit",
dependencies: ["FTAPIKit", "PromiseKit"]),
.testTarget(
name: "FTAPIKitTests",
dependencies: ["FTAPIKit"])
Expand Down
54 changes: 0 additions & 54 deletions Sources/FTAPIKit/APIAdapter+Types.swift

This file was deleted.

55 changes: 0 additions & 55 deletions Sources/FTAPIKit/APIAdapter.swift

This file was deleted.

85 changes: 0 additions & 85 deletions Sources/FTAPIKit/APIEndpoint.swift

This file was deleted.

39 changes: 39 additions & 0 deletions Sources/FTAPIKit/APIError+Standard.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import Foundation

/// Standard API error returned in `APIResult` when no custom error
/// was parsed in the `APIAdapter` first and the response from server
/// was invalid.
public enum APIErrorStandard: APIError {
/// Error raised by URLSession.
case connection(URLError)
case encoding(EncodingError)
case decoding(DecodingError)
/// Status code error when the response status code
/// is larger or equal to 500 and less than 600.
case server(Int, URLResponse, Data?)
/// Status code error when the response status code
/// is larger or equal to 400 and less than 500.
case client(Int, URLResponse, Data?)
case unhandled(data: Data?, response: URLResponse?, error: Error?)

public init?(data: Data?, response: URLResponse?, error: Error?, decoding: Decoding) {
switch (data, response as? HTTPURLResponse, error) {
case let (_, _, error as URLError):
self = .connection(error)
case let (_, _, error as EncodingError):
self = .encoding(error)
case let (_, _, error as DecodingError):
self = .decoding(error)
case let (data, response?, nil) where 400..<500 ~= response.statusCode:
self = .client(response.statusCode, response, data)
case let (data, response?, nil) where 500..<600 ~= response.statusCode:
self = .server(response.statusCode, response, data)
case (_, .some, nil), (.some, nil, nil):
return nil
default:
self = .unhandled(data: data, response: response, error: error)
}
}

public static var unhandled: Standard = .unhandled(data: nil, response: nil, error: nil)
}
39 changes: 3 additions & 36 deletions Sources/FTAPIKit/APIError.swift
Original file line number Diff line number Diff line change
@@ -1,42 +1,9 @@
import Foundation

public protocol APIError: Error {
init?(data: Data?, response: URLResponse?, error: Error?, decoder: JSONDecoder)
}
typealias Standard = APIErrorStandard

/// Standard API error returned in `APIResult` when no custom error
/// was parsed in the `APIAdapter` first and the response from server
/// was invalid.
public enum StandardAPIError: APIError {
/// Error raised by NSURLSession corresponding to NSURLErrorCancelled at
/// domain NSURLErrorDomain.
case cancelled
/// Connection error when no response and data was received.
case connection(Error)
/// Status code error when the response status code
/// is larger or equal to 500 and less than 600.
case server(Int, Data?)
/// Status code error when the response status code
/// is larger or equal to 400 and less than 500.
case client(Int, Data?)
/// Multipart body part error, when the stream for the part
/// or the temporary request body stream cannot be opened.
case multipartStreamCannotBeOpened
init?(data: Data?, response: URLResponse?, error: Error?, decoding: Decoding)

public init?(data: Data?, response: URLResponse?, error: Error?, decoder: JSONDecoder) {
switch (data, response as? HTTPURLResponse, error) {
case let (_, _, error as NSError) where error.domain == NSURLErrorDomain && error.code == NSURLErrorCancelled:
self = .cancelled
case let (_, _, error?):
self = .connection(error)
case let (data, response?, nil) where 400..<500 ~= response.statusCode:
self = .client(response.statusCode, data)
case let (data, response?, nil) where 500..<600 ~= response.statusCode:
self = .server(response.statusCode, data)
case (_, .some, nil), (.some, nil, nil):
return nil
case (nil, nil, nil):
fatalError("No response, data or error was returned from URLSession")
}
}
static var unhandled: Self { get }
}
12 changes: 0 additions & 12 deletions Sources/FTAPIKit/AnyEncodable.swift

This file was deleted.

File renamed without changes.
Loading

0 comments on commit ae8cdfd

Please sign in to comment.