Swiftpack.co - TheAngryDarling/SwiftWebRequestCombine as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by TheAngryDarling.
TheAngryDarling/SwiftWebRequestCombine 1.0.1
Provides Publisher methods, using Swift Combine, to the WebRequest classes
⭐️ 0
🕓 1 year ago
iOS macOS watchOS tvOS
.package(url: "https://github.com/TheAngryDarling/SwiftWebRequestCombine.git", from: "1.0.1")

Web Request Combie

swift >= 5.0 macOS iOS tvOS watchOS Apache 2.0

Provides Publisher methods, using Swift Combine, to the WebRequest classes

Requirements

  • Swift 5.0+

Usage

Data Web Request


let session: URLSession = ....

// From The Web Request itself
let request = WebRequest.DataRequest(URL(string: "http://.....")!, usingSession: session)
let cancellable = request.publisher().sink(
    receiveCompletion: { c in
        switch c in {
            case .finished:
                // finished successfully
            case .failure(let e):
                // finished with error
        }
    },
    receiveValue: { r in 
        // The Data Received
        let dta = r.value
        // The URL Response
        let response = r.response
    }
)

// From the URL Session directly
let cancellable = session.webRequestDataTaskPublisher(for: URL(string: "http://.....")!)
    .sink(
        receiveCompletion: { c in
            switch c in {
                case .finished:
                    // finished successfully
                case .failure(let e):
                    // finished with error
            }
        },
        receiveValue: { r in 
            // The Data Received
            let dta = r.value
            // The URL Response
            let response = r.response
        }
    )

Data Event Web Request


let session: URLSession = ....

// From The Web Request itself
let request = WebRequest.DataRequest(URL(string: "http://.....")!, usingSession: session)
let cancellable = request.dataEventPublisher().sink(
    receiveCompletion: { c in
        switch c in {
            // Note: Cancelling the request and/or publisher
            // will not produce an error here, it will
            // just return finished
            case .finished:
                // finished successfully
            case .failure(let e):
                // finished with error
        }
    },
    receiveValue: { r in 
        // The Data Received
        let dta = r.value
        // The URL Response
        let response = r.response
    }
)

// From the URL Session directly
let cancellable = session.webRequestDataEventTaskPublisher(for: URL(string: "http://.....")!)
    .sink(
        receiveCompletion: { c in
            switch c in {
                case .finished:
                    // finished successfully
                case .failure(let e):
                    // finished with error
            }
        },
        receiveValue: { r in 
            // The Data Received
            let dta = r.value
            // The URL Response
            let response = r.response
        }
    )

Download Web Request


let session: URLSession = ....

// From The Web Request itself
let request = WebRequest.DownloadRequest(URL(string: "http://.....")!, usingSession: session)
let cancellable = request.publisher().sink(
    receiveCompletion: { c in
        switch c in {
            case .finished:
                // finished successfully
            case .failure(let e):
                // finished with error
        }
    },
    receiveValue: { r in 
        // The URL to the location the file was downloaded to
        let url = r.value
        // The URL Response
        let response = r.response
    }
)

// From the URL Session directly
let cancellable = session.webRequestDownloadTaskPublisher(for: URL(string: "http://.....")!)
    .sink(
        receiveCompletion: { c in
            switch c in {
                case .finished:
                    // finished successfully
                case .failure(let e):
                    // finished with error
            }
        },
        receiveValue: { r in 
            // The URL to the location the file was downloaded to
            let url = r.value
            // The URL Response
            let response = r.response
        }
    )

Upload Web Request


let session: URLSession = ....

// From The Web Request itself
let request = WebRequest.UploadRequest(URL(string: "http://.....")!, 
                                       fromFile: URL(....), 
                                       usingSession: session)
let cancellable = request.publisher().sink(
    receiveCompletion: { c in
        switch c in {
            case .finished:
                // finished successfully
            case .failure(let e):
                // finished with error
        }
    },
    receiveValue: { r in 
        // The data response from the upload
        let url = r.value
        // The URL Response
        let response = r.response
    }
)

// From the URL Session directly
let cancellable = session.webRequestUploadTaskPublisher(for: URL(string: "http://.....")!,
                                                        fromFile: URL(...))
    .sink(
        receiveCompletion: { c in
            switch c in {
                case .finished:
                    // finished successfully
                case .failure(let e):
                    // finished with error
            }
        },
        receiveValue: { r in 
            // The data response from the upload
            let url = r.value
            // The URL Response
            let response = r.response
        }
    )

Dependencies

  • WebRequest - The core WebRequest package this package extends to provide Publisher features
  • LittleWebServer - Used in the Test Cases as a local server to test request cases and generate testable responses

Author

  • Tyler Anger - Initial work

License

Copyright 2022 Tyler Anger

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

HERE or http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

GitHub

link
Stars: 0
Last commit: 1 year ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics