DEV Community

Condy
Condy

Posted on

Network Plugin

Network Plugin, support batch and chain operation

  • Friends who are familiar with swift should know an excellent three party library Moya, the plugin version of the network request is really fragrant, so I use the idea to make a pure oc version of the plugin Network request library.
  • Friends who are familiar with oc should know an excellent three party library YTKNetwork, object based protocol version network request, and then his batch network request and chain network The request is also super fragrant.
  • Combining some of the advantages of the two, make a pure OC version of the batch and chain plugin version of the network request library.

Function list

The plugin version of the network request can be more convenient and quick to customize the exclusive network request, and supports batch operation and chain operation.

  • Support basic network requests, download and upload files.
  • Support configuration of general request and path, general parameters, etc.
  • Support batch operation.
  • Support chain network request.
  • Support setting loading animation plugin.
  • Support analysis result plugin.
  • Support web cache plugin.
  • Support configuration of self built certificate plugin.
  • Support to modify the request body and get the response result plugin.
  • Support network log packet capture plugin.
  • Support refresh to load more plugins.
  • Support error code parsing plugin.
  • Support error and empty data UI display plugin.
  • Support display indicator plugin.
  • Support failed error prompt plugin.
  • Support request parameter set secret key plugin.
  • Support network data unzip and parameter zip plugin.

Network

KJBaseNetworking: network request base class, based on AFNetworking package use

> There are also two entrances to set the common root path and common parameters, similar to: userID, token, etc.

/// Root path address
@property (nonatomic, strong, class) NSString *baseURL;
/// Basic parameters, similar to: userID, token, etc.
@property (nonatomic, strong, class) NSDictionary *baseParameters;
Enter fullscreen mode Exit fullscreen mode

> Packaged methods include basic network requests, upload and download files, etc.

KJNetworkingRequest: Request body, set network request related parameters, including parameters, request method, plug-ins, etc.

KJNetworkingResponse: Respond to the request result, get the data generated between plugins, etc.

KJNetworkingType: Summarize all enumerations and callback declarations

KJNetworkBasePlugin: Plugin base class, plugin parent class

KJNetworkPluginManager: Plugin manager, central nervous system

/// Plugin version network request
/// @param request request body
/// @param success success callback
/// @param failure failure callback
+ (void)HTTPPluginRequest:(KJNetworkingRequest *)request 
                  success:(KJNetworkPluginSuccess)success 
                  failure:(KJNetworkPluginFailure)failure;
Enter fullscreen mode Exit fullscreen mode

KJNetworkingDelegate: Plugin protocol, manage network request results

Currently, there are 5 protocol methods extracted, starting time, network request time, network success, network failure, and final return

/// Start preparing for network requests
/// @param request Request related data
/// @param response response data
/// @param endRequest whether to end the following network request
/// @return returns the cached data, successResponse is not empty means there is cached data
- (KJNetworkingResponse *)prepareWithRequest:(KJNetworkingRequest *)request
                                    response:(KJNetworkingResponse *)response
                                  endRequest:(BOOL *)endRequest;

/// Network request start time request
/// @param request Request related data
/// @param response response data
/// @param stopRequest Whether to stop the network request
/// @return Returns the data processed by the plugin at the beginning of the network request
- (KJNetworkingResponse *)willSendWithRequest:(KJNetworkingRequest *)request
                                     response:(KJNetworkingResponse *)response
                                  stopRequest:(BOOL *)stopRequest;

/// Successfully received data
/// @param request receives successful data
/// @param response response data
/// @param againRequest Whether you need to request the network again
/// @return returns the data after successful plugin processing
- (KJNetworkingResponse *)succeedWithRequest:(KJNetworkingRequest *)request
                                    response:(KJNetworkingResponse *)response
                                againRequest:(BOOL *)againRequest;

/// Failure handling
/// @param request failed network activity
/// @param response response data
/// @param againRequest Whether you need to request the network again
/// @return returns the data processed by the failed plugin
- (KJNetworkingResponse *)failureWithRequest:(KJNetworkingRequest *)request
                                    response:(KJNetworkingResponse *)response
                                againRequest:(BOOL *)againRequest;

/// Ready to return to the business logic call at the moment
/// @param request Request related data
/// @param response response data
/// @param error error message
/// @return returns the data after final processing
- (KJNetworkingResponse *)processSuccessResponseWithRequest:(KJNetworkingRequest *)request
                                                   response:(KJNetworkingResponse *)response
                                                      error:(NSError **)error;
Enter fullscreen mode Exit fullscreen mode

Plugins collection

There are 13 plugins available for you to use:

Chain

  • Chained network requests are actually mainly used to manage network requests with interdependencies, and it can actually eventually be used to manage multiple topologically sorted network requests.
// Test the chained 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];
}
Enter fullscreen mode Exit fullscreen mode

More about chained plugin network processing.👒👒

Batch

  • Regarding batch network requests, provide configuration information such as setting the maximum concurrent number, the number of failed calls, and the timing of error reconnection.
// Test batch network requests
- (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];
}
Enter fullscreen mode Exit fullscreen mode

More about batch plugin network processing.👒👒

About the author


License

KJNetworkPlugin is available under the MIT license. See the LICENSE file for more info.

Link

Top comments (0)