This is a package usefull to make simple HTTP requests and upload some files.
The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift
compiler.
Once you have your Swift package set up, adding Simple Newtwork as a dependency is as easy as adding it to the dependencies
value of your Package.swift
.
dependencies: [
.package(url: "https://github.com/Armay2/SimplyNetwork", .upToNextMajor(from: "5.4.0"))
]
We provide you diffrents methods for making HTTP requests
This is one way of making a request. The full definition looks like this
open func request<T: Codable>(_ strUrl: String,
method: HTTPMethod = .get,
parameters: Parameters? = nil,
headers: HTTPHeaders? = nil,
paramDestination: ParamDestination? = .methodDependent,
_ completion: @escaping CodableRequestCompletion<T>) throws {
Notice that's not the only definition, you can also request directly some data.
Request Codable Object
Call request with determide object to decode
var myObject: MyObject?
SN.request("https://yourUrl/content/") { (result: Result<(MyObject, URLResponse), Error>) in
switch result {
case .success((let object, let response)):
myObject = object
print(response)
case .failure(let err):
print(error)
}
}
Your Object must conform to Codable
Protocol
Request some data
try? SN.request(url) { (result: Result<(Data, URLResponse), Error>) in
switch result {
case .success((let dat, _)):
data = dat
case .failure(let err):
error = err
}
}
let parameters: [String: String] = [
"username": "name@doamin.com",
"password": "password",
"locale": "en-gb"]
var headers = HTTPHeaders()
headers.add(name: "Content-Type", value: "application/x-www-form-urlencoded")
headers.add(name: "Accept", value: "application/json")
try? SN.request("https://httpbin.org", method: .post, parameters: parameters, headers: headers, paramDestination: .httpBody) { (result: Result<(MyObject, URLResponse), Error>) in
switch result {
case .success((let obj, let response)):
print(obj)
print(response)
case .failure(let err):
print(error)
}
}
open func uploadFile(fileURL: URL,
to targetURL: String,
completion: @escaping DataRequestCompletion) throws {
open func uploadFile(dataToSend: Data,
to targetURL: String,
completion: @escaping DataRequestCompletion) throws {
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Distributed under the MIT License. See LICENSE
for more information.
Arnaud: @NAUNAU22
Project Link: https://github.com/Armay2/SimplyNetwork