Super Easy Networking is simple and convenient wrapper around NSURLSession that supports common needs. A framework that is small enough to read in one go but useful enough to include in any project. It is fully tested framework for iOS, tvOS, watchOS and OS X.
Endpoint definitions:
struct APIEndpoints {
static func getMovies(with moviesRequestDTO: MoviesRequest) -> Endpoint<MoviesResponse> {
return Endpoint(path: "search/movie/",
method: .get,
headerParamaters: ["Content-Type": "application/json"], // Optional
queryParametersEncodable: moviesRequestDTO)
}
}
API Data (Data Transfer Objects):
struct MoviesRequest: Encodable {
let query: String
let page: Int
}
struct MoviesResponse: Decodable {
struct Movie: Decodable {
private enum CodingKeys: String, CodingKey {
case title
case overview
case posterPath = "poster_path"
}
let title: String
let overview: String
let posterPath: String
}
private enum CodingKeys: String, CodingKey {
case movies = "results"
}
let movies: [Movie]
}
API Networking Configuration:
struct AppConfiguration {
var apiKey: String = "xxxxxxxxxxxxxxxxxxxxxxxxx"
var apiBaseURL: String = "http://api.themoviedb.org"
}
class DIContainer {
static let shared = DIContainer()
lazy var appConfiguration = AppConfiguration()
lazy var apiDataTransferService: DataTransferService = {
let config = ApiDataNetworkConfig(baseURL: URL(string: appConfiguration.apiBaseURL)!,
queryParameters: ["api_key": appConfiguration.apiKey,
"language": NSLocale.preferredLanguages.first ?? "en"])
let apiDataNetwork = DefaultNetworkService(config: config)
return DefaultDataTransferService(with: apiDataNetwork)
}()
}
Making API call:
let endpoint = APIEndpoints.getMovies(with: MoviesRequest(query: "Batman Begins", page: 1))
dataTransferService.request(with: endpoint) { result in
guard case let .success(response) = result, let movies = response.movies else { return }
self.show(movies)
}
pod 'SENetworking'
Then pod install and import SFNetworking in files where needed
github "kudoleh/SENetworking"
Then carthage update and import SFNetworking_iOS in files where needed (e.g. for iOS platform)
Xcode tab: File -> Swift Packages -> Add Package Dependency
Enter package repository URL: https://github.com/kudoleh/SENetworking
And then import SFNetworking in files where needed
Copy folder SENetworking into your project
Oleh Kudinov, [email protected]
MIL License, Open Source License