KJNetworkPluginManager
- Friends familiar with Swift should know an excellent third-party library Moya, plug-in version of the network request is really fragrant, so draw on ideas to make a pure OC version of the plug-in network request library
- Those who are familiar with OC should know an excellent three-party library YTKNetwork, which is based on the object protocol version of network request, and its batch network request and chain network request are also super sweet
- Combine the two and make one
batch
和The chain
Plug-in network request library
KJNetworkPlugin module
KJNetworkPlugin is a network request library based on protocol – oriented network abstraction layer. It is encapsulated and used again based on AFNetworking. Simply speaking, it should be divided into several major plates:
- Chain: plug-in network request
- Batch: indicates the network request of the Batch plug-in version
- Network: Plug-in manager and Network request base class
- Plugins: a collection of plug-ins, currently there are 5 plug-ins for use
- Loading: Loading error plug-in
- Anslysis: Data parsing plug-in
- Cache: Cache plug-in
- Certificate: self-created Certificate plug-in
- Thief: Modifier plug-in
In fact, there are upload log plug-in, encryption and decryption plug-in and so on. Due to the high degree of coupling with the project, sharing is not considered for the time being. I will provide ideas later, and you can encapsulate your own exclusive plug-in according to your ideas
The Network edition piece
- KJBaseNetworking: Network request base class, based on AFNetworking encapsulation
There are also two entrances for setting common root paths and common parameters, such as userID, token, etc
@property (nonatomic, strong, class) NSString *baseURL; @property (nonatomic, strong, class) NSDictionary *baseParameters;Copy the code
Encapsulation of the basic network request, upload and download files and other methods
- KJNetworkingRequest: Request body, set network request parameters, including parameters, request mode, plug-ins, etc
kjSerializerHttp@property (nonatomic, assign) KJSerializer requestSerializer; kjSerializerHttp@property (nonatomic, assign) KJSerializer responseSerializer; @property (nonatomic, assign) NSTimeInterval timeoutInterval; @property (nonatomic, strong) NSDictionary *header; @property (nonatomic, strong) NSArray<id<KJNetworkDelegate>>*plugins; / / / request type, default KJNetworkRequestMethodPOST @ property (nonatomic, assign) KJNetworkRequestMethod method; [KJBaseNetworking baseURL] @property (nonatomic, strong, nullable) NSString * IP; @property (nonatomic, strong) NSString *path; @property (nonatomic, strong, nullable) id params; @property (nonatomic, strong, readonly) id secretParams; @property (nonatomic, strong, readonly) NSString *URLString; // Network request plug-in timing, @property (nonatomic, assign, readOnly) KJRequestOpportunity opportunity; @property (nonatomic, strong, readOnly) NSString *requestIdentifier; @property (nonatomic, strong, nullable) KJConstructingBody *constructingBody; @property (nonatomic, strong, nullable) KJDownloadBody *downloadBody;Copy the code
After the batch and chain network request, also need to inherit the base class and then design some advanced usage, after free I will slowly write articles to share, looking forward to your attention
-
KJNetworkingResponse: To respond to a request, this class should be mostly used internally by the plug-in library or by the KJNetworkThiefPlugin to retrieve data generated between plug-ins, etc
-
KJNetworkingType: Summarize all enumerations and callback declarations
-
KJNetworkingDelegate: plug-in protocol. Currently, the following 5 protocol methods are extracted, which are roughly divided into start time, network request time, network success, network failure, and final return
// @param Request Data related to requests // @param endRequest Whether to end the following network requests /// @return Returns data processed by the plug-in. - (KJNetworkingResponse *)prepareWithRequest:(KJNetworkingRequest *)request endRequest:(BOOL *)endRequest; // @param Request Request data // @param stopRequest Whether to stop network requests /// @return Returns the data processed by the plug-in when network requests start - (KJNetworkingResponse *)willSendWithRequest:(KJNetworkingRequest *)request stopRequest:(BOOL *)stopRequest; // @param Request Receives data successfully // @Param againRequest Whether to request the network again /// @return Returns data processed by the successful plug-in - (KJNetworkingResponse *)succeedWithRequest:(KJNetworkingRequest *)request againRequest:(BOOL *)againRequest; /// @param Request failed network activity /// @Param againRequest Whether to request the network again /// @return returns the data processed after the failed plug-in - (KJNetworkingResponse) *)failureWithRequest:(KJNetworkingRequest *)request againRequest:(BOOL *)againRequest; // @param Request requests related data // @param error error message /// @return returns the final processed data - (KJNetworkingResponse) *)processSuccessResponseWithRequest:(KJNetworkingRequest *)request error:(NSError **)error;Copy the code
-
KJNetworkBasePlugin: Plug-in base class, plug-in parent class
-
KJNetworkPluginManager: the plug-in manager, the central nervous system, is actually only one method for the outside world to call
@param Request request body @param Success Callback // @param failure callback + (void)HTTPPluginRequest:(KJNetworkingRequest *)request success:(KJNetworkPluginSuccess)success failure:(KJNetworkPluginFailure)failure;Copy the code
A collection of Plugins
There are 5 plugins available:
-
KJNetworkLoadingPlugin: load box and error box plug-in based on MBProgressHUD encapsulation
-
KJNetworkAnslysisPlugin: parsing data plug-in based on MJExtension encapsulation
-
KJNetworkCachePlugin: Network caching plug-in based on YYCache encapsulation
-
Certificate of self-built plug-in KJNetworkCertificatePlugin: configuration
-
KJNetworkingRequest: Modify KJNetworkingRequest and get KJNetworkingResponse plug-in
- The KJNetworkThiefPlugin is an introduction to the use of the modifier plug-in
Later, I will introduce each plug-in design and usage
Chain Plug-in network
Chain network request, on how to use later to write an article, looking forward to your continued attention
Currently, there are two packages using the chain plug-in scheme.
- Solution 1: Customize parameters
// @param request // @param request // @param request // @param request // /// @param chain will respond to a single failure, return the next network request body, if null, can terminate the subsequent request, ResponseObject + (void)HTTPChainRequest:(__kindof KJNetworkingRequest *)request success:(KJNetworkChainSuccess)success failure:(KJNetworkChainFailure)failure chain:(KJNetworkNextChainRequest)chain,... ;Copy the code
- Scheme 2: the chain closure is adopted
@param request // @param request // @param request /// @return returns its own object + (instancetype)HTTPChainRequest:(__kindof KJNetworkingRequest *)request failure:(KJNetworkChainFailure)failure; // Request body transfer vector, The callback returns on a network request @ property (nonatomic, copy, readonly) KJNetworkChainManager * (^ chain) (KJNetworkNextChainRequest); @property (nonatomic, copy, readOnly) void(^lastChain)(void(^)(id responseObject));Copy the code
Batch Batch plug-in network
Batch plug-in network request, which provides configuration information such as the maximum number of concurrent requests, number of failed calls, and error reconnection time
@param reconnect reconnect reconnect reconnect reconnect reconnect reconnect reconnect reconnect reconnect reconnect reconnect reconnect reconnect reconnect reconnect reconnect Return to success and failure data array + (void) HTTPBatchRequestConfiguration (KJBatchConfiguration *) configuration reconnect:(KJNetworkBatchReconnect)reconnect complete:(KJNetworkBatchComplete)complete;Copy the code
The ending is introduced
Pod installation and use
Pod 'KJNetworkPlugin' # plugin pod 'KJNetworkPlugin/Batch' # 'KJNetworkPlugin/Loading' pod 'KJNetworkPlugin/Cache' pod 'KJNetworkPlugin/Thief' pod 'KJNetworkPlugin/Anslysis' pod 'KJNetworkPlugin/Certificate'Copy the code
KJNetworkPlugin is a portal that allows you to write something
There are related plug-ins behind I will also slowly add…