SlideShare una empresa de Scribd logo
1 de 31
Descargar para leer sin conexión
Avoiding API
            Library Antipatterns
              Paul Mison, 12 January 2009
               London Ruby User Group




Hi, Iʼm Paul Mison and Iʼm here to talk about Ruby,
and APIs.
In particular, Iʼll be talking about Flickrʼs API. This is
its documentation page. Flickr, as Iʼm sure you all
know, is a site for sharing photos.
Photos like this one, in fact. by Ryan Forsythe

They offer a REST-style API, as well as XML-RPC
and SOAP, which Iʼll ignore. However, you can take
the lessons from this and apply it to many other
popular modern web APIs, which have a similar
structure.
The screenshots here are of their API Explorer tool,
which is a nice way to see what arguments a
method takes and what it returns. But you donʼt
want to worry about XML: you just want kitten
pictures!
RubyForge lists a lot of projects to talk to Flickr.
(This was taken a while ago; I should probably
have searched GitHub.)
In fact, I make out that there are seven libraries
listed. It turns out that a lot of these are
abandoned. Iʼll explain why.
First, hereʼs a Ruby Flickr library called flickr-fu. It
works, and returns objects.
Letʼs try this perfectly valid Flickr API method - in
fact, itʼs one of an entire subset of methods in the
groups namespace - and we see the library is
throwing an error. Why? Well, Thereʼs no groups
method in flickr-fu because nobodyʼs written it yet.
This is part of the code for flickr-fu, for the people
methods. Note all this code thatʼs just copying part
of the response to the Person object. Now, imagine
going through and doing that for the groups
methods...
... and there are a few. Daunting, isnʼt it? I can
understand why they havenʼt got around to it yet.
In fact, these are the areas that flickr_fu provides,
according to commonthreadʼs github copy.
r2flickr, an extension of rflickr, which is listed on the
API home page, does better, but even so, itʼs
missing some of the newer parts of the API (such
as the flickr.places methods).
This is the problem
         with abstraction.



This is the problem with abstraction. Once you start
setting up objects like that, then you find that
thereʼs a lot of work involved to add new ones, and
Flickrʼs API is quite broad. This explains the
abandoned modules- their authors got bored of
writing boilerplate code.
Even when youʼve implemented all the existing
methods, thatʼs not the end. “We added 1 new API
method, and updated 7 others”. If youʼre looking
after an API, thatʼs the last thing you want to see.
... especially since, for now, thereʼs no way to even
track those changes yourself unless you write your
own code to use Flickrʼs reflection methods -
although Flickr are aiming to change that.

http://flickr.com/groups/api/discuss/
72157604690623636/
There is a solution.




Fixing It: The Request
Fixing It: The Request

So, we can see why the OO approach leads to
unfinished, or unmaintained, libraries. How do you
avoid this?

http://flickr.com/photos/gspragin/2043558365/
Nothing to do with fixing, really, this picture, but I
liked it anyway.
All a request to Flickr is is a signed call. This is
from flickraw, and thatʼs all you need, including
argument signing.
In fact, if you look at flickr-fu earlier, it has a similar
internal method, send_request. If they just exposed
it, I could use it for group or places calls. To be fair,
it is documented as a public method, but you
wouldnʼt think to look for it.
You can be a step cleverer, and offer the nice
methods, even if you donʼt write boilerplate.

This is another part of flickraw, where it uses
Flickrʼs own reflection methods - the ones
mentioned earlier - to dynamically build the list of
methods itʼs able to use. Smart, but takes a bit of
effort.

http://flickr.com/photos/omaromar/417305666/
Fixing It: The Response

Now, what about the responses?
Back to flickr-fu. Look at all that code copying from
the rsp XML object to properties of the Person
object. What a drag, but I suppose it saves me
having to do it. Still, surely thereʼs a better way?
Hereʼs some good news. Flickr supports returning
its responses as JSON
Object




The O in JSON stands for Object, and this means
that youʼre able to get things back that you can
easily treat as, well, objects, without any of that
painful XML traversing that we all know and love.
To return an API response in JSON
      format, send a parameter “format”
      in the request with a value of “json”




