Using Web Services with JavaScript - Fronttrends 2010

128
Chris&an Heilmann Fron0rends, Warsaw, Poland, October 2010 Using web services in JavaScript.
  • date post

    12-Sep-2014
  • Category

    Documents

  • view

    5.821
  • download

    0

description

 

Transcript of Using Web Services with JavaScript - Fronttrends 2010

Page 1: Using Web Services with JavaScript - Fronttrends 2010

Chris&an HeilmannFron0rends, Warsaw, Poland, October 2010

Using web services in JavaScript.

Page 2: Using Web Services with JavaScript - Fronttrends 2010
Page 3: Using Web Services with JavaScript - Fronttrends 2010

What is the web?

Page 4: Using Web Services with JavaScript - Fronttrends 2010

Was ist das Web?

Data + Interfaces

Page 5: Using Web Services with JavaScript - Fronttrends 2010

Lots of yummy, yummy data.

Page 6: Using Web Services with JavaScript - Fronttrends 2010

If a company has a bit of brains these days then they build on and release an API for their services.

Page 7: Using Web Services with JavaScript - Fronttrends 2010
Page 8: Using Web Services with JavaScript - Fronttrends 2010

Company benefits:

People bring your data and services into environments you could never reach.

Page 9: Using Web Services with JavaScript - Fronttrends 2010

Our benefits:

solve problems and get information without buying data or writing code.

Page 10: Using Web Services with JavaScript - Fronttrends 2010

Solving 2 problems.

Page 11: Using Web Services with JavaScript - Fronttrends 2010

Problem #1: Distance between two places on Earth.

Page 12: Using Web Services with JavaScript - Fronttrends 2010

Sometimes it would be good to know the distance between two places on Earth.

Page 13: Using Web Services with JavaScript - Fronttrends 2010

For this you could use any mapping service and navigate from one to the other.

Page 14: Using Web Services with JavaScript - Fronttrends 2010

But what if you want to have that as plain information?

Page 15: Using Web Services with JavaScript - Fronttrends 2010

Battle Plan:1. Find the location of the two places

on Earth

2. Calculate the distance.

Page 16: Using Web Services with JavaScript - Fronttrends 2010

Yahoo GeoPlanet is a data set that has information about the location of places on Earth.

Page 17: Using Web Services with JavaScript - Fronttrends 2010

Yahoo GeoPlanet is a data set that has information about the location of places on Earth.

http://developer.yahoo.com/geo/geoplanet/

Page 18: Using Web Services with JavaScript - Fronttrends 2010
Page 20: Using Web Services with JavaScript - Fronttrends 2010

This gives us the latitude and longitude of both places...

Page 21: Using Web Services with JavaScript - Fronttrends 2010

...but how do we calculate the distance?

Page 23: Using Web Services with JavaScript - Fronttrends 2010

Putting it all together...

Page 24: Using Web Services with JavaScript - Fronttrends 2010

Putting it all together...

Page 25: Using Web Services with JavaScript - Fronttrends 2010
Page 26: Using Web Services with JavaScript - Fronttrends 2010
Page 27: Using Web Services with JavaScript - Fronttrends 2010
Page 28: Using Web Services with JavaScript - Fronttrends 2010

Problem #1: Foreign Tweets.

Page 29: Using Web Services with JavaScript - Fronttrends 2010

Twitter supports different languages, but has no translation service.

Page 30: Using Web Services with JavaScript - Fronttrends 2010

Google has a translation service though.

Page 31: Using Web Services with JavaScript - Fronttrends 2010

Battle Plan:1. Investigate Twitter’s search API and

Google’s translation API and if needed, get keys.

2. Get the results from Twitter for a certain search.

3. Loop over the results, see which ones are not in English, and then translate them with the Google Translation API.

Page 32: Using Web Services with JavaScript - Fronttrends 2010

The solution is very similar to the other solution.

Page 33: Using Web Services with JavaScript - Fronttrends 2010

It also suffers from the same issues.

Page 34: Using Web Services with JavaScript - Fronttrends 2010

Not fun with JS:1. Asynchronous lookups with

generated script nodes are a pain to get right - what if one breaks?

2. Depending on how many Tweets are not in English, you have to hammer Google’s translation API which slows down your overall app.

Page 35: Using Web Services with JavaScript - Fronttrends 2010

Simplifying access.

Page 36: Using Web Services with JavaScript - Fronttrends 2010

Using several APIs in conjunction in JavaScript is not fun.

Page 37: Using Web Services with JavaScript - Fronttrends 2010

It doesn’t matter if that is internally or externally in a company.

Page 38: Using Web Services with JavaScript - Fronttrends 2010

Yahoo had a lot of these problems...

Page 39: Using Web Services with JavaScript - Fronttrends 2010

...which is why they built YQL to work around that.

Page 40: Using Web Services with JavaScript - Fronttrends 2010

Using YQL, web services become databases you can access like any other DB.

Page 41: Using Web Services with JavaScript - Fronttrends 2010

