Skip to content

Aldo10012/EZNetworking

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

82 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EZNetworking

EZNetworking is a Swift package that provides a set of awesome utilities to make Swift networking development easier and more fun.

Table of Content

Installation

Swift Package Manager

To integrate EZnetworking into your Xcode project using Swift Package Manager, add the following dependency to your Package.swift file:

swift Copy code

dependencies: [
    .package(url: "https://github.com/Aldo10012/EZNetworking.git", from: "2.1.0")
]

Alternatively, you can add the package directly through Xcode:

Open your project in Xcode. Go to File > Add Packages.... Enter the package repository URL: https://github.com/Aldo10012/EZNetworking.git. Choose the version and add the package to your project.

Usage

Building a Request

To quickly and easily create a URLRequest utilize the RequestBuilder()

let request = RequestBuilder().build(httpMethod: .GET, urlString: "http://www.example.com", parameters: [])
  • httpMethod: inject the http method you want to use: GET, POST, PUT, DELETE
  • urlString: inject your api url as a string
  • parameters: inject a list of query parameters

How to add query parameters?

Just pass in an array of HTTPParameter into the parameters argument. Here's an example.

let request = RequestBuilder().build(
        httpMethod: .GET,
        urlString: "http://www.example.com",
        parameters: [
            .init(key: "query_param_key_1", value: "query_param_value_1"),
            .init(key: "query_param_key_2", value: "query_param_value_2"),
            .init(key: "query_param_key_3", value: "query_param_value_3")
        ]
    )

How to add headers?

The next argument in you can pass in an array of HTTPHeader. HTTPHeader is an enum where each case is associated with a different http header. Some common ones are .accept(MediaType) and .contentType(MediaType).

Here's an example.

let request = RequestBuilder().build(
        httpMethod: .GET,
        urlString: "http://www.example.com",
        parameters: [
            .init(key: "query_param_key_1", value: "query_param_value_1"),
            .init(key: "query_param_key_2", value: "query_param_value_2"),
            .init(key: "query_param_key_3", value: "query_param_value_3")
        ],
        headers: [
            .accept(.json),
            .contentType(.json)
        ]
    )

What about authorization?

Many API calls require the "Authorization" field. This is handled by HTTPHeader.authorization(Authorization). The most common method of authorization is Bearer. ex: "Authorization": "Bearer YOUR_API_KEY" You can easily do this with the Authorization.bearer(String) Here's an example

let request = RequestBuilder().build(
        httpMethod: .GET,
        urlString: "http://www.example.com",
        parameters: [
            .init(key: "query_param_key_1", value: "query_param_value_1"),
            .init(key: "query_param_key_2", value: "query_param_value_2"),
            .init(key: "query_param_key_3", value: "query_param_value_3")
        ],
        headers: [
            .accept(.json),
            .contentType(.json),
            .authorization(.bearer("Your_API_KEY"))
        ]
    )
How do I set custom headers if not handled by HTTPHeader?

If you are not using "Bearer" for authorizaiton, you can use Authorization.custom(String). Here's an example:

let request = RequestBuilder().build(
        httpMethod: .GET,
        urlString: "http://www.example.com",
        parameters: [
            .init(key: "query_param_key_1", value: "query_param_value_1"),
            .init(key: "query_param_key_2", value: "query_param_value_2"),
            .init(key: "query_param_key_3", value: "query_param_value_3")
        ],
        headers: [
            .accept(.json),
            .contentType(.json),
            .authorization(.custom("custom_non_bearer_value"))
        ]
    )

How to add a body?

Inject a Data object into the body parameter.

let myData: Data()
let request = RequestBuilder().build(
        httpMethod: .GET,
        urlString: "http://www.example.com",
        parameters: [
            .init(key: "query_param_key_1", value: "query_param_value_1"),
            .init(key: "query_param_key_2", value: "query_param_value_2"),
            .init(key: "query_param_key_3", value: "query_param_value_3")
        ],
        headers: [
            .accept(.json),
            .contentType(.json),
            .authorization(.bearer("Your_API_KEY"))
        ],
        body: myData
    )