Getting back a JSON response rather than an
XML one is trivial; you just change one parameter.
(Sidenote probably not for the talk: you also want
“nocallback” to be set, otherwise the JSON will be
an executable bit of code rather than just an object,
but itʼs still pretty straightforward.)
This is exactly what flickraw does, and as a result,
it can automatically pick up any additions that Flickr
make to their response format. And when you
come to output it, you can just write clean Ruby
code.
Conclusions

So, what have we learnt?

http://www.flickr.com/photos/
artimagesmarkcummins/513969188
Offer simple access
        wrappers to users.



Instead of writing a method per allowed call, first
write a generic method - youʼll probably need it
anyway.
Abstraction sucks -
        Rubyʼs dynamic.
       unlessdo the work.
        Let it youʼre clever



Rubyʼs a dynamic language. Use that to your best
advantage - for example, building convenience
methods on the fly.
Abstraction sucks -
           Use JSON,
       unless youʼre clever
            not XML.



Use JSON, which can be easily turned into an
object, rather than XML, to save yourself work
converting things.
Thank you.

          Questions?
http://www.flickr.com/photos/antimega/2468011723/
http://www.flickr.com/photos/kubina/279522886/
http://www.flickr.com/photos/infrarad/182786930/
http://flickr.com/photos/gspragin/2043558365/
http://flickr.com/photos/omaromar/417305666/
http://flickr.com/photos/artimagesmarkcummins/513969188

Más contenido relacionado

La actualidad más candente

Myphp-busters: symfony framework (PHPCon.it)
Myphp-busters: symfony framework (PHPCon.it)Myphp-busters: symfony framework (PHPCon.it)
Myphp-busters: symfony framework (PHPCon.it)Stefan Koopmanschap
 
Merb presentation at ORUG
Merb presentation at ORUGMerb presentation at ORUG
Merb presentation at ORUGMatt Aimonetti
 
Gdg dev fest 2107 to kotlin, with love
Gdg dev fest 2107   to kotlin, with loveGdg dev fest 2107   to kotlin, with love
Gdg dev fest 2107 to kotlin, with loveAyman Mahfouz
 
State Of Zope Linuxtag 2008
State Of Zope Linuxtag 2008State Of Zope Linuxtag 2008
State Of Zope Linuxtag 2008Andreas Jung
 
Being Dangerous with Twig (Symfony Live Paris)
Being Dangerous with Twig (Symfony Live Paris)Being Dangerous with Twig (Symfony Live Paris)
Being Dangerous with Twig (Symfony Live Paris)Ryan Weaver
 
JavaScript Essentials for Ember development
JavaScript Essentials for Ember developmentJavaScript Essentials for Ember development
JavaScript Essentials for Ember developmentLeo Hernandez
 
Mixing Plone and Django for explosive results
Mixing Plone and Django for explosive resultsMixing Plone and Django for explosive results
Mixing Plone and Django for explosive resultsSimone Deponti
 
Rails development environment talk
Rails development environment talkRails development environment talk
Rails development environment talkReuven Lerner
 
Flickr Architecture Presentation
Flickr Architecture PresentationFlickr Architecture Presentation
Flickr Architecture Presentationeraz
 
Into The Box 2018 Automate Your Test
Into The Box 2018 Automate Your Test Into The Box 2018 Automate Your Test
Into The Box 2018 Automate Your Test Ortus Solutions, Corp
 
java.util.concurrent for Distributed Coordination, GeeCON Krakow 2019
java.util.concurrent for Distributed Coordination, GeeCON Krakow 2019java.util.concurrent for Distributed Coordination, GeeCON Krakow 2019
java.util.concurrent for Distributed Coordination, GeeCON Krakow 2019Ensar Basri Kahveci
 

La actualidad más candente (16)

Myphp-busters: symfony framework (PHPCon.it)
Myphp-busters: symfony framework (PHPCon.it)Myphp-busters: symfony framework (PHPCon.it)
Myphp-busters: symfony framework (PHPCon.it)
 
Merb presentation at ORUG
Merb presentation at ORUGMerb presentation at ORUG
Merb presentation at ORUG
 
Gdg dev fest 2107 to kotlin, with love
Gdg dev fest 2107   to kotlin, with loveGdg dev fest 2107   to kotlin, with love
Gdg dev fest 2107 to kotlin, with love
 
The future of templating and frameworks
The future of templating and frameworksThe future of templating and frameworks
The future of templating and frameworks
 
State Of Zope Linuxtag 2008
State Of Zope Linuxtag 2008State Of Zope Linuxtag 2008
State Of Zope Linuxtag 2008
 
Being Dangerous with Twig (Symfony Live Paris)
Being Dangerous with Twig (Symfony Live Paris)Being Dangerous with Twig (Symfony Live Paris)
Being Dangerous with Twig (Symfony Live Paris)
 
JavaScript Essentials for Ember development
JavaScript Essentials for Ember developmentJavaScript Essentials for Ember development
JavaScript Essentials for Ember development
 
Mixing Plone and Django for explosive results
Mixing Plone and Django for explosive resultsMixing Plone and Django for explosive results
Mixing Plone and Django for explosive results
 
Iron Sprog Tech Talk
Iron Sprog Tech TalkIron Sprog Tech Talk
Iron Sprog Tech Talk
 
Clojure presentation
Clojure presentationClojure presentation
Clojure presentation
 
Erjang
ErjangErjang
Erjang
 
Rails development environment talk
Rails development environment talkRails development environment talk
Rails development environment talk
 
Flickr Architecture Presentation
Flickr Architecture PresentationFlickr Architecture Presentation
Flickr Architecture Presentation
 
Into The Box 2018 Automate Your Test
Into The Box 2018 Automate Your Test Into The Box 2018 Automate Your Test
Into The Box 2018 Automate Your Test
 
Moving to PHP from Java
Moving to PHP from JavaMoving to PHP from Java
Moving to PHP from Java
 
java.util.concurrent for Distributed Coordination, GeeCON Krakow 2019
java.util.concurrent for Distributed Coordination, GeeCON Krakow 2019java.util.concurrent for Distributed Coordination, GeeCON Krakow 2019
java.util.concurrent for Distributed Coordination, GeeCON Krakow 2019
 

Destacado

Provincie Utrecht Web 2.0 (16 April 2009) [Final]
Provincie Utrecht   Web 2.0 (16 April 2009) [Final]Provincie Utrecht   Web 2.0 (16 April 2009) [Final]
Provincie Utrecht Web 2.0 (16 April 2009) [Final]R. Zandbergen
 
Attica multimedia guides
Attica multimedia guidesAttica multimedia guides
Attica multimedia guidesWorld Reach PR
 
Indennità
IndennitàIndennità
Indennitàsdimeo
 
Email marketing-metrics-benchmark-study-2014-silverpop
Email marketing-metrics-benchmark-study-2014-silverpopEmail marketing-metrics-benchmark-study-2014-silverpop
Email marketing-metrics-benchmark-study-2014-silverpopYoli Chisholm
 
Online Advertising Ecosystem
Online Advertising Ecosystem Online Advertising Ecosystem
Online Advertising Ecosystem Yoli Chisholm
 
Online Advertising Ad Type Benchmarks Metrics - Pointroll
Online Advertising Ad Type Benchmarks Metrics - PointrollOnline Advertising Ad Type Benchmarks Metrics - Pointroll
Online Advertising Ad Type Benchmarks Metrics - PointrollYoli Chisholm
 
Software and SaaS Key Metrics and Benchmarks
Software and SaaS Key Metrics and BenchmarksSoftware and SaaS Key Metrics and Benchmarks
Software and SaaS Key Metrics and BenchmarksDiana Esparza
 

Destacado (7)

Provincie Utrecht Web 2.0 (16 April 2009) [Final]
Provincie Utrecht   Web 2.0 (16 April 2009) [Final]Provincie Utrecht   Web 2.0 (16 April 2009) [Final]
Provincie Utrecht Web 2.0 (16 April 2009) [Final]
 
Attica multimedia guides
Attica multimedia guidesAttica multimedia guides
Attica multimedia guides
 
Indennità
IndennitàIndennità
Indennità
 
Email marketing-metrics-benchmark-study-2014-silverpop
Email marketing-metrics-benchmark-study-2014-silverpopEmail marketing-metrics-benchmark-study-2014-silverpop
Email marketing-metrics-benchmark-study-2014-silverpop
 
Online Advertising Ecosystem
Online Advertising Ecosystem Online Advertising Ecosystem
Online Advertising Ecosystem
 
Online Advertising Ad Type Benchmarks Metrics - Pointroll
Online Advertising Ad Type Benchmarks Metrics - PointrollOnline Advertising Ad Type Benchmarks Metrics - Pointroll
Online Advertising Ad Type Benchmarks Metrics - Pointroll
 
Software and SaaS Key Metrics and Benchmarks
Software and SaaS Key Metrics and BenchmarksSoftware and SaaS Key Metrics and Benchmarks
Software and SaaS Key Metrics and Benchmarks
 

Similar a Avoiding API Library Antipatterns

YQL - Christian Heilmann Open Hack London presentation
YQL - Christian Heilmann Open Hack London presentationYQL - Christian Heilmann Open Hack London presentation
YQL - Christian Heilmann Open Hack London presentationKorben00
 
Get started with AAR
Get started with AARGet started with AAR
Get started with AARRené Mertins
 
RubyMotion Inspect Conference - 2013. (With speaker notes.)
RubyMotion Inspect Conference - 2013. (With speaker notes.)RubyMotion Inspect Conference - 2013. (With speaker notes.)
RubyMotion Inspect Conference - 2013. (With speaker notes.)alloy020
 
Flickr Services
Flickr ServicesFlickr Services
Flickr Servicesroyans
 
Flickr Services
Flickr ServicesFlickr Services
Flickr Servicesroyans
 
xkcd viewer report
xkcd viewer reportxkcd viewer report
xkcd viewer reportZx MYS
 
EdTechJoker Spring 2020 - Lecture 8 Drupal again
EdTechJoker Spring 2020 - Lecture 8 Drupal againEdTechJoker Spring 2020 - Lecture 8 Drupal again
EdTechJoker Spring 2020 - Lecture 8 Drupal againBryan Ollendyke
 
Semantic web, python, construction industry
Semantic web, python, construction industrySemantic web, python, construction industry
Semantic web, python, construction industryReinout van Rees
 
Ryan Christiani I Heard React Was Good
Ryan Christiani I Heard React Was GoodRyan Christiani I Heard React Was Good
Ryan Christiani I Heard React Was GoodFITC
 
Workshop: Introduction to Web Components & Polymer
Workshop: Introduction to Web Components & Polymer Workshop: Introduction to Web Components & Polymer
Workshop: Introduction to Web Components & Polymer John Riviello
 
Introduction to new technologies in drupal 8
Introduction to new technologies in drupal 8Introduction to new technologies in drupal 8
Introduction to new technologies in drupal 8naxoc
 
Web components Introduction
Web components IntroductionWeb components Introduction
Web components IntroductionEugenio Romano
 
Open event (show&tell april 2016)
Open event (show&tell april 2016)Open event (show&tell april 2016)
Open event (show&tell april 2016)Jorge López-Lago
 
RubyMotion: Put your Dreams in Motion with Ruby
RubyMotion: Put your Dreams in Motion with RubyRubyMotion: Put your Dreams in Motion with Ruby
RubyMotion: Put your Dreams in Motion with RubyAstrails
 

Similar a Avoiding API Library Antipatterns (20)

React js basics
React js basicsReact js basics
React js basics
 
YQL - Christian Heilmann Open Hack London presentation
YQL - Christian Heilmann Open Hack London presentationYQL - Christian Heilmann Open Hack London presentation
YQL - Christian Heilmann Open Hack London presentation
 
Get started with AAR
Get started with AARGet started with AAR
Get started with AAR
 
