Giter VIP home page Giter VIP logo

homework-assignment-2's Introduction

Homework Assignment 2 - Node.js Masterclass


Getting started

Requirements

  • Node.js working installation.
  • A working Mailgun.com account, required for sending email receipts to customers.
  • A working Stripe.com account, required for stripe payment platform platform integration.
  • curl, Postman or any other application with similar functionalities at your disposal.

Configuring the API

  • Download and extract this repository to a local directory.
  • Navigate to folder-containing-the-extracted-repository/app/lib/config/ directory. There should be files named stripeConfig.js and mailgunConfig.js. These files act as containers for data required from your end.
  • Open the stripeConfig.js file in the editor of your choice.
  • Find the stripe object in the file. You can see that this project has a few properties initializaed by an empty string. YOU NEED TO REPLACE THE EMPTY STRINGS WITH VALID ONES.
    Screenshot: An image showing what to replace in the stripeConfig.js
  • Save the file and close it.
  • Open the mailgunConfig.js file in the editor of your choice, and update the necessary information under the mailgun object.
    Screeshot: An image showing what to replace in the mailgunConfig.js
  • Save the file and close it.

Now the API setup is complete.

Running the API

  • Open a terminal/command-line window and navigate into the app/ directory.
  • Run node index.js.
  • If everything's O.K. then the terminal is now displaying relevant port information.
  • Check the next section to know more about API Interactions.

The default environment is "staging" and currently is the only environment defined in the configuration file.

API overview

What's happening?

To understand this, lets assume that we have a new customer who wants to order something on the Pizza Store. We are to assume that the Pizza Delivery Online Service already has a frontend configured to work with this API.

Details about the request formats are discussed later in "Interacting with API" section of this document.

Now, the primary requirement to access any of the functionalities provided by the online service is for a customer to have an account. To create an account, the customer needs to provide

  • First name
  • Last name
  • E-mail
  • Street address
  • Password

This part is handled with a POST request from the frontend with all the above mentioned fields as request body. Once the server validates the data, modifies and saves it, it sends a O.K. response to the frontend. This concludes the Registration Process. That's not all.
The next requirement for the customer to access the API is a valid session. For that the customer needs to share its email and password with the API. This also happens with a POST request with the fields in the request body (JSON). If the authentication is successful, the API sends another O.K. response to the frontend, this time with a TOKEN. That's essentially the Login Process.

The server assigns expiration to the tokens at the time of creation. This ensures that if no token modifications occur, each session is limited.

Now the customer can access other features, namely

  • Edit their Credentials
  • Delete their account
  • Access the menu
  • Add items from the menu to the cart
  • Place orders

I want to refactor the API to be a message driven API without using external modules and dependencies. Any suggestions on how i can improve on that?

Integration with stripe.com and mailgun.com

All the necessary information required to perform valid test transaction is stored in config file. For testing purposes, a module with all dummy information has been added to the API. When the app launches in staging environment, this module is used. This module adds payment information to each new user whenever a valid create user request is received.

The configuration folder in the API directory contains stripe and mailgun containers. Once the API is supplied with necessary keys and tokens, it uses them to perform HTTP basic authentication in case of a stripe and mailgun transaction.

Interacting with API

The API accepts HTTP and HTTPS requests. Assuming that the API is running under test configuration with all the necessary bells and whistles (referring to MAILGUN and STRIPE credentials here), the following sections explain behavior that can be expected from the API.

Create a user

request_type : POST
path : users
payload_type : JSON
fields :

  • firstName
  • lastName
  • email
  • streetAddress

example : POST request at localhost:3000/users with following payload

{
    "firstName":"John",
    "lastName":"Doe",
    "email":"[email protected]",
    "streetAddress":"111, xyz street"
} 



Update a user

request_type : PATCH
path : users
payload_type : JSON
fields : token and atleast one of the following field to update

  • firstName
  • lastName
  • streetAddress

example : UPDATE request at localhost:3000/users with token placed in header and following payload

{
    "lastName":"zzz"
} 



Delete a user

request_type : DELETE
path : users
payload_type : JSON
fields : email

example : DELETE request at localhost:3000/[email protected] with a valid token in the header


User Login

request_type : POST
path : session
payload_type : JSON
fields : email

example : POST request at localhost:3000/login with following payload

{
    "email":"[email protected]",
    "password":"abcd1234"
} 



User Logout

request_type : DELETE
path : session
payload_type : JSON
fields : email

example : POST request at localhost:3000/logout with a valid token in the header

Get menu

request_type : GET
path : menu
payload_type : JSON
fields : token

example : GET request at localhost:3000/menu?token=akh279sbn09mn2gh89xSJ2 with a valid token in the header


Update shopping cart

request_type : PATCH path : cart
payload_type : JSON
fields :

  • token
  • items

example : UPDATE request at localhost:3000/cart with a valid token in the header and following payload

{
    "items": [
        {
            "id":"1A",
            "quantity":1
        },
        {
            "id":"12B",
            "quantity":4
        }
    ]
} 



Create order

request_type : POST
path : orders
payload_type : JSON
fields :

  • token

example : POST request at localhost:3000/orders with a valid token in the header and following payload

{
    "token":"akh279sbn09mn2gh89xSJ2"
} 



homework-assignment-2's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.