Plug-in version of the network request architecture, support batch operation and chain operation

  • 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

  • Combining the advantages of both, a pure OC version of batch and chain plug-in version of the network request library is made.

  • At present, nine plug-ins are sorted out: parsing plug-in, cache plug-in, self-built certificate plug-in, loading prompt plug-in, modify request plug-in, log capture plug-in, error code plug-in, refresh plug-in, network error and empty data plug-in

Feature list

Plug-in version of the network request can be more convenient to customize the exclusive network request, and support batch operation, chain operation

The main function list is as follows:

  • Support basic network requests, download and upload files
  • Supports the configuration of common requests, paths, and parameters
  • Support setup loading and prompt box plug-ins
  • Support parsing result plug-ins
  • Support network cache plug-ins
  • You can configure the self-built certificate plug-in
  • Support to modify the request body and get the response result plug-in
  • Support network log packet capture plug-in
  • Support refresh to load more plug-ins
  • Support error code resolution plug-ins
  • Support error and null data UI display plug-ins
  • Batch operations
  • Support for chained network requests

Using the tutorial

  • Basic Usage Tutorial
  • Plugin Tutorial
  • Batch web use tutorial
  • Chain web use tutorial

The Network edition piece

KJBaseNetworking: network request base class, basic network request, upload and download files and other methods

@property (nonatomic, strong, class) NSString *baseURL; @property (nonatomic, strong, class) NSDictionary *baseParameters;Copy the code

KJNetworkingRequest: set network request parameters, including parameters, request mode, plug-in, etc. KJNetworkingResponse: respond to the request result, get the data generated between plug-ins, etc. KJNetworkingType: Summary all enumerations and callbacks declare KJNetworkBasePlugin: plug-in base class, plug-in parent class KJNetworkPluginManager: plug-in manager, central nerve

@param Request request body @param Success Callback // @param failure callback + (void)HTTPPluginRequest:(KJNetworkingRequest *)request success:(KJNetworkPluginSuccess)success failure:(KJNetworkPluginFailure)failure;Copy the code

KJNetworkingDelegate: Plug-in protocol for managing network request results

  • At present, the following five 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 Requests related data /// @Param againRequest Whether to request the network again /// @return returns the data processed by the successful plug-in - (KJNetworkingResponse *)succeedWithRequest:(KJNetworkingRequest *)request againRequest:(BOOL *)againRequest; // @param Request Requests related data /// @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

Plugins

There are currently 13 plugins available:

  • KJNetworkLoadingPlugin: Loads the animation plugin
  • KJNetworkAnslysisPlugin: Parse data plug-in
  • KJNetworkCachePlugin: Network cache plugin
  • Certificate of self-built plug-in KJNetworkCertificatePlugin: configuration
  • KJNetworkThiefPlugin: Resource modifier plug-in
  • KJNetworkCapturePlugin: network log capture plug-in
  • KJNetworkCodePlugin: Error code parsing plugin
  • KJNetworkRefreshPlugin: Refresh loads more plug-ins
  • KJNetworkEmptyPlugin: UI display plug-in for error messages and empty data
  • KJNetworkIndicatorPlugin: Indicator plugin
  • KJNetworkWarningPlugin: Error prompt plugin
  • KJNetworkSecretPlugin: Key plug-in
  • KJNetworkZipPlugin: Unzip plug-in

Chain

  • In fact, chain network request is mainly used to manage network requests that are interdependent, it can actually be used to manage multiple topologically sorted network requests.
/ / test chain network request - (void) testChainNetworking {XCTestExpectation * expectation = [self expectationWithDescription: @ "test chain."]; KJNetworkingRequest * request = [[KJNetworkingRequest alloc] init]; request.method = KJNetworkRequestMethodGET; request.ip = @"https://www.httpbin.org"; request.path = @"/ip"; request.responseSerializer = KJSerializerJSON; [KJNetworkChainManager HTTPChainRequest:request failure:^(NSError * error) { XCTFail(@"%@", error.localizedDescription);  }] .chain(^__kindof KJNetworkingRequest * _Nullable(id _Nonnull responseObject) { KJNetworkingRequest * request = [[KJNetworkingRequest alloc] init]; request.ip = @"https://www.httpbin.org"; request.path = @"/post"; request.params = { "ip": responseObject["origin"] }; return request; }) .lastChain(^(id _Nonnull responseObject) { [expectation fulfill]; }); [self waitForExpectationsWithTimeout:300 handler:nil]; }Copy the code

Learn more about chained plug-in network processing. 👒👒

Batch

  • For batch network requests, you can set the maximum number of concurrent requests, number of failed calls, and error reconnection time
/ / test batch network request - (void) testBatchNetworking {XCTestExpectation * expectation = [self expectationWithDescription: @ "test batch."]; NSMutableArray * array = [NSMutableArray array]; { KJNetworkingRequest * request = [[KJNetworkingRequest alloc] init]; request.method = KJNetworkRequestMethodGET; request.path = @"/headers"; request.responseSerializer = KJSerializerJSON; [array addObject:request]; }{ KJNetworkingRequest * request = [[KJNetworkingRequest alloc] init]; request.method = KJNetworkRequestMethodGET; request.path = @"/ip"; [array addObject:request]; } KJBatchConfiguration * configuration = [KJBatchConfiguration sharedBatch]; configuration.maxQueue = 3; configuration.requestArray = array.mutableCopy; [KJNetworkBatchManager HTTPBatchRequestConfiguration:configuration reconnect:^BOOL(NSArray<KJNetworkingRequest *> * _Nonnull reconnectArray) { return YES;  } complete:^(NSArray<KJBatchResponse *> * _Nonnull result) { [expectation fulfill]; }]; [self waitForExpectationsWithTimeout:300 handler:nil]; }Copy the code

More about batch plug-in network processing. 👒👒

The last

  • The plugin KJNetworkPlugin is recommended for OC, and Moya for Siwft.
  • Attach a development acceleration library, simply so convenient.🎷
  • KJCategoriesDemo address 🎷 you can click a star if you like.
  • I will package a set of responsive plug-in network requests when I have time to spare, so the bosses think it is helpful and look for a star 🌟.
  • Portal: KJNetworkPluginDemo
  • End .🎷