RubyMotion Inspect Conference - 2013. (With speaker notes.)
RubyMotion Inspect Conference - 2013. (With speaker notes.)RubyMotion Inspect Conference - 2013. (With speaker notes.)
RubyMotion Inspect Conference - 2013. (With speaker notes.)
 
Flickr Services
Flickr ServicesFlickr Services
Flickr Services
 
Flickr Services
Flickr ServicesFlickr Services
Flickr Services
 
Graphql
GraphqlGraphql
Graphql
 
xkcd viewer report
xkcd viewer reportxkcd viewer report
xkcd viewer report
 
EdTechJoker Spring 2020 - Lecture 8 Drupal again
EdTechJoker Spring 2020 - Lecture 8 Drupal againEdTechJoker Spring 2020 - Lecture 8 Drupal again
EdTechJoker Spring 2020 - Lecture 8 Drupal again
 
Semantic web, python, construction industry
Semantic web, python, construction industrySemantic web, python, construction industry
Semantic web, python, construction industry
 
Ryan Christiani I Heard React Was Good
Ryan Christiani I Heard React Was GoodRyan Christiani I Heard React Was Good
Ryan Christiani I Heard React Was Good
 
Workshop: Introduction to Web Components & Polymer
Workshop: Introduction to Web Components & Polymer Workshop: Introduction to Web Components & Polymer
Workshop: Introduction to Web Components & Polymer
 
Introduction to new technologies in drupal 8
Introduction to new technologies in drupal 8Introduction to new technologies in drupal 8
Introduction to new technologies in drupal 8
 
Yahoo is open to developers
Yahoo is open to developersYahoo is open to developers
Yahoo is open to developers
 
Web components Introduction
Web components IntroductionWeb components Introduction
Web components Introduction
 
Concurrency in ruby
Concurrency in rubyConcurrency in ruby
Concurrency in ruby
 
Open event (show&tell april 2016)
Open event (show&tell april 2016)Open event (show&tell april 2016)
Open event (show&tell april 2016)
 
Hack Rio/OS
Hack Rio/OSHack Rio/OS
Hack Rio/OS
 
RubyMotion: Put your Dreams in Motion with Ruby
RubyMotion: Put your Dreams in Motion with RubyRubyMotion: Put your Dreams in Motion with Ruby
RubyMotion: Put your Dreams in Motion with Ruby
 
Monad Fact #6
Monad Fact #6Monad Fact #6
Monad Fact #6
 

Último

Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 

Último (20)

Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 

