GithubHelp home page GithubHelp logo

hammock2's Introduction

hammock2

hammock2 is a single .cs file for making munchy munchy API. Fully dynamic, so no "client library" required.

LOL jk there's dependencies

	PM> Install-Package Microsoft.Net.Http	# Or provide your own implementation of IHttpEngine
	PM> Install-Package ServiceStack.Text	# Or provide your own implementation of IMediaConverter
	PM> Install-Package AsyncBridge         # Only required if your project is .NET 4.0, not .NET 4.5

Usage

// Spin up a client
dynamic twitter = new Http("https://api.twitter.com");

// GET https://api.twitter.com/users/show.json?screen_name=danielcrenna
var reply = twitter.Users.Show.Dot.Json(screen_name: "danielcrenna");    
var response = reply.Response;
var user = reply.Body;

// Get the result
Console.WriteLine(response.StatusCode + response.ReasonPhrase); // 200 OK
Console.WriteLine(user.ScreenName + ": " + user.Status.Text);   // danielcrenna: Best. Library. Evar.

Bring your own web stack

hammock2 needs engines for HTTP and JSON. If you don't want to use the defaults then don't bring in the partial classes that implement them. If you take only the hammock.cs file, then you need to implement these interfaces to provide your own serializer implementation:

public interface IMediaConverter
{
    string DynamicToString(dynamic instance);
    IDictionary<string, object> StringToHash(string json);
    string HashToString(IDictionary<string, object> hash);
    T DynamicTo<T>(dynamic instance);
    T StringTo<T>(string instance);
}

public interface IHttpEngine
{
    dynamic Request(string url, string method, NameValueCollection headers, dynamic body, bool trace);
}

Concrete addiction

If you really need concrete classes, you can do that too, by deserializing the whole body, or a portion of it:

public class StripeCustomer
{
    public string Id { get; set; }
	// ...
}

public class Stripe
{
    private readonly dynamic _stripe;
    public Stripe()
    {
        _stripe = new Http("https://api.stripe.com/v1/");
        _stripe.Auth = HttpAuth.Basic(ConfigurationManager.AppSettings["StripeTestKey"]);
    }
    public IEnumerable<StripeCustomer> GetCustomers()
    {
        // GET https://api.stripe.com/v1/customers
        var reply = _stripe.Customers();
        
        // { count : "2", data: [{customer},{customer}] }
        var body = reply.Body;
        
        // Cherry-pick the data element where customers live
        var customers = body.Deserialize<IEnumerable<StripeCustomer>>(body.Data);
        return customers;
    }
}

Tracing requests and responses:

// Uses stock .NET trace writer
var api = new Http("https://awe.sm");
api.Trace = true;

Authentication

Use the HttpAuth helper:

_stripe = new Http("https://api.stripe.com/v1/");
_stripeKey = ConfigurationManager.AppSettings["StripeTestKey"];
_stripe.Auth = HttpAuth.Basic(_stripeKey);

Pass in your own custom pre-request code:

Action<Http> auth = http =>
{
    var authorization = Convert.ToBase64String(Encoding.UTF8.GetBytes(_stripeKey + ":"));
    http.Headers.Add("Authorization", "Basic " + authorization);
};
_stripe.Auth = auth;

TODO

  • match method prefix to web method
  • content negotiation / formatters
  • async/await

hammock2's People

Contributors

danielcrenna avatar dependabot[bot] avatar hyeongukryu avatar

Watchers

 avatar  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.