Skip to content

goojoob/GOONetwork

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

7 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

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.dev - Original development - goojoob


πŸ“„ License

Creative Commons License
This 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

About

Network Library to decode API Rest URL into Swift Object

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages