XUtils

RxNetworks

Network API With RxSwift + Moya + HandyJSON + Plugins.


Plugins

CocoaPods Compatible CocoaPods Compatible CocoaPods Compatible

This module is mainly based on moya package network related plugins.

  • At present, 14 plugins have been packaged for you to use:
    • Header: Network HTTP Header Plugin.
    • Authentication: Interceptor plugin.
    • Debugging: Network printing, built in plugin.
    • GZip: Network data unzip plugin.
    • Shared: Network sharing plugin.
    • Files: Network downloading files And Uploading resources plugin.
    • Token: Token verify plugin.
    • Ignore: Ignore plugin, the purpose is to ignore a plugin in this network request.
    • CustomCache: Custom network data caching plugin.
    • Cache: Network data cache plugin.
    • Lottie: Animation loading plugin based on lottie.

For ios platform:

If you want to use token plugin and auth plugin you can refer to the project use case.

  • Auth: Authorization plugin.

🎷 Simple to use, implement the protocol method in the API protocol, and then add the plugin to it:

var plugins: APIPlugins {
    let cache = NetworkCachePlugin.init()
    let loading = NetworkLoadingPlugin.init()
    let warning = NetworkWarningPlugin.init()
    let shared = NetworkSharedPlugin.init()
    let gzip = NetworkGZipPlugin.init()
    return [loading, cache, warning, shared, gzip]
}

HandyJSON

This module is based on HandyJSON package network data parsing.

  • Roughly divided into the following 3 parts:
    • HandyDataModel: Network outer data model.
    • HandyJSONError: Parse error related.
    • RxHandyJSON: HandyJSON data parsing, currently provides two parsing solutions.
      • Option 1: Combine HandyDataModel model to parse out data.
      • Option 2: Parse the data of the specified key according to keyPath, the precondition is that the json data source must be in the form of a dictionary.

🎷 Example of use in conjunction with the network part:

func request(_ count: Int) -> Driver<[CacheModel]> {
    CacheAPI.cache(count).request()
        .asObservable()
        .mapHandyJSON(HandyDataModel<[CacheModel]>.self)
        .compactMap { $0.data }
        .observe(on: MainScheduler.instance)
        .delay(.seconds(1), scheduler: MainScheduler.instance)
        .asDriver(onErrorJustReturn: [])
}

HollowCodable

HollowCodable is a codable customization using property wrappers library for Swift.

This module is serialize and deserialize the data, Replace HandyJSON.

🎷 Example of use in conjunction with the network part:

func request(_ count: Int) -> Observable<[CodableModel]> {
    CodableAPI.cache(count)
        .request(callbackQueue: DispatchQueue(label: "request.codable"))
        .deserialized(ApiResponse<[CodableModel]>.self)
        .compactMap({ $0.data })
        .observe(on: MainScheduler.instance)
        .catchAndReturn([])
}

public extension Observable where Element: Any {
    
    @discardableResult func deserialized<T: HollowCodable>(_ type: T.Type) -> Observable<T> {
        return self.map { element -> T in
            return try T.deserialize(element: element)
        }
    }
}

Remarks

The general process is almost like this, the Demo is also written in great detail, you can check it out for yourself.🎷

BoomingDemo

Tip: If you find it helpful, please help me with a star. If you have any questions or needs, you can also issue.

Thanks.🎇


Articles

  • coming soon...