Avoiding API Library Antipatterns

  • 1. Avoiding API Library Antipatterns Paul Mison, 12 January 2009 London Ruby User Group Hi, Iʼm Paul Mison and Iʼm here to talk about Ruby, and APIs.
  • 2. In particular, Iʼll be talking about Flickrʼs API. This is its documentation page. Flickr, as Iʼm sure you all know, is a site for sharing photos.
  • 3. Photos like this one, in fact. by Ryan Forsythe They offer a REST-style API, as well as XML-RPC and SOAP, which Iʼll ignore. However, you can take the lessons from this and apply it to many other popular modern web APIs, which have a similar structure.
  • 4. The screenshots here are of their API Explorer tool, which is a nice way to see what arguments a method takes and what it returns. But you donʼt want to worry about XML: you just want kitten pictures!
  • 5. RubyForge lists a lot of projects to talk to Flickr. (This was taken a while ago; I should probably have searched GitHub.)
  • 6. In fact, I make out that there are seven libraries listed. It turns out that a lot of these are abandoned. Iʼll explain why.
  • 7. First, hereʼs a Ruby Flickr library called flickr-fu. It works, and returns objects.
  • 8. Letʼs try this perfectly valid Flickr API method - in fact, itʼs one of an entire subset of methods in the groups namespace - and we see the library is throwing an error. Why? Well, Thereʼs no groups method in flickr-fu because nobodyʼs written it yet.
  • 9. This is part of the code for flickr-fu, for the people methods. Note all this code thatʼs just copying part of the response to the Person object. Now, imagine going through and doing that for the groups methods...
  • 10. ... and there are a few. Daunting, isnʼt it? I can understand why they havenʼt got around to it yet.
  • 11. In fact, these are the areas that flickr_fu provides, according to commonthreadʼs github copy.
  • 12. r2flickr, an extension of rflickr, which is listed on the API home page, does better, but even so, itʼs missing some of the newer parts of the API (such as the flickr.places methods).
  • 13. This is the problem with abstraction. This is the problem with abstraction. Once you start setting up objects like that, then you find that thereʼs a lot of work involved to add new ones, and Flickrʼs API is quite broad. This explains the abandoned modules- their authors got bored of writing boilerplate code.
  • 14. Even when youʼve implemented all the existing methods, thatʼs not the end. “We added 1 new API method, and updated 7 others”. If youʼre looking after an API, thatʼs the last thing you want to see.
  • 15. ... especially since, for now, thereʼs no way to even track those changes yourself unless you write your own code to use Flickrʼs reflection methods - although Flickr are aiming to change that. http://flickr.com/groups/api/discuss/ 72157604690623636/
  • 16. There is a solution. Fixing It: The Request
  • 17. Fixing It: The Request So, we can see why the OO approach leads to unfinished, or unmaintained, libraries. How do you avoid this? http://flickr.com/photos/gspragin/2043558365/ Nothing to do with fixing, really, this picture, but I liked it anyway.
  • 18. All a request to Flickr is is a signed call. This is from flickraw, and thatʼs all you need, including argument signing.
  • 19. In fact, if you look at flickr-fu earlier, it has a similar internal method, send_request. If they just exposed it, I could use it for group or places calls. To be fair, it is documented as a public method, but you wouldnʼt think to look for it.
  • 20. You can be a step cleverer, and offer the nice methods, even if you donʼt write boilerplate. This is another part of flickraw, where it uses Flickrʼs own reflection methods - the ones mentioned earlier - to dynamically build the list of methods itʼs able to use. Smart, but takes a bit of effort. http://flickr.com/photos/omaromar/417305666/
  • 21. Fixing It: The Response Now, what about the responses?
  • 22. Back to flickr-fu. Look at all that code copying from the rsp XML object to properties of the Person object. What a drag, but I suppose it saves me having to do it. Still, surely thereʼs a better way?
  • 23. Hereʼs some good news. Flickr supports returning its responses as JSON
  • 24. Object The O in JSON stands for Object, and this means that youʼre able to get things back that you can easily treat as, well, objects, without any of that painful XML traversing that we all know and love.
  • 25. To return an API response in JSON format, send a parameter “format” in the request with a value of “json” Getting back a JSON response rather than an XML one is trivial; you just change one parameter. (Sidenote probably not for the talk: you also want “nocallback” to be set, otherwise the JSON will be an executable bit of code rather than just an object, but itʼs still pretty straightforward.)
  • 26. This is exactly what flickraw does, and as a result, it can automatically pick up any additions that Flickr make to their response format. And when you come to output it, you can just write clean Ruby code.
  • 27. Conclusions So, what have we learnt? http://www.flickr.com/photos/ artimagesmarkcummins/513969188
  • 28. Offer simple access wrappers to users. Instead of writing a method per allowed call, first write a generic method - youʼll probably need it anyway.
  • 29. Abstraction sucks - Rubyʼs dynamic. unlessdo the work. Let it youʼre clever Rubyʼs a dynamic language. Use that to your best advantage - for example, building convenience methods on the fly.
  • 30. Abstraction sucks - Use JSON, unless youʼre clever not XML. Use JSON, which can be easily turned into an object, rather than XML, to save yourself work converting things.
  • 31. Thank you. Questions? http://www.flickr.com/photos/antimega/2468011723/ http://www.flickr.com/photos/kubina/279522886/ http://www.flickr.com/photos/infrarad/182786930/ http://flickr.com/photos/gspragin/2043558365/ http://flickr.com/photos/omaromar/417305666/ http://flickr.com/photos/artimagesmarkcummins/513969188