YQL http://developer.yahoo.com/yql/console/

Page 42: Using Web Services with JavaScript - Fronttrends 2010

YQL http://developer.yahoo.com/yql/console/

select {what} from {where} where {conditions}

Page 43: Using Web Services with JavaScript - Fronttrends 2010

Foreign Tweets?

Page 44: Using Web Services with JavaScript - Fronttrends 2010

select text from twitter.search where q=”ft2010” and iso_language_code=”pl”

Page 45: Using Web Services with JavaScript - Fronttrends 2010

select * from google.translate where q in (select text from twitter.search where q=”ft2010” and iso_language_code=”pl”) and target=”en”

Page 46: Using Web Services with JavaScript - Fronttrends 2010

Keywords for Warsaw?

Page 47: Using Web Services with JavaScript - Fronttrends 2010

select * from search.web where query="warsaw"

Page 48: Using Web Services with JavaScript - Fronttrends 2010

select abstract from search.web where query="warsaw"

Page 49: Using Web Services with JavaScript - Fronttrends 2010

select abstract from search.web(100) where query="warsaw"

Page 50: Using Web Services with JavaScript - Fronttrends 2010

select * from search.termextract where context in (select abstract from search.web(100) where query="warsaw")

Page 51: Using Web Services with JavaScript - Fronttrends 2010

select * from search.termextract where context in (select abstract from search.web(100) where query="warsaw") | sort(field="Result") | unique(field="Result")

Page 52: Using Web Services with JavaScript - Fronttrends 2010

Photos of Warsaw?

Page 53: Using Web Services with JavaScript - Fronttrends 2010

select * from flickr.photos.search(50) where woe_id in (select woeid from geo.places where text="warsaw") and min_taken_date = "2009"

Page 54: Using Web Services with JavaScript - Fronttrends 2010

Instead of going crazy filtering and sorting in JS...

Page 55: Using Web Services with JavaScript - Fronttrends 2010

...use the YQL server and then have a very simple JS for displaying.

Page 56: Using Web Services with JavaScript - Fronttrends 2010

Using web services with YQL in JS.

Page 57: Using Web Services with JavaScript - Fronttrends 2010

YQL is a web service endpoint on its own...

Page 58: Using Web Services with JavaScript - Fronttrends 2010

https://query.yahooapis.com/v1/public/yql?q={uri-encoded-query}&format={xml|json}&diagnostics={true|false}&callback={function}&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys

Page 59: Using Web Services with JavaScript - Fronttrends 2010

As it supports JSON-P you can use it with dynamically created script nodes.

Page 60: Using Web Services with JavaScript - Fronttrends 2010
Page 61: Using Web Services with JavaScript - Fronttrends 2010

Special case: Scraping

http://www.flickr.com/photos/fdtate/4426760544/

Page 63: Using Web Services with JavaScript - Fronttrends 2010
Page 65: Using Web Services with JavaScript - Fronttrends 2010

http://y.ahoo.it/r/ENSPGm

Page 67: Using Web Services with JavaScript - Fronttrends 2010

HTML as JSON is not fun.

Page 68: Using Web Services with JavaScript - Fronttrends 2010

JSON-P-X = HTML as a string in a JSON-P container!

Page 69: Using Web Services with JavaScript - Fronttrends 2010
Page 70: Using Web Services with JavaScript - Fronttrends 2010

Using YQL re-use of web content is very easy indeed.

Page 71: Using Web Services with JavaScript - Fronttrends 2010

Be safe, be good...

Page 72: Using Web Services with JavaScript - Fronttrends 2010

Using JSON-P in JavaScript is easy.

Page 73: Using Web Services with JavaScript - Fronttrends 2010

Don’t rely on data arriving - test for it!

Page 74: Using Web Services with JavaScript - Fronttrends 2010

XML to JSON?

Page 75: Using Web Services with JavaScript - Fronttrends 2010

XML to JSON?

Page 76: Using Web Services with JavaScript - Fronttrends 2010

Using JSON is easy with libraries.

Page 77: Using Web Services with JavaScript - Fronttrends 2010

$.getJSON(url+'&callback=?', function(data){});

JSON-P and jQuery:

Page 78: Using Web Services with JavaScript - Fronttrends 2010

$.ajax({ url: url, dataType: 'jsonp', jsonp: 'callback', jsonpCallback: 'ohyeah'});function ohyeah(data){}

JSON-P and jQuery:

Page 79: Using Web Services with JavaScript - Fronttrends 2010

Which one to use?

Page 80: Using Web Services with JavaScript - Fronttrends 2010

getJSON() is dangerous with other people’s data.

Page 82: Using Web Services with JavaScript - Fronttrends 2010

Cachebreaking is not a good idea.

Page 83: Using Web Services with JavaScript - Fronttrends 2010

Local caching is a good idea though.

Page 84: Using Web Services with JavaScript - Fronttrends 2010

Cookies suck, though.