How do I add a time interval?

Assign a value to timeoutInterval

let myData: Data()
let request = RequestBuilder().build(
        httpMethod: .GET,
        urlString: "http://www.example.com",
        parameters: [
            .init(key: "query_param_key_1", value: "query_param_value_1"),
            .init(key: "query_param_key_2", value: "query_param_value_2"),
            .init(key: "query_param_key_3", value: "query_param_value_3")
        ],
        headers: [
            .accept(.json),
            .contentType(.json),
            .authorization(.bearer("Your_API_KEY"))
        ],
        body: myData,
        timeoutInterval: 30
    )

Performing a Request

You can easily execute Requests using AsyncRequestPerformer() or RequestPerformer() It can manage error handling and is capable of performing requests and returning responses using Async/Await and Completion Handlers.

  • If you opt to performing your network requests using Async/Await, try using AsyncRequestPerformer()
  • If you opt to performing your network requests using callbacks, try using RequestPerformer()

How to get an api response using Async/Await?

func asyncMethodName() async throws {
    let request = RequestBuilder().build(httpMethod: .GET, urlString: "http://www.example.com", parameters: [])!
    let performer = AsyncRequestPerformer()
    
    do {
        let person = try await performer.perform(request: request, decodeTo: Person.self)
        print(person.age, person.name)
    } catch let error as NetworkingError {
        print(error)
    }
}

How to make api call using Async/Await without decoding a response?

func asyncMethodName() async throws {
    let request = RequestBuilder().build(httpMethod: .GET, urlString: "http://www.example.com", parameters: [])!
    let performer = AsyncRequestPerformer()
    
    do {
        try await performer.perform(request: request)
        print("Did succeed")
    } catch let error as NetworkingError {
        print(error)
    }
}

How to get an api response using completion handlers?

let request = RequestBuilder().build(httpMethod: .GET, urlString: "http://www.example.com", parameters: [])
let performer = RequestPerformer()
let task = performer.performTask(request: request, decodeTo: Person.self) { result in
    switch result {
    case .success(let person):
        print(person.name, person.age)
    case .failure(let error):
        print(error)
    }
}
task.resume()

How to make api call using completion handlers without decoding a response?

let request = RequestBuilder().build(httpMethod: .GET, urlString: "http://www.example.com", parameters: [])
let performer = RequestPerformer()
let task = performer.performTask(request: request) { result in
    switch result {
    case .success:
        print("did succeed")
    case .failure(let error):
        print(error)
    }
}
task.reaume()

Download Files

You can easily download files with async/await using or with completion handlers using FileDownloader()

Async/Await

let testURL = URL(string: "https://example.com/example.pdf")!
do {
    let localURL = try await FileDownloader().downloadFile(with: testURL)
    // handle the returned local URL path. Perhaps write and save it in FileManager
} catch let error as NetworkingError{
    // handle error
}

Completion hander

let testURL = URL(string: "https://example.com/example.pdf")!
let task = FileDownloader().downloadFile(url: testURL) { result in
    switch result {
    case .success:
        // handle the returned local URL path. Perhaps write and save it in FileManager
    case .failure(let error):
        // handle error
    }
}
task.resume()

Download Images

You can easily download images with async/await or with completion handlers using ImageDownloader()

Async/Await

let imageURL = URL(string: "https://some_image_url.png")
do {
    let image = try await ImageDownloader().downloadImage(from: imageURL)
    // handle success
} catch let error as NetworkingError {
    // handle error
}

Completion hander

let imageURL = URL(string: "https://some_image_url.png")
let task = ImageDownloader().downloadImageTask(url: imageURL) { result in
    switch result {
    case .success:
        // handle success
    case .failure(let error):
        // handle error
    }
}
task.resume()

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages