All Projects → Karumi → BothamNetworking

Karumi / BothamNetworking

Licence: Apache-2.0 license
Networking Framework written in Swift.

Programming Languages

swift
15916 projects
objective c
16641 projects - #2 most used programming language
ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to BothamNetworking

RetryRequestInterceptor-for-OkHttp
a interceptor for OkHttp which can save failed request in storage and will retry request until success or retry times over limit , or request live time over limit
Stars: ✭ 42 (+61.54%)
Mutual labels:  interceptor, retry-requests
grpc-apm-spring-boot-starter
Spring boot starter for gRPC framework with Elastic APM
Stars: ✭ 18 (-30.77%)
Mutual labels:  interceptor
Examples
Examples of Mock Service Worker usage with various frameworks and libraries.
Stars: ✭ 163 (+526.92%)
Mutual labels:  interceptor
ionic4-angular7-httpinterceptor-example
Ionic 4 and Angular 7 Tutorial: HTTP Interceptor Example
Stars: ✭ 15 (-42.31%)
Mutual labels:  interceptor
Arouter
💪 A framework for assisting in the renovation of Android componentization (帮助 Android App 进行组件化改造的路由框架)
Stars: ✭ 13,587 (+52157.69%)
Mutual labels:  interceptor
ogma
A monorepo for the ogma logger and related packages
Stars: ✭ 201 (+673.08%)
Mutual labels:  interceptor
Sieppari
Small, fast, and complete interceptor library for Clojure/Script
Stars: ✭ 133 (+411.54%)
Mutual labels:  interceptor
superagent-intercept
Add functions that will be called during end() e.g. for handling error conditions without having the same code all over the place.
Stars: ✭ 23 (-11.54%)
Mutual labels:  interceptor
Cauldron
C# Toolkit
Stars: ✭ 68 (+161.54%)
Mutual labels:  interceptor
go-grpcmw
A Go package and protobuf generator for managing grpc interceptors.
Stars: ✭ 19 (-26.92%)
Mutual labels:  interceptor
python-grpc-demo
Python + gRPC demos
Stars: ✭ 135 (+419.23%)
Mutual labels:  interceptor
Component
🔥🔥🔥A powerful componentized framework.一个强大、100% 兼容、支持 AndroidX、支持 Kotlin并且灵活的组件化框架
Stars: ✭ 2,434 (+9261.54%)
Mutual labels:  interceptor
spydriver
🕵️ Lightweight utility to intercept WebDriver and WebElement method calls.
Stars: ✭ 24 (-7.69%)
Mutual labels:  interceptor
Interceptor
A browser extension to mock AJAX requests at the browser level
Stars: ✭ 182 (+600%)
Mutual labels:  interceptor
OKHttpLogInterceptor
A Pretty OkHttp Logging Interceptor(一款简洁漂亮的OkHttp Logging拦截器)
Stars: ✭ 16 (-38.46%)
Mutual labels:  interceptor
Spring Boot Examples
个人学习 SpringBoot2.x 写的一些示例程序,目前正在持续更新中.....
Stars: ✭ 159 (+511.54%)
Mutual labels:  interceptor
esa-httpclient
An asynchronous event-driven HTTP client based on netty.
Stars: ✭ 82 (+215.38%)
Mutual labels:  interceptor
dynamic-add-date
一款基于MyBatis框架,可以对插入和更新Sql语句动态地添加日期列和对应值的插件
Stars: ✭ 48 (+84.62%)
Mutual labels:  interceptor
grpc-jwt-spring-boot-starter
Spring boot starter for gRPC framework with JWT authorization
Stars: ✭ 24 (-7.69%)
Mutual labels:  interceptor
logging-interceptor
CDI interceptor for logging to slf4j
Stars: ✭ 25 (-3.85%)
Mutual labels:  interceptor

⚠️ Archived - Use Alamofire 5 or later instead

Karumi logo BothamNetworking Build Status CocoaPods

BothamNetworking is a networking framework written in Swift.

This project will help you to setup all your API clients and implement your networking layer easily. BothamNetworking provides classes to send HTTP requests and obtain HTTP responses ready to parse with just a few lines.

In addition, BothamNetworking adds a Request/Response interceptor mechanisms to be able to modify the HTTPRequest before being sent and the HTTPResponse after being received. This mechanism can be used to implement authentication policies, add headers to a request or add log traces. BothamNetworking contains some already implemented interceptors like NSLogInterceptor, JSONHeadersRequestInterceptor and some authentication mechanisms like BaseAuthentication.

##Usage

This framework contains all the classes needed to implement your networking layer with an easy to use facade named BothamAPIClient. If you love Swift Playgrounds clone this project and review the playgrounds we've created to show how to use this framework.

###Send a request using different HTTP methods:

let apiClient = BothamAPIClient(baseEndpoint: "https://api.github.com/repos/Karumi/BothamNetworking")

apiClient.GET("/issues")

apiClient.POST("/issues")