Page 85: Using Web Services with JavaScript - Fronttrends 2010

Would be good to have a better solution for that.

Page 86: Using Web Services with JavaScript - Fronttrends 2010

localStorage = cookies on steroids.

Page 87: Using Web Services with JavaScript - Fronttrends 2010

if(('localStorage' in window) && window['localStorage'] !== null){localStorage.setItem( 'cake', 'much better than cookies')

}

Page 88: Using Web Services with JavaScript - Fronttrends 2010

if(('localStorage' in window) && window['localStorage'] !== null){var what = localStorage.getItem( 'cake')// what -> 'much better than cookies'

}

Page 89: Using Web Services with JavaScript - Fronttrends 2010

localStorage only stores Strings - use JSON to work around that.

Page 90: Using Web Services with JavaScript - Fronttrends 2010

if(('localStorage' in window) && window['localStorage'] !== null){localStorage.setItem( 'cake', JSON.stringify( {yummy:‘yes’,candles:5} ));

}

Page 91: Using Web Services with JavaScript - Fronttrends 2010

if(('localStorage' in window) && window['localStorage'] !== null){var what = JSON.parse( localStorage.getItem('cake'));// what -> Object{...} // and not [Object object]

}

Page 92: Using Web Services with JavaScript - Fronttrends 2010

Let’s wrap this up in a function.

Page 93: Using Web Services with JavaScript - Fronttrends 2010

yql - the queryid - storage key namecacheage - how long to cachecallback - obvious, isn’t it?

Page 94: Using Web Services with JavaScript - Fronttrends 2010

Browsers supporting localStorage fetch the data every hour.

Page 95: Using Web Services with JavaScript - Fronttrends 2010

Others still work, but load the data every time.

Page 96: Using Web Services with JavaScript - Fronttrends 2010

OK, dann machen wir das halt..

Page 97: Using Web Services with JavaScript - Fronttrends 2010

OK, dann machen wir das halt..

Page 98: Using Web Services with JavaScript - Fronttrends 2010

OK, dann machen wir das halt..

Page 99: Using Web Services with JavaScript - Fronttrends 2010
Page 100: Using Web Services with JavaScript - Fronttrends 2010
Page 101: Using Web Services with JavaScript - Fronttrends 2010
Page 102: Using Web Services with JavaScript - Fronttrends 2010
Page 103: Using Web Services with JavaScript - Fronttrends 2010
Page 104: Using Web Services with JavaScript - Fronttrends 2010
Page 105: Using Web Services with JavaScript - Fronttrends 2010
Page 106: Using Web Services with JavaScript - Fronttrends 2010

There are a few libraries offering fallbacks to other browers via Flash.

Page 107: Using Web Services with JavaScript - Fronttrends 2010

You can of course also use those.

Page 108: Using Web Services with JavaScript - Fronttrends 2010

Offering your own API.

Page 109: Using Web Services with JavaScript - Fronttrends 2010

To get your own API into YQL all you need to do is write an XML schema and put it on GitHub.

Page 111: Using Web Services with JavaScript - Fronttrends 2010

YQL allows you to write “executable tables”...

Page 112: Using Web Services with JavaScript - Fronttrends 2010

...which means you can convert data with JavaScript that will be executed server-side.

Page 113: Using Web Services with JavaScript - Fronttrends 2010

Twitter translate example:

Page 114: Using Web Services with JavaScript - Fronttrends 2010

Offering your own API.

Page 115: Using Web Services with JavaScript - Fronttrends 2010

Offering your own API.

Page 116: Using Web Services with JavaScript - Fronttrends 2010
Page 117: Using Web Services with JavaScript - Fronttrends 2010

SELECT * FROM twitter.translate WHERE language="en" and search="warsaw" and amount="20"

Page 118: Using Web Services with JavaScript - Fronttrends 2010

Distance example:

Page 119: Using Web Services with JavaScript - Fronttrends 2010
Page 120: Using Web Services with JavaScript - Fronttrends 2010
Page 121: Using Web Services with JavaScript - Fronttrends 2010

SELECT * FROM geo.distance WHERE place1=”london” and place2="warsaw"

Page 123: Using Web Services with JavaScript - Fronttrends 2010

Using your JS tables.

Page 124: Using Web Services with JavaScript - Fronttrends 2010

Write your schema, put it on the web and use, err... USE to, err... use it.

Page 125: Using Web Services with JavaScript - Fronttrends 2010

use “http://app.pl/distance.xml” as distance; SELECT * FROM distance WHERE place1=”london” and place2="warsaw"

Page 126: Using Web Services with JavaScript - Fronttrends 2010

Both problems solved and released as an API - in JS!

Page 127: Using Web Services with JavaScript - Fronttrends 2010

In summaryUse YQL instead of wasting time reading API docs for a simple taskFilter data in the service and get the info back in formats you need.Use the fast YQL server instead of doing lots of requests.Write your own JS APIs using execute.Use local storage and don’t break caching.Go and use the web.