EZNetworking is a Swift package that provides a set of awesome utilities to make Swift networking development easier and more fun.
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.
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 stringparameters
: inject a list of 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")
]
)
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)
]
)
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"))
]
)
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"))
]
)
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
)
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
)
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 usingAsyncRequestPerformer()
- If you opt to performing your network requests using callbacks, try using
RequestPerformer()
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)
}
}
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)
}
}
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()
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()
You can easily download files with async/await
using or with completion handlers using FileDownloader()
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
}
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()
You can easily download images with async/await
or with completion handlers using ImageDownloader()
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
}
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()