GOONetwork

GOONetwork is a small library that provides a convenient way to decode JSON from a URL using the decodeJSON method.

⚙️ Installation

Setup with Swift Package Manager

?‍? Compatibility

  • macOS 12+
  • iOS 13.0+
  • Xcode 12.0+

? Usage

To use GOONetwork, import the library in your code:

import Foundation
import GOONetwork

The decodeJSON method is available on the GOONetwork struct. It has the following signature:

public struct GOONetwork {
  static public func decodeJSON<T: Decodable>(fromUrl: String) async throws -> T?
}

To use decodeJSON, pass the URL of the JSON you want to decode as a string to the fromUrl parameter. The decodeJSON method returns a decoded optional object of type T asynchronously.

struct Product: Decodable {
    let title: String
    let description: String
}
let jsonUrl = "https://example.com/data.json"
let decodedObject: Product? = try await GOONetwork.decodeJSON(fromUrl: jsonUrl)

⛔ Error Handling

The decodeJSON method throws an error if something goes wrong during the decoding process. The following errors can be thrown:

  • NetworkError.urlError: This error is thrown if the provided URL is not valid.
  • NetworkError.responseError: This error is thrown if the HTTP response status code is not 200.
  • NetworkError.dataError: This error is thrown if there is an error getting the data from the provided URL.
  • NetworkError.decodeError: This error is thrown if there is an error decoding the JSON.

You can catch these errors using a catch block in your code. For example:

do {
  let decodedObject: Product? = try await GOONetwork.decodeJSON(fromUrl: jsonUrl)
} catch let err as NetworkError {
  // Handle the error here
}

?️ Created with

✒️ Author

Goojoob.devOriginal developmentgoojoob

? License

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution 4.0 International license (CC BY 4.0).

? Thank You

  • Talk to others about this project ?
  • We can have a ☕ whenever you want

GitHub

View Github