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.enumif 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-iosThere 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.URLRequestfor sending the request.Data,String,Decodable).Client
Client- an object with only one method for executingEndpoint.Endpoint.Combineand you don’t have to make wrappers for each request.The separation into
ClientandEndpointallows you to separate the asynchronous code inClientfrom 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.ApexyAlamofireuse built in implementation ofAsync/AwaitinAlamofireURLSessionnewAsync/Awaitmethods was implemented usingURLSession‘sAsyncAwaitextended implementation for iOS 14 and below. (look intoURLSession+Concurrency.swiftfor more details)Client,CombineClientandConcurrenyClientare 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.VoidEndpointbasic 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
UploadEndpointto send files or large amounts of data. In themakeRequest()method you need to returnURLRequestand 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. UseProgressobject 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:
Modela folder with network-level models. That’s what we send to the server and what we get in the response.Endpointa folder with requests.Commona folder with common helpers e.g.APIError.The final file and folder structure
JsonEndpointVoidEndpointBookListEndpointBookEndpointUpdateBookEndpointDeleteBookEndpointAPIErrorBookBookListEndpointTestsBookEndpointTestsUpdateBookEndpointTestsDeleteBookEndpointTestsRequirements
Additional resources