目录
目录README.md

CombineURLSession

Use this package in order to ease up working with Combine URLSession. We support working with Codable for all main HTTP methods GET, POST, PUT and DELETE. We also support MultipartUpload

Instalation

To install this package just add the follwing in yor package

.package(url: "https://github.com/DanielMandea/url-session-combine.git", from: "1.0.0"),

Usage

Define SomeService Example


class SomeService: BaseService {
    
    // MARK: - Session
    
    let sessionProvider: SessionProvider
    
    // MARK: - Init
    
    init(api: API = ServiceConfiguration.api, sessionProvider: SessionProvider = DefaultSessionProvider(authenticatorProvider: Authenticator())) {
        self.sessionProvider = sessionProvider
        super.init(api: api)
    }
    
    func get<T: Codable>(for path: String, headers: [String: String] = ["Content-Type":"application/json"], decoder: JSONDecoder = JSONDecoder.iso8601JsonDecoder) -> AnyPublisher<T, Error> {
        Publishers.CombineLatest(just(path: path), sessionProvider.jwt()).flatMap {
            self.get(from: $0, headers: self.merge(jwt: $1, with: headers), decoder: decoder)
        }.eraseToAnyPublisher()
    }
}

Call SomeService Example

    get(for: "somepath/", decoder: JSONDecoder.iso8601FullDateJsonDecoder)

Multipart Upload Example

    Publishers.CombineLatest(just(path: athletes), sessionProvider.jwt()).map {
        URLRequest.multipart(for: $0, 
                          method: .POST, 
                          headers: ["Authorization": $1, "Accept-Encoding": "gzip, deflate, br"], 
                          payload: value, 
                          multiparts: [Multipart(key: "profilePhoto", data: profileImageData, mimeType: .jpeg, fileName: "\(UUID().uuidString).jpeg")],
                          encoder: JSONEncoder.iso860JSONEncoder, 
                          decoder: JSONDecoder.iso8601FullDateJsonDecoder)
    }.flatMap {
        self.task(for: $0, decoder: JSONDecoder.iso8601FullDateJsonDecoder)
    }
    .eraseToAnyPublisher()
关于
58.0 KB
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

©Copyright 2023 CCF 开源发展委员会
Powered by Trustie& IntelliDE 京ICP备13000930号