Major changes of Protocols and Async/Await in Apexy (#40)
Разделил протоколы. Переписал методы нового concurrency.
Поправил запросы в ULRSession
APIResult вынес в основной таргет
Поправил заголовки файлов
поправил образ и версию xcode
Поправил распаковку результата
Переработал методы для URLSession
Вынес обработку результатов
Вынес ResponseObserver
Обновил README
обновил доку
Apply suggestions from code review
Co-authored-by: Ivan Vavilov vavilov2@gmail.com
Async cancelation and thread switching fixed
Thread safety fix + test fix + new tests for helper
Specified type for continuation
Fix cancelation check. Fix test. Fix using async/await in sync method
Fix spacing in closure
Co-authored-by: Ivan Vavilov vavilov2@gmail.com
Apexy
The library for organizing a network layer in a project.
namespace
.enum
if different requests have the same response.Installation
CocoaPods
To integrate Apexy into your Xcode project using CocoaPods, specify it in your Podfile.
If you want to use Apexy with Alamofire:
pod 'Apexy'
If you want to use Apexy without Alamofire:
pod 'Apexy/URLSession'
If you want to use ApexyLoader:
pod 'Apexy/Loader'
Swift Package Manager
If you have Xcode project, open it and select File → Swift Packages → Add package Dependency and paste Apexy repository URL:
https://github.com/RedMadRobot/apexy-ios
There are 3 package products: Apexy, ApexyAlamofire, ApexyLoader.
Apexy — Uses URLSession under the hood
ApexyAlamofire — Uses Alamofire under the hood
ApexyLoader — add-on for Apexy to store fetched data in memory and observe loading state. See the documentation for details ApexyLoader:
If you have your own Swift package, add Apexy as a dependency to the dependencies value of your Package.swift.
Endpoint
Endpoint
- one of the basic protocols for organizing work with REST API. It is a set of request and response processing.URLRequest
for sending the request.Data
,String
,Decodable
).Client
Client
- an object with only one method for executingEndpoint
.Endpoint
.Combine
and you don’t have to make wrappers for each request.The separation into
Client
andEndpoint
allows you to separate the asynchronous code inClient
from the synchronous code inEndpoint
. Thus, the side effects are isolated inClient
, and the pure functions in the non-mutableEndpoint
.CombineClient
CombineClient
- protocol that wrap up network request inCombine
.ConcurrencyClient
ConcurrencyClient
- protocol that wrap up network request inAsync/Await
.Client
‘s methods.ApexyAlamofire
use built in implementation ofAsync/Await
inAlamofire
URLSession
newAsync/Await
methods was implemented usingURLSession
‘sAsyncAwait
extended implementation for iOS 14 and below. (look intoURLSession+Concurrency.swift
for more details)Client
,CombineClient
andConcurrenyClient
are separated protocols. You can specify method that you are using by using specific protocol.Getting Started
Since most requests will receive JSON, it is necessary to make basic protocols at the module level. They will contain common requests logic for a specific API.
JsonEndpoint
- basic protocol for requests waiting for JSON in the response body.VoidEndpoint
basic protocol for requests not waiting for a response body.BookListEndpoint
- get a list of books.BookEndpoint
- get a book byID
.UpdateBookEndpoint
- update a book.DeleteBookEndpoint
- delete a book byID
.Sending a large amount of data to the server
You can use
UploadEndpoint
to send files or large amounts of data. In themakeRequest()
method you need to returnURLRequest
and the data you are uploading, it can be a file.file(URL)
, a data.data(Data)
or a stream.stream(InputStream)
. To execute the request, call theClient.upload(endpoint: completionHandler:)
method. UseProgress
object to track the progress of the data upload or cancel the request.Network Layer Organization
If your application is called
Household
, the network module will be calledHouseholdAPI
.Split the network layer into folders:
Model
a folder with network-level models. That’s what we send to the server and what we get in the response.Endpoint
a folder with requests.Common
a folder with common helpers e.g.APIError
.The final file and folder structure
JsonEndpoint
VoidEndpoint
BookListEndpoint
BookEndpoint
UpdateBookEndpoint
DeleteBookEndpoint
APIError
Book
BookListEndpointTests
BookEndpointTests
UpdateBookEndpointTests
DeleteBookEndpointTests
Requirements
Additional resources