Git Product home page Git Product logo

httpsessionmanager's Introduction

HttpSessionManager

After learning 'Alamofire' and 'Moya', I packaged a general-purpose network request library based on Alamofire, which is simple, convenient and practical.

Getting Started

Download HttpSessionManager,have a try with the example.

Requirements

iOS 8.0+
Xcode 10.1+
Swift 4.2+

Installing

HttpSessionManager supports multiple methods for installing the library in a project.

1.CocoaPods

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'

target 'TargetName' do
pod 'HttpSessionManager', '~> 1.0.0'
end

2.Manually
you can drag the downloaded code directly into your project.

Usage

  1. Create a new class that implement protocol of 'RequestConfigProtocol'
class RequestConfig: RequestConfigProtocol {
    
    /// Adding to path ,retrun URL.
    public func getBaseURL(path: String) -> URL {
        return URL(string: "https://github.com")!
    }
    
    /// request headers
    public var headers: [String: String]? {
        return nil
    }
    /// and so on.
}
  1. Setting configuration for HttpSessionManager.shared.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:[UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
     
    HttpSessionManager.shared..config = RequestConfig()
    return true
}

3.Network request API

enum UserAPI {
    // get user info.
    case userInfo
    // modify user nickname.
    case modifyNickname(nickname: String)
}

extension UserAPI: TargetType {
    var path: String {
        switch self {
        case .userInfo:
            return "UserInfo"
        case .modifyNickname:
            return "modifyNickname"
        }
    }
    var parameters: [String : Any]? {
        switch self {
        case .userInfo:
            return nil
        case let .modifyNickname(nickname: nickname):
            return ["nickname": nickname]
        }
    }
    /// Optional 
    /// Each network request interface, the implementation in the TargetType protocol can be different.
    var customURL: URL? {
        switch self {
        case .userInfo:
            return URL(string: "https://xxx")
        case .modifyNickname:
            return URL(string: "https://yyy")
        }
    }
    /// and so on.
}
  1. Call API like this
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        HttpSessionManager.shared.request(target: MultiTarget(UserAPI.userInfo)) { result in
            switch result {
            case .success(let successRes):
                print(successRes)
            case .failure(let failRes):
                print(failRes.returnMsg)
            }
        }
    }
}

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

  • zhengrusong

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Acknowledgments

  • Hat tip to anyone whose code was used
  • Inspiration
  • etc

httpsessionmanager's People

Contributors

zhengrusong avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

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.