Coder Social home page Coder Social logo

curllog's Introduction

CURLLog - A tool to simplify web API development.

Converts NSURLSessionTasks and NSURLConnections to curl commands and print them to the console. This tool can accelerate development and debugging of API services and allows you to see what's actually going on.

For example logging a request to get the weather:

2016-02-04 12:52:47.000 CURLExample[24182:590796] 
**************** HTTP SUCCESS 200 **********************
**** REQUEST ****
curl -X GET "http://api.openweathermap.org/data/2.5/weather?lat=37.78&lon=-122.41&APPID=4199a667b2597ff5b28f33ec06d6a31b" | python -m json.tool | pbcopy
**** RESPONSE ****
{
    base = "cmc stations";
    clouds =     {
        all = 0;
    };
    cod = 200;
    coord =     {
        lat = "37.77";
        lon = "-122.42";
    };
    dt = 1454617809;
    id = 5391959;
    main =     {
        "grnd_level" = "1035.51";
        humidity = 85;
        pressure = "1035.51";
        "sea_level" = "1043.98";
        temp = "285.82";
        "temp_max" = "285.82";
        "temp_min" = "285.82";
    };
    name = "San Francisco";
    sys =     {
        country = US;
        message = "0.008500000000000001";
        sunrise = 1454598632;
        sunset = 1454636235;
    };
    weather =     (
                {
            description = "Sky is Clear";
            icon = 01d;
            id = 800;
            main = Clear;
        }
    );
    wind =     {
        deg = "61.5011";
        speed = "2.76";
    };
}
****************************************************

Notice that the logged curl command from above:

curl -X GET "http://api.openweathermap.org/data/2.5/weather?lat=37.78&lon=-122.41&APPID=4199a667b2597ff5b28f33ec06d6a31b"

You can paste this into terminal to rerun the command. You can also modify the request after you paste it.

For convenience the curl command is piped into some other commands to copy pretty json to your clipboard. This pipe command can be modified by setting the outputType variable on CURLLog.sharedInstance.

curl -X GET "http://api.openweathermap.org/data/2.5/weather?lat=37.78&lon=-122.41&APPID=4199a667b2597ff5b28f33ec06d6a31b" | python -m json.tool | pbcopy

Usage

First import the header file (to your bridging header if using Swift)

#import "CURLLog.h"

To log request and response together, call one of the provided methods on CURLLog.sharedInstance in the completion block.

Obj-C
NSURL *url = [NSURL URLWithString:<your url string>];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

// Must use __block qualifier so that the task variable is still alive when we use it inside the completion handler.
__block NSURLSessionDataTask *task = [_session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    if(error != nil) {
        [[CURLLog sharedInstance] logCURLForTask:task error:error];
    } else {
        [[CURLLog sharedInstance] logCURLForTask:task payload:data];
    }
}];

[task resume];
Swift
let request = NSURLRequest(URL: url)

// Must declare task as optional variable so that it can be captured by the closure
var task: NSURLSessionTask? = nil
task = session.dataTaskWithRequest(request, completionHandler: { (data: NSData?, request: NSURLResponse?, error: NSError?) -> Void in
    if let error = error {
        CURLLog.sharedInstance().logCURLForTask(task, error: error)
    } else {
        CURLLog.sharedInstance().logCURLForTask(task, payload: data)
    }
})

task?.resume()

If you aren't concerned with the response then you can log outside of the completion handler.

Obj-C
NSURL *url = [NSURL URLWithString:<your url string>];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

NSURLSessionDataTask *task = [_session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    // handle your business...
}];

[[CURLLog sharedInstance] logCURLForTask:task];

[task resume];

Swift
let request = NSURLRequest(URL: url)

let task = session.dataTaskWithRequest(request, completionHandler: { (data: NSData?, request: NSURLResponse?, error: NSError?) -> Void in
    // Handle your business...
})

CURLLog.sharedInstance().logCURLForTask(task)

task?.resume()

Example project

Clone this repo and open CURLExample.xcodeproj which include a Swift example and Obj-C example.

curllog's People

Contributors

zakkhoyt avatar

Watchers

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.