Upload
aeshan-wijetunge
View
62
Download
1
Embed Size (px)
Citation preview
Initial setup
• Install postman https://www.getpostman.com/
• Download workshop materials• Install nodejs https://nodejs.org/en/• Github repo http://bit.ly/2cMeCm1• Signup for PP – https://www.paypal.com/signup/account
• Postman collection http://bit.ly/2dkWFwj
Objectives
• Appreciate the benefits of leveraging public APIs
• Understand basics of calling a REST APIs• Can make payments using the PayPal API• Understand the benefits of Asynchronous
programming• Create a mashup of PayPal APIs leverging the
Async library
What is a REST API?
• REpresentational State Transfer• Context of Web Services• URLs (e.g:
https://api.github.com/users/aeshanw)• Interface to Resources over the Internet
Credit: http://www.robert-drummond.com/
Being RESTful
• APIs are Resource-centric• REST is built-atop the HTTP protocol• HTTP is the infrastructure of the Internet• REST APIs (a.k.a web services) support– GET– POST– PUT– DELETE
Anatomy of HTTP
Credits: http://cis.msjc.edu/ Mt San Jacinto College
Calling a simple API
• Fetch github user details • https://api.github.com/users/aeshanw• Response can be viewed on a web browser• This is a GET operation• What about POST/PUT/DELETE?– We need a more advanced tool to work with REST
APIs
Introducing POSTMAN
• Tool for working with REST APIs • As a developer you can– Customize the HTTP Headers of request– Authenticate your requests to protected APIs• Supports Oauth for this purpose
Using POSTMAN
• Import the POSTMAN collection:– https://www.getpostman.com/collections/
b0038ed98853ba7e1b10• Call the github URI with POSTMAN• Note the status code and response headers
PayPal APIs
• A huge collection of APIs handling– Payments– Invoicing– Billing Agreements
• Our focus will be on– Storing a credit card on the Vault API– Making a payment with the Vaulted card
PayPal Secured by OAuth
• Similar to log-in with username & password• You need a PayPal account (personal)• Authenticate via v1/oauth2/token API• Get your clientID & secret• POSTMAN Authorization tab– Select `Basic Auth` Type– Username clientID– Password secret
Authenticating your requests
v1/oauth2/token v1/vault/credit-cards
/v1/payments/payment
clientID + secret AccessToken
AccessToken
Add credit card
• Using /v1/vault/credit-cards• Request Body is in a JSON-format• Request Headers include the AccessToken
credit cards for testing
• PayPal sandbox is for testing only• Allows use of dummy cards for testing• Use this site for generating credit card
numbers for testing
• http://www.freeformatter.com/credit-card-number-generator-validator.html
Pay with a Vaulted card
v1/oauth2/token
v1/vault/credit-cards
v1/payments/payment
AccessToken
CardToken
PaymentID
Try this in POSTMAN
• Use the POSTMAN collection provided earlier• Use POSTMAN global variables to store the
AccessToken to be used in subsequent requests
• Exercise– Get Token (Authenticate)– Add a card to vault– Make a payment with the vaulted card
Getting started with Node.js
• Let’s make the calls from our application• Using node.js as an example, but concepts
valid in any language.• Exercise : Run the node.js app locally– $ npm install– $ npm start– Goto http://localhost:8000/
API calls from node.js
• Use the request module as REST Clienthttps://www.npmjs.com/package/request
• Look for Lab 1.0a in demoapp/controllers/index.js
Optimization• What if you need to add several cards at
once?
addCard1 payWithCard1 addCard2 payWithCard2
• Is there a faster way?• Yes, Parallelization
Asynchronous Complexity
• Many languages support Asynchronous libraries. E.g: Python, Java, Javascript, Scala, Erlang…
• But, Developing code that is Asynchronous can be difficult.
• Thankfully most modern languages provide libraries to manage the complexity
• Async is popular among Javascript developers
Using Async
• Docs here https://www.npmjs.com/package/async
• Async can execute your code– In series– In parallel
Async series
• Call the APIs in sequence one after the other• Run nodeserv dummy service to better
understand the difference– $ cd nodeserv– $ npm install– $ node index.js
• Look for Lab 1.1a in demoapp/controllers/index.js
Async series workshop
• Nodeserv has a 5 sec time-delay to simulate I/O latency
• How long would this lab code take to call the task API twice?
Async parallel
• Call the APIs in parallel• Look for Lab 1.1b in
demoapp/controllers/index.js • Did you notice any difference in the time taken
to call the API twice?
PayPal Integration
• Let’s integrate the vaulted-card solution into our node.js application
• First, we need to get the access_token• Look for Lab 2.0 in
demoapp/controllers/index.js • Test the api-call by hitting
localhost:8000/gettoken• Ensure you get the access_token
Vault the card
• Pass the access_token to a funtion which adds the card to vault
• Look for Lab 2.1 in demoapp/controllers/index.js
• Ensure you get the card_token from the vault API response
• Test the api-call by hitting localhost:8000/addcard
Make Payment
• Use case: We need to split a big payment into 2 credit cards due to some credit-limits on each card
• Parallelize the vaulting of 2 cards• Parallelize the 2 payments using the card
tokens• Look for Lab 2.2 in
demoapp/controllers/index.js
Fix the code
• Fix the payment api call in payWithToken • Parallelize the functions addCardToVault &
payWithToken in order to complete the tasks as efficiently as possible.
• Test the calls by hitting localhost:8000/paywithvault– Check the logs to verify the result
Bonus lab (optional)
• Notice that the Card Tokens are logged to console
• Can we lookup the Vaulted Card details?• Lookup the PayPal Vault API to do this
Recap
• Appreciate the use of APIs in applications• Call a RESTful API using POSTMAN• Call a RESTful API from an application• Debug API Integrations using logs• Parallelize API calls with Async libraries
Beyond Javascript
• Many languages support Async– Python– Java
So what’s stopping you from using it in your next application?
Thanks!
• https://github.com/nodeworkshop/ntu_2016_async_workshop
• PP Sample Cards • https://www.paypalobjects.com/en_US/
vhelp/paypalmanager_help/credit_card_numbers.htm