Coder Social home page Coder Social logo

ajnetworking-swift's Introduction

AJNetworking-Swift

A Network Framework basic on Alamofire and HandyJSON. Swift version is 4.0.


Usage

Step1

Firstly you need use class AJNetworkConfig to define some global parameters, it is a singleton.

    /// global network request host path
    public var host:String? = nil;

    /// https request certificate password
    public var httpsCerPW:String? = nil;

    /// https request certificate bundle path
    public var httpsCerPath:String? = nil;

    /// custom hub delegate
    public var hubHanlder:AJHubPlugin? = nil;

Step2

AJRequest<MultipartTestRequest, AJBaseCommonResponseBean<AJBaseBean>>.sendRequest(.uploadAvatar(avatar: #imageLiteral(resourceName: "test")), callback: { (res:AJBaseCommonResponseBean<AJBaseBean>?, fileUrl:URL?, err:AJError?) in
            
            if err == nil {
                print("๐Ÿค–:"+(res?.toJSONString() ?? ""));
                print("#file#: \(fileUrl?.absoluteString ?? "")");
            }
            
        }) { (progress:Progress) in
            
            let percent:Double = Double(progress.completedUnitCount)/Double(progress.totalUnitCount);
            print("\(#line)-#progress#: \(percent)");
        }
  • AJRequest is in charge of network request, it has two generic params <S:AJRequestBody, E:AJBaseResponseBean>.

  • AJRequestBody is a protocal, it contains network request params define . Parameter apiPath and params must be implemented, rest has default implementation.

  • AJBaseResponseBean is a basic json serialize class, it also contain two sub basic class AJBaseCommonResponseBean and AJBaseListResponseBean. They has one generic parameter must inherit class AJBaseBean. They are all inherit protocol HandyJSON.

Multipart Request

inherit protocol AJRequestBody , and turn multipartFormData, here is example:

    var multipartFormData:[FormData]? {
        
        switch self {
        case .uploadAvatar(let avatar):
            
            // if you upload file, you need to send mimeType
            let formData:FormData = FormData(data: UIImageJPEGRepresentation(avatar, 0.6)!, name: "avatar", mimeType:"image/jpeg");
            
            // if you need post other params, can use utf8 ecoding data
            let data:Data = "18090939282".data(using: .utf8)!;
            let param:FormData = FormData(data: data, name: "phone", mimeType: nil);
            
            return [formData, param];
        }
    }

Download File Request

inherit protocol AJRequestBody , and turn downloadFileDestination, it return a tuple which contain two params, here is example:

    var downloadFileDestination:(filePath:String, fileName:String)? {
        
        let doc = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
        let name:String = "avatar.jpeg";
        
        return (filePath:doc.absoluteString, fileName:name);
    }

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.