-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e0e6311
commit 9ebeda8
Showing
26 changed files
with
373 additions
and
80 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,21 @@ | ||
# Uncomment the next line to define a global platform for your project | ||
# platform :ios, '9.0' | ||
|
||
target 'SwiftRorty.iOS' do | ||
# Comment the next line if you don't want to use dynamic frameworks | ||
use_frameworks! | ||
|
||
# Pods for SwiftRorty.iOS | ||
pod 'lottie-ios' | ||
pod "Resolver" | ||
|
||
target 'SwiftRorty.iOSTests' do | ||
inherit! :search_paths | ||
# Pods for testing | ||
end | ||
|
||
target 'SwiftRorty.iOSUITests' do | ||
# Pods for testing | ||
end | ||
|
||
end |
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
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
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,118 @@ | ||
// | ||
// RestClient.swift | ||
// SwiftRorty.iOS | ||
// | ||
// Created by developersancho on 26.02.2022. | ||
// | ||
|
||
import Foundation | ||
|
||
import Combine | ||
|
||
/// Provides access to the REST Backend | ||
protocol RestClient { | ||
/// Retrieves a JSON resource and decodes it | ||
func get<T: Decodable, E: Endpoint>(_ endpoint: E) -> AnyPublisher<T, Error> | ||
|
||
/// Creates some resource by sending a JSON body and returning empty response | ||
func post<S: Encodable, E: Endpoint>(_ endpoint: E, using body: S) | ||
-> AnyPublisher<Void, Error> | ||
} | ||
|
||
class RestClientImpl: RestClient { | ||
private let session: URLSession | ||
|
||
init(sessionConfig: URLSessionConfiguration? = nil) { | ||
self.session = URLSession(configuration: sessionConfig ?? URLSessionConfiguration.default) | ||
} | ||
|
||
func get<T, E>(_ endpoint: E) -> AnyPublisher<T, Error> where T: Decodable, E: Endpoint { | ||
startRequest(for: endpoint, method: "GET", jsonBody: nil as String?) | ||
.tryMap { try $0.parseJson() } | ||
.eraseToAnyPublisher() | ||
} | ||
|
||
func post<S, E>(_ endpoint: E, using body: S) | ||
-> AnyPublisher<Void, Error> where S: Encodable, E: Endpoint | ||
{ | ||
startRequest(for: endpoint, method: "POST", jsonBody: body) | ||
.map { _ in () } | ||
.catch { error -> AnyPublisher<Void, Error> in | ||
switch error { | ||
case RestClientErrors.noDataReceived: | ||
// API's Post request doesn't return data back even with code 200 | ||
return Just(()).setFailureType(to: Error.self).eraseToAnyPublisher() | ||
default: | ||
return Fail<Void, Error>(error: error).eraseToAnyPublisher() | ||
} | ||
} | ||
.eraseToAnyPublisher() | ||
} | ||
|
||
private func startRequest<T: Encodable, S: Endpoint>(for endpoint: S, | ||
method: String, | ||
jsonBody: T? = nil) | ||
-> AnyPublisher<InterimRestResponse, Error> { | ||
var request: URLRequest | ||
|
||
do { | ||
request = try buildRequest(endpoint: endpoint, method: method, jsonBody: jsonBody) | ||
} catch { | ||
print("Failed to create request: \(String(describing: error))") | ||
return Fail(error: error).eraseToAnyPublisher() | ||
} | ||
|
||
print("Starting \(method) request for \(String(describing: request))") | ||
|
||
return session.dataTaskPublisher(for: request) | ||
.mapError { (error: Error) -> Error in | ||
print("Request failed: \(String(describing: error))") | ||
return RestClientErrors.requestFailed(error: error) | ||
} | ||
// we got a response, lets see what kind of response | ||
.tryMap { (data: Data, response: URLResponse) in | ||
let response = response as! HTTPURLResponse | ||
print("Got response with status code \(response.statusCode) and \(data.count) bytes of data") | ||
|
||
if response.statusCode == 400 { | ||
throw RestClientErrors.requestFailed(code: response.statusCode) | ||
} | ||
return InterimRestResponse(data: data, response: response) | ||
}.eraseToAnyPublisher() | ||
} | ||
|
||
private func buildRequest<T: Encodable, S: Endpoint>(endpoint: S, | ||
method: String, | ||
jsonBody: T?) throws -> URLRequest { | ||
var request = URLRequest(url: endpoint.url, timeoutInterval: 10) | ||
request.httpMethod = method | ||
// if we got some data, we encode as JSON and put it in the request | ||
if let body = jsonBody { | ||
do { | ||
request.httpBody = try JSONEncoder().encode(body) | ||
} catch { | ||
throw RestClientErrors.jsonDecode(error: error) | ||
} | ||
} | ||
return request | ||
} | ||
|
||
struct InterimRestResponse { | ||
let data: Data | ||
let response: HTTPURLResponse | ||
|
||
func parseJson<T: Decodable>() throws -> T { | ||
if data.isEmpty { | ||
throw RestClientErrors.noDataReceived | ||
} | ||
|
||
do { | ||
return try JSONDecoder().decode(T.self, from: data) | ||
} catch { | ||
print("Failed to decode JSON: \(error)", String(describing: error)) | ||
throw RestClientErrors.jsonDecode(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// | ||
// RestClientErrors.swift | ||
// SwiftRorty.iOS | ||
// | ||
// Created by developersancho on 26.02.2022. | ||
// | ||
|
||
import Foundation | ||
|
||
enum RestClientErrors: Error { | ||
case requestFailed(error: Error) | ||
case requestFailed(code: Int) | ||
case noDataReceived | ||
case jsonDecode(error: Error) | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,20 @@ | ||
// | ||
// Decoder.swift | ||
// SwiftRorty.iOS | ||
// | ||
// Created by developersancho on 25.02.2022. | ||
// | ||
|
||
import Foundation | ||
import Combine | ||
|
||
class Decoder { | ||
func decode<T: Decodable>(_ data: Data) -> AnyPublisher<T, BaseError> { | ||
let decoder = JSONDecoder() | ||
|
||
return Just(data) | ||
.decode(type: T.self, decoder: decoder) | ||
.mapError({ .parse(description: $0.localizedDescription) }) | ||
.eraseToAnyPublisher() | ||
} | ||
} |
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
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
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,13 @@ | ||
// | ||
// CharacterListDto.swift | ||
// SwiftRorty.iOS | ||
// | ||
// Created by developersancho on 27.02.2022. | ||
// | ||
|
||
import Foundation | ||
|
||
struct CharacterListDto { | ||
let info: PageInfo | ||
let characters: [CharacterDto] | ||
} |
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
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
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
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
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
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.