GithubHelp home page GithubHelp logo

marinehero / responsedetective Goto Github PK

View Code? Open in Web Editor NEW

This project forked from netguru/responsedetective

0.0 2.0 0.0 325 KB

Sherlock Holmes of the networking layer

Home Page: https://netguru.co/blog/response-detective-ios-open-source

License: MIT License

Swift 93.99% Ruby 1.93% C++ 1.19% Objective-C 2.89%

responsedetective's Introduction

ResponseDetective

ResponseDetective is a non-intrusive framework for intercepting any outgoing requests and incoming responses between your app and your server for debugging purposes.

Usage

Incorporating ResponseDetective in your project is very simple – it all comes down to a couple of steps.

Step 1: Register interceptors

You may register as many request, response and error interceptors as you like.

// request
InterceptingProtocol.registerRequestInterceptor(BaseInterceptor())
InterceptingProtocol.registerRequestInterceptor(JSONInterceptor())

// response
InterceptingProtocol.registerResponseInterceptor(BaseInterceptor())
InterceptingProtocol.registerResponseInterceptor(JSONInterceptor())
InterceptingProtocol.registerResponseInterceptor(HTMLInterceptor())

// error
InterceptingProtocol.registerErrorInterceptor(BaseInterceptor())

Step 2: Register the protocol

For InterceptingProtocol to work, it needs to be added as a middleman between your NSURLSession and the Internet. You can do this by registering it in your session's NSURLSessionConfiguration.protocolClasses.

let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.protocolClasses = [InterceptingProtocol.self]

let session = NSURLSession(configuration: configuration)

If you're using Alamofire as your networking framework, integrating ResponseDetective is as easy as initializing your Manager with NSURLSessionConfiguration described above.

let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.protocolClasses = [InterceptingProtocol.self]

let manager = Alamofire.Manager(configuration: configuration)

Step 3: Profit

Now it's time to perform the actual request.

let request = NSURLRequest(URL: NSURL(string: "http://httpbin.org/get")!)
let task = session.dataTaskWithRequest(request)
task.resume()

Voilà! Now check out your console output.

200 no error

Content-Length: 301
Server: nginx
Content-Type: application/json
Access-Control-Allow-Origin: *
Date: Wed, 08 Jul 2015 13:10:13 GMT
Access-Control-Allow-Credentials: true
Connection: keep-alive

{
  "args" : {

  },
  "headers" : {
    "User-Agent" : "ResponseDetective\/1 CFNetwork\/711.4.6 Darwin\/15.0.0",
    "Accept-Encoding" : "gzip, deflate",
    "Host" : "httpbin.org",
    "Accept-Language" : "en-us",
    "Accept" : "*\/*"
  },
  "origin" : "109.206.223.85",
  "url" : "http:\/\/httpbin.org\/get"
}

Available interceptors

Interceptor Description
HeadersInterceptor Intercepts all requests and responses and displays their metadata, including errors.
HTMLInterceptor Intercepts only HTML requests and responses.
ImageInterceptor Intercepts only image responses.
JSONInterceptor Intercepts only JSON requests and responses.
PlainTextInterceptor Intercepts only plain text requests and responses.
XMLInterceptor Intercepts only XML requests and responses.

Installation

Carthage

If you're using Carthage, just add the following dependency to your Cartfile:

github "netguru/ResponseDetective"

CocoaPods

Using ResponseDetective with CocoaPods is as easy as adding the following dependency to your Podfile:

pod 'ResponseDetective'

About

Maintainers

Adrian Kashivskyy

Aleksander Popko

License

ResponseDetective is licensed under the MIT License. See LICENSE.md.

responsedetective's People

Contributors

akashivskyy avatar

Watchers

James Pereira avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.