Skip to content

💻 A Swift package for flexible networking in SwiftUI, offering JSON and data fetching, file uploading, and downloading using Escaping Closures, Combine, and async-await.

License

Notifications You must be signed in to change notification settings

MMMagicCoder/SwiftNet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SwiftNet

SwiftNet is a Swift package designed to simplify networking operations in SwiftUI. It offers three flexible approaches for fetching, downloading, and uploading data or files using Escaping Closures, Combine, and async-await techniques, allowing you to choose the method that best suits your project requirements.

Table of contents

Requirements

  • SwiftUI
  • iOS 13.0 or above

Installation

You can access SwiftNet through Swift Package Manager.

Swift Package Manager (SPM)

In xcode select:

File > Swift Packages > Add Package Dependency...

Then paste this URL:

https://github.com/MMMagicCoder/SwiftNet.git

Usage

SwiftNet provides a streamlined way to perform networking tasks. Whether you're fetching small data, downloading large files, or uploading content, you can choose between Escaping Closures, Combine, or async-await methods. To use these methods, instantiate the appropriate manager:

  • let networkManager = EscapingNetworkManager() for Escaping Closures.
  • let networkManager = CombineNetworkManager() for Combine.
  • let networkManager = AsyncNetworkManager() for async-await.

Below are examples for each type of task.

Fetching Data

SwiftNet provides two types of data fetching: JSON Fetching and Data Fetching. For JSON fetching, your model must conform to the FetchableModel protocol, which ensures the proper structure for decoding JSON responses.

  • JSON Fetching

You can fetch JSON data by using the following methods, depending on the approach you choose. Make sure your model conforms to FetchableModel:

struct DataModel: FetchableModel {
    let userId: Int
    let id: Int
    let title: String
    let body: String
}

Using Escaping Closures

 networkManager.fetchJSON(fromURL: "https://example.com/api/data") { (result: [MyModel]?, response, error) in
    if let result = result {
        // Handle the decoded JSON result
    } else if let error = error {
        // Handle the error
    }
}

Using Combine

  networkManager.fetchJSON(fromURL: url) { (returnedData: [DataModel]?, response, error) in
     if let result = result {
         // Handle the decoded JSON result
    } else if let error = error {
        // Handle the error
    }
}

Using async-await

Task {
    do {
        let result: [MyModel] = try await networkManager.fetchJSON(fromURL: "https://example.com/api/data")
        // Handle the decoded JSON result
    } catch {
        // Handle the error
    }
}
  • Data Fetching

    Data fetching is used when you need to fetch raw data (e.g., images, binary files) without decoding it into a specific model.

    Using Escaping Closures

  networkManager.fetchData(fromURL: "https://example.com/file") { data, response, error in
    if let data = data {
        // Handle the fetched data
    } else if let error = error {
        // Handle the error
    }
}

Using Combine

 networkManager.fetchData(fromURL: url) { (returnedData, response, error) in
     if let result = result {
         // Handle the returned data
    } else if let error = error {
        // Handle the error
    }
}

Using async-await

Task {
    do {
        let data = try await networkManager.fetchData(fromURL: "https://example.com/file")
        // Handle the fetched data
    } catch {
        // Handle the error
    }
}

Downloading Files

Using Escaping Closures Or Combine

networkManager.downloadData(fromURL: url) { tempLocalUrl, response, error in
    if let tempLocalUrl = tempLocalUrl {
       // Get data from tempLocalUrl url
  } else if error = error {
       // Handle the error
  }
}

Using async-await

Task {
    do {
        let fileURL = try await networkManager.downloadFile(using: .async)
        // Use the file
    } catch {
        // Handle the error
    }
}

Uploading Data

Using Escaping Closures Or Combine

networkManager.uploadData(toURL: url, data: json, mimeType: .json) { response , error in
   if let error = error {
       // Handle the error
  } else {
       // Handle the response 
  }
}

Using async-await

  Task {
      do {
          let returnedResponse = try await networkManager.uploadData(toURL: url, data: json, mimeType: .json)
              // Handle the response
   } catch {
             // Handle the error
  }
}

Contribution

If you encounter any issues, feel free to open an issue. Contributions are also welcome, whether it's bug fixes, new features, or documentation improvements.

License

NetworkKit is distributed under the MIT License.

MIT License

Copyright (c) 2024 Mohammad Mahdi Moayeri

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

About

💻 A Swift package for flexible networking in SwiftUI, offering JSON and data fetching, file uploading, and downloading using Escaping Closures, Combine, and async-await.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages