Slide 1

Slide 1 text

The Sonora Software Developers Group

Slide 2

Slide 2 text

Help yourself to food and drink Mix, mingle, and chat Thanks to Front Porch for the food, drink, and location Welcome

Slide 3

Slide 3 text

Agenda • Introduction • JSON • REST and RESTful Web Services • Github’s REST API • Q&A

Slide 4

Slide 4 text

The web is packed full of web APIs just waiting to be consumed. Sites such as Meetup, GitHub, Twitter, LinkedIn, and Facebook to name a few all have developer APIs that can be used programmatically to consume the data contained within their site. By implementing their APIs in similar fashions using REST, JSON, and OAuth, the ease at which these APIs can be consumed is amazing. For this talk, I am going to discuss how to consume GitHub’s RESTful web API using web resources, NuGet packages, the .NET framework, and a few lines of C# code. Along the way, I will also touch on topics such as JSON, REST, Schemas, and the HTTP protocol. JSON Web API Consumption

Slide 5

Slide 5 text

A lightweight text-based open standard designed for human-readable data interchange. It is derived from the JavaScript scripting language for representing simple data structures and associative arrays, called objects. Despite its relationship to JavaScript, it is language-independent, with parsers available for many languages. JSON – JavaScript Object Notation { "firstName": "John", "lastName" : "Smith", "age" : 25, "address" : { "streetAddress": "21 2nd Street", "city" : "New York", "state" : "NY", "postalCode" : "10021" }, "phoneNumber": [ { "type" : "home", "number": "212 555-1234" }, { "type" : "fax", "number": "646 555-4567" } ] }

Slide 6

Slide 6 text

JSON Source http://coderwall.com/scottksmith95.json JSON Formatter and Validator http://jsonformatter.curiousconcept.com/ JSON Demo

Slide 7

Slide 7 text

A style of software architecture for distributed systems such as the World Wide Web. REST has emerged over the past few years as a predominant Web service design model by displacing other design models such as SOAP and WSDL due to its simpler style.  Client-server  Stateless  Cacheable  Layered System  Code and demand (optional)  Uniform interface REST – Representational State Transfer

Slide 8

Slide 8 text

A RESTful web service is a web service implemented using HTTP and the principles of REST. It is a collection of resources, with four defined aspects:  The base URI for the web service such as http://example.com/resources  The content type of the data supported by the web service. This could be JSON, XML, or any other valid Internet media type  The set of operations supported by the web service such as GET, PUT, POST, or DELETE.  The API must be hypertext driven. RESTful Web Services

Slide 9

Slide 9 text

Resource GET PUT POST DELETE Collection URI, such as http://example.com/resources/ List the URIs and perhaps other details of the collection's members. Replace the entire collection with another collection. Create a new entry in the collection. The new entry's URL is assigned automatically and is usually returned by the operation. Delete the entire collection. Element URI, such as http://example.com/resource/item17 Retrieve a representation of the addressed member named item17 of the collection. Replace the addressed member of the collection, or if it doesn't exist, createit. Treat the addressed member as a collection in its own right and create a new entry in it. Delete the addressed member of the collection. RESTful Web Service HTTP Methods

Slide 10

Slide 10 text

The first thing that should always be done is to seek out documentation for the web API. The current version for GitHub’s is v3 and can be found here: http://developer.github.com/v3/. By looking at the documentation, you will notice that the first page discusses topics such as Schema, Client Errors, HTTP Verbs, Authentication, Pagination, Rate Limiting, and a few others. While all are equally important, we will be focusing on Schema. GitHub’s REST API

Slide 11

Slide 11 text

 All API access is over HTTPS and accessed from the api.github.com domain.  All data is sent and received as JSON.  Blank fields are included as null instead of being omitted.  All timestamps are returned in ISO 8601 format:  YYYY-MM-DDTHH:MM:SSZ GitHub’s REST API Schema

Slide 12

Slide 12 text

API Call https://api.github.com/users/scottksmith95 JSON to C# Converter http://json2csharp.com/ JSON.NET http://james.newtonking.com/projects/json-net.aspx GitHub’s REST API Demo

Slide 13

Slide 13 text

Q & A

Slide 14

Slide 14 text

 Scott Smith  [email protected]  Blog: http://scottksmith95.wordpress.com/2012/05/01/json-web-api-consumption/  Code: https://github.com/scottksmith95/JSON-Web-API-Consumption  Gists: https://gist.github.com/scottksmith95  Slides: https://speakerdeck.com/u/scottksmith95 Resources