This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
• 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
• 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
• 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
• 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
• 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?