Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Oct 29, 2021. It is now read-only.

Versatile HTTP Networking in Swift

License

NotificationsYou must be signed in to change notification settings

intelygenz/NetClient-iOS

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TwitterVersionLicensePlatformSwiftCarthage compatibleSwift Package Manager CompatibleBuild StatusHelp Contribute to Open Source

Net is a versatile HTTP networking library written in Swift.

🌟 Features

  • URL / JSON / Property List Parameter Encoding
  • Upload File / Data / Stream / Multipart Form Data
  • Download File using Request or Resume Data
  • Authentication with URLCredential
  • Basic, Bearer and Custom Authorization Handling
  • Default and Custom Cache Controls
  • Default and Custom Content Types
  • Upload and Download Progress Closures with Progress (only iOS >= 11)
  • cURL Command Debug Output
  • Request and Response Interceptors
  • Asynchronous and synchronous task execution
  • Inference of response object type
  • Network reachability
  • TLS Certificate and Public Key Pinning
  • Retry requests
  • Codable / Decodable / Encodable protocols compatible (JSON / Property List)
  • Customizable acceptable status codes range
  • watchOS Compatible
  • tvOS Compatible
  • macOS Compatible
  • Alamofire Implementation
  • MoyaProvider Extension
  • Kommander Extension
  • RxSwift Extension
  • Stub Implementation

📋 Requirements

  • iOS 8.0+ / macOS 10.9+ / tvOS 9.0+ / watchOS 2.0+
  • Xcode 9.0+
  • Swift 4.0+

📲 Installation

Net is available throughCocoaPods. To installit, simply add the following line to your Podfile:

pod'NetClient'

For Swift 3 compatibility use:

pod'NetClient','~> 0.2'

Or you can install it withCarthage:

github "intelygenz/NetClient-iOS"

Or install it withSwift Package Manager:

dependencies:[.package(url:"https://github.com/intelygenz/NetClient-iOS.git")]

🐒 Usage

Build a NetRequest

import Netdo{letrequest=tryNetRequest.builder("YOUR_URL")!.setAccept(.json).setCache(.reloadIgnoringLocalCacheData).setMethod(.PATCH).setTimeout(20).setJSONBody(["foo","bar"]).setContentType(.json).setServiceType(.background).setCacheControls([.maxAge(500)]).setURLParameters(["foo":"bar"]).setAcceptEncodings([.gzip,.deflate]).setContentEncodings([.gzip]).setBasicAuthorization(user:"user", password:"password").setHeaders(["foo":"bar"]).build()}catch{print("Request error:\(error)")}

Request asynchronously

import Netletnet=NetURLSession()net.data(URL(string:"YOUR_URL")!).async{(response, error)indo{iflet object:[AnyHashable:Any]=try response?.object(){print("Response dictionary:\(object)")}elseiflet error= error{print("Net error:\(error)")}}catch{print("Parse error:\(error)")}}

Request synchronously

import Netletnet=NetURLSession()do{letobject:[AnyHashable:Any]=try net.data("YOUR_URL").sync().object()print("Response dictionary:\(object)")}catch{print("Error:\(error)")}

Request from cache

import Netletnet=NetURLSession()do{letobject:[AnyHashable:Any]=try net.data("YOUR_URL").cached().object()print("Response dictionary:\(object)")}catch{print("Error:\(error)")}

Track progress

import Netletnet=NetURLSession()do{lettask=try net.data("YOUR_URL").progress({ progressinprint(progress)}).sync()}catch{print("Error:\(error)")}

Add interceptors for all requests

import Netletnet=NetURLSession()net.addRequestInterceptor{ requestin    request.addHeader("foo", value:"bar")    request.setBearerAuthorization(token:"token")return request}

Retry requests

import Netletnet=NetURLSession()net.retryClosure={ response, _, _in response?.statusCode== XXX}do{lettask=try net.data("YOUR_URL").retry({ response, error, retryCountinreturn retryCount<2}).sync()}catch{print("Error:\(error)")}

🧙‍♂️ Codable

Encodable

import Netletrequest=NetRequest.builder("YOUR_URL")!.setJSONObject(Encodable()).build()

Decodable

import Netletnet=NetURLSession()do{letobject:Decodable=try net.data("YOUR_URL").sync().decode()print("Response object:\(object)")}catch{print("Error:\(error)")}

🤝 Integrations

pod'NetClient/Alamofire'
import Netletnet=NetAlamofire()...

LoveMoya?

pod'NetClient/Moya'
import Netimport Moyaletrequest=NetRequest("YOUR_URL")!letprovider=MoyaProvider<NetRequest>()provider.request(request){ resultinswitch result{caselet.success(response):print("Response:\(response)")caselet.failure(error):print("Error:\(error)")}}
pod'NetClient/Kommander'
import Netimport Kommanderletnet=NetURLSession()letkommander=Kommander.defaultnet.data(URL(string:"YOUR_URL")!).execute(by: kommander, onSuccess:{ objectinprint("Response dictionary:\(objectas[AnyHashable:Any])")}){ errorinprint("Error:\(String(describing: error?.localizedDescription))")}net.data(URL(string:"YOUR_URL")!).executeDecoding(by: kommander, onSuccess:{ objectinprint("Response object:\(objectasDecodable)")}){ errorinprint("Error:\(String(describing: error?.localizedDescription))")}

LoveRxSwift?

pod'NetClient/RxSwift'
import Netimport RxSwiftletrequest=NetRequest("YOUR_URL")!_= net.data(request).rx.response().observeOn(MainScheduler.instance).subscribe{print($0)}

Stub Implementation

pod'NetClient/Stub'
import Netletnet=NetStub()net.asyncBehavior=.delayed(.main,.seconds(10)) // If you want to delay the response.net.nextResult=.response(NetResponse.builder()....build())// Your test request herenet.nextResult=.error(.net(code:500, message:"Your network error.", headers:..., object:..., underlying:...))// Your test request here

❤️ Etc.

  • Contributions are very welcome.
  • Attribution is appreciated (let's spread the word!), but not mandatory.

👨‍💻 Authors

alexruperez,alejandro.ruperez@intelygenz.com

👮‍♂️ License

Net is available under the MIT license. See the LICENSE file for more info.


[8]ページ先頭

©2009-2025 Movatter.jp