apiClient.PUT("/issues")

apiClient.DELETE("/issues/1")

###Add headers to the request:

apiClient.GET("/issues", headers: ["User-Agent": "BothamNetworking Headers", "Accept": "application/json; q=0.5"]) 

###Add parameters to the request:

apiClient.DELETE("/issues", parameters: ["id": "1"])

###Add a body to the request:

apiClient.POST("/authorizations", body: ["scopes": ["repo_status", "user:email"]]) 

The body encoding will be determined by the HTTP headers used. To encode your body using json add a "ContentType: application/json" header to your request

###Request execution result:

BothamNetworking uses Result return type composed by HTTPResponse or BothamAPIClientError instances. We have added a ResultType extension to be able to provide an easy to use mechanism to parse your response information using SwiftyJSON as parsing library.

apiClient.GET("/repos") { result in
	result.mapJSON { json in
       for result in json["results"].arrayValue {
			let id = result["id"].stringValue
			let name = result["name"].stringValue
		}
    }
}

This is the information available in the HTTPResponse struct:

public struct HTTPResponse {

    public let statusCode: Int
    public let headers: CaseInsensitiveDictionary<String>?
    public let body: NSData
    
    ...
    
}

The errors BothamNetworking can return are:

public enum BothamAPIClientError: ErrorType, Equatable {

    case HTTPResponseError(statusCode: Int, body: NSData)
    case NetworkError
    case HTTPClientError(error: NSError)
    case ParsingError(error: NSError)
    case UnsupportedURLScheme
    case Retry

}

###Interceptors:

BothamRequestInterceptor and BothamResponseInterceptor are two protocols you can use to modify a HTTPRequest instance before sending it or a HTTPResponse before receiving it. This mechansim can be used to implement authentication policies, add default information to a request, add log traces or retry requests. An example could be NSLogInterceptor, JSONHeadersRequestInterceptor or BasicAuthentication.

class JSONHeadersRequestInterceptor: BothamRequestInterceptor {

	func intercept(request: HTTPRequest) -> HTTPRequest {
        return request.appendingHeaders(["Content-Type": "application/json", "Accept": "application:json"])
    }
}
public protocol BasicAuthentication: BothamRequestInterceptor, BothamResponseInterceptor {
    var credentials: (username: String, password: String) { get }
    func onAuthenticationError(realm: String) -> Void
}

extension BasicAuthentication {
    public func intercept(request: HTTPRequest) -> HTTPRequest {

        let userPass = "\(credentials.username):\(credentials.password)"

        let userPassData = userPass.dataUsingEncoding(NSUTF8StringEncoding)!
        let base64UserPass = userPassData.base64EncodedStringWithOptions([])

        let header = ["Authorization" : "Basic \(base64UserPass)"]

        return request.appendingHeaders(header)
    }

    public func intercept(response: HTTPResponse,
        completion: (Result<HTTPResponse, BothamAPIClientError>) -> Void) {
        if response.statusCode == 401, let unauthorizedHeader = response.headers?["WWW-Authenticate"] {
            let regex = try! NSRegularExpression(pattern: "Basic realm=\"(.*)\"", options: [])
            let range = NSMakeRange(0, unauthorizedHeader.utf8.count)
            if let match = regex.firstMatchInString(unauthorizedHeader, options: [], range: range) {
                let realm = (unauthorizedHeader as NSString).substringWithRange(match.rangeAtIndex(1))
                onAuthenticationError(realm)
            }
        }
        completion(Result.Success(response))
    }
}

Interceptors can be added to a BothamAPIClient instance or to all the BothamAPIClient instances at the same time. Interceptors added globally will be evaluated before and after every request independently of the BothamAPIClient instance. Interceptors added locally will be just appplied to the BothamAPIClient instance where you add those interceptors.

let apiClient = BothamAPIClient(baseEndpoint: "https://api.github.com/repos/Karumi/")

//Add interceptors locally
apiClient.requestInterceptors.append(NSLogInterceptor())
apiClient.responseInterceptors.append(NSLogInterceptor())

//Add interceptors globally
BothamAPIClient.globalRequestInterceptors.append(NSLogInterceptor())
BothamAPIClient.globalResponseInterceptors.append(NSLogInterceptor())

###Retry requests:

To be able to retry a request add a BothamResponseInterceptor and return a BothamAPIClientError.RetryError when needed. BothamAPIClient will automatically retry the original request you sent.

class RetryInterceptor: BothamResponseInterceptor {

	func intercept(response: HTTPResponse, completion: (Result<HTTPResponse, BothamAPIClientError>) -> Void) {
            if response.statusCode == 401 {
                completion(Result.Failure(.RetryError))
            } else {
                completion(Result.Success(response))
            }
    }

}

Be careful when using this mechanism as you can create an infinite loop or DOS yourself. Remember you should always call completion callback to do not break the BothamResponseInterceptor chain.

License

Copyright 2016 Karumi

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].