Armay2 / SimplyNetwork

A simple swift package for http requests

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

swift-version swift-package-manager Platforms

SimpleNetworkBanner

Table of content

About

This is a package usefull to make simple HTTP requests and upload some files.

Installation

Swift Package Manager

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"))
]

Exemples

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.

Simple request

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
	}
}

Request with full configuration

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)
	}
}

Upload a file

open func uploadFile(fileURL: URL,
                         to targetURL: String,
                         completion: @escaping DataRequestCompletion) throws {
                         
open func uploadFile(dataToSend: Data,
                         to targetURL: String,
                         completion: @escaping DataRequestCompletion) throws {                        

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Arnaud: @NAUNAU22

Project Link: https://github.com/Armay2/SimplyNetwork

About

A simple swift package for http requests


Languages

Language:Swift 100.0%