Giter VIP home page Giter VIP logo

backend's Introduction

The the API powering StockStalker

StockStalker - Backend API

Table of Contents

Makefile Commands

stop: Stop the running server

rm: Remove all unused containers

rm-all: Stop and remove all containers

rmi: Remove stockstalker images without removing base images. Useful for speeding up build time when switching from one start script to another such as make start to make test

rmi-all: Remove all images

purge: Use with caution Completely purge Docker containers, networks, images, volumes, and cache

lint: Run prettier and eslint

build: Build development server without running the server

start: Start development server at port 8080

reload: Stop development server and restart the server at port 8080

hard-reload: Stop container, remove container, rebuild container, and start development server

debug: Start development server in debug mode

test: Start test server

test-security: Test security vulnerabilities (must have snyk installed globally)

test-image-security: Test security vulnerabilities for base images (must have snyk installed globally)

reload-test: Reload test server

hard-reload-test: Stop container, remove container, rebuild container, and start test server

Required Software

How to Run

Clone the repo

git clone [email protected]:Stock-Stalker/backend.git

cd into the directory

cd backend

Rename .env.sample to .env

mv .env.sample .env

Edit the .env file to contain your environment variables

vim .env

Run the application!

make start

To stop the app press CNTRL + C. Then run:

make stop

API Documentation

Base URL

https://stockstalker.tk/api

Get All Stocks

Request

GET https://stockstalker.tk/api/stock

Response

Success status code: 200

This route returns all names and symbols of the NASDAQ and DOW companies currently supported by StockStalker.

Example response:

[
  {
    "symbol": "AACG",
    "companyName": "ATA Creativity Global - American Depositary Shares, each representing two common shares"
  },
  {
    "symbol": "AACQ",
    "companyName": "Artius Acquisition Inc. - Class A Common Stock"
  },
  ...
]

Types:

  • symbol: String
  • companyName: String

Error status code: 500

An unsuccessful request, for any reason (such as the database connection times out, or there's another server-side issue) will result in an error response:

{
    "message": "Connection refused."
}

Get One Stock

Request

GET https://stockstalker.tk/api/stock/:symbol

Where :symbol refers to the unique ticker symbol for a given stock.

Ex: AAPL for Apple

Can also accept the company name, case insensitive.

Ex: Apple, ApPle, apple, or APPLE

The company name or symbol sent in the query parameters will be validated.

Response

Success status code: 200

Successful responses will return stockData. An example response:

{
  "stockData": {
    "symbol": "AAPL",
    "companyName":"Apple Inc. - Common Stock",
    "historicalData": [{
        "datetime": "2021-03-16",
        "open":"125.70000",
        "high":"127.22000",
        "low":"124.71500",
        "close":"125.57000",
        "volume":"114813750"
        },
        {
          "datetime":"2021-03-15",
          "open":"121.41000",
          "high":"124.00000",
          "low":"120.43000",
          "close":"123.99000",
          "volume":"92590555"
          }
        ... ]
      }
    }
}

Types:

  • stockData: Object
    • symbol: String
    • companyName: String
    • historicalData: Array(Objects)
      • datetime: String
      • open: String
      • high: String
      • low: String
      • close: String
      • volume: String

Error status code: 404

An unsuccessful validation (for example, inserting a company name that doesn't exist) will return a 404 status code for "resource not found". An example error response:

{
    "message": "Cannot Find The Company or Symbol"
}

Get Popular Stocks

Request

GET https://stockstalker.tk/api/stock/popular

The route returns the current price of popular stocks: Apple Inc (AAPL), Tesla (TSLA), Netflix (NFLX), Amazon (AMZN), Facebook (FB).

Response

Success status code: 200

An example response:

{

  "AAPL": {
              "price": "123.71500"
          },
   ...

}

Types:

  • Array
    • Symbol: Object
      • price: Number

Error status code: 500

In the event that there is another error due to a failure on the server side, a status code of 500 will be returned, along with a message stating the error.

Get Stock Prediction

Request

GET https://stockstalker.tk/api/stock/prediction/:symbol

Where :symbol refers to the unique ticker symbol for a given stock.

Ex: AAPL for Apple

Can also accept the company name, case insensitive.

Ex: Apple, ApPle, apple, or APPLE

The company name or symbol sent in the query parameters will be validated.

Response

Success status code: 200

An example response:

{

  "prediction": 0

}

Types:

  • prediction: Integer

Error status code: 500

In the event that there is another error due to a failure on the server side, a status code of 500 will be returned, along with a message stating the error.

Users

Users are able to make accounts with StockStalker to track their own watchlist. This enables users to quickly access the stocks that matter to them.

Signing Up Users

Request

POST https://stockstalker.tk/api/user/signup

The request expects in the body a username and password.

Username must be between 4 and 25 characters long.

Password must be between 8 and 25 characters long.

Example request body:

{
    "username": "newuser",
    "password": "password"
}

Types:

  • username: String
  • password: String

Response

Success status code: 200

A successful response will send a Bearer token to be set in the Authorization header. This header will be evaluated for all restricted routes.

Example Authorization header for your reference:

headers: {
  'Authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IkZBS0VVU0VSIiwidXNlcklkIjoiNjA1M2JlZTg5YzU3MTcwMTQyMDExYzM2IiwiaWF0IjoxNjE2MTAxMTIzLCJleHAiOjE2MTYxMDQ3MjN9.IzWXlrFS7mQqR652keVEHnR4ayspk5yyyMjRpYTY7gw'
}

The response will send a success message and your new user's username.

Example response:

{
    "message": "Sign up successful!",
    "user": {
        "username": "newuser"
    },
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IkZBS0VVU0VSIiwidXNlcklkIjoiNjA1M2JlZTg5YzU3MTcwMTQyMDExYzM2IiwiaWF0IjoxNjE2MTAxMTIzLCJleHAiOjE2MTYxMDQ3MjN9.IzWXlrFS7mQqR652keVEHnR4ayspk5yyyMjRpYTY7gw"
}

Types:

  • message: String

  • user: Object

    • username: String
  • token: String

Validation error status code: 422

In the event that the request body cannot be validated, a status code of 422 will be returned, along with an error message containing something similar to (for example):

"ValidationError": "Username must be more than 4 characters"

Error status code: 409

In the event that the username you are attempting to sign up with already exists, a status code of 409 will be returned, along with a message stating that the user already exists.

Example 409 error message:

{
    "message": "User already exists!"
}

Error status code: 500

In the event that there is another error due to a failure on the server side, a status code of 500 will be returned, along with a message stating the error.

Example 500 error message:

{
    "err": "[error] 29#29: *17 upstream timed out (110: Operation timed out) while reading response header from upstream, client: 172.18.0.1, server: stockstalker.tk"
}

Signing In Users

Request

POST https://stockstalker.tk/api/user/signin

The request expects in the body a username and password.

Username must be between 4 and 25 characters long.

Password must be between 8 and 25 characters long.

Example request body:

{
    "username": "newuser",
    "password": "password"
}

Types:

  • username: String
  • password: String

Response

Success status code: 200

A successful response will send a Bearer token to be set in the Authorization header. This header will be evaluated for all restricted routes.

Example Authorization header for your reference:

headers: {
  'Authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IkZBS0VVU0VSIiwidXNlcklkIjoiNjA1M2JlZTg5YzU3MTcwMTQyMDExYzM2IiwiaWF0IjoxNjE2MTAxMTIzLCJleHAiOjE2MTYxMDQ3MjN9.IzWXlrFS7mQqR652keVEHnR4ayspk5yyyMjRpYTY7gw'
}

Additionally, the response will send a success message to verify your user is now signed in.

Example response:

{
    "message": "Sign in successful!",
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IkZBS0VVU0VSIiwidXNlcklkIjoiNjA1M2JlZTg5YzU3MTcwMTQyMDExYzM2IiwiaWF0IjoxNjE2MTAxMTIzLCJleHAiOjE2MTYxMDQ3MjN9.IzWXlrFS7mQqR652keVEHnR4ayspk5yyyMjRpYTY7gw"
}

Types:

  • message: String
  • token: String

Validation error status code: 422

In the event that the request body cannot be validated, a status code of 422 will be returned, along with an error message containing something similar to (for example):

"ValidationError": "Username must be more than 4 characters"

Cannot find user error status code: 401

In the event that the username entered does not correspond to an existing user, a status code of 401 will be returned, along with an error message like the following:

{
    "message": "User does not exist!"
}

Error status code: 500

In the event that there is another error due to a failure on the server side, a status code of 500 will be returned, along with a message stating the error.

Example 500 error message:

{
    "err": "[error] 29#29: *17 upstream timed out (110: Operation timed out) while reading response header from upstream, client: 172.18.0.1, server: stockstalker.tk"
}

User Watchlists

Each user is able to track a watchlist - a list of stocks they're interested in tracking the performance of.

Adding To or Removing From the Watchlist

Request

PATCH https://stockstalker.tk/api/user/watchlist

To add or remove a stock from a user's watchlist, send the symbol in the request body. StockStalker will then check for the presence of this stock in the watchlist. If the stock is already present, it will interpret the request as a "remove" request. If the stock is not present, it will be interpreted as an "add" request.

Example request body:

{
    "symbol": "AAPL"
}

Types:

  • symbol: String

Response

Success status code: 200

A successful response will return the user's watchlist. An example response is as follows:

{
  "watchlist": [
    {
      "_id": "6052a534e808fd0e72580cf2",
      "symbol": "AAPL",
      "companyName": "Apple, Inc. - Common Stock"
    },
    {
      "_id": "6052a534e808fd0e72580cf2",
      "symbol": "TSLA",
      "companyName": "Tesla - Common Stock"
    }
  ]
}

Types:

  • watchlist: List containing objects
    • _id: String (representing objectId)
    • symbol: String
    • companyName: String

Error status code: 403

An erroneous response will resolve with a status code of 403, and send the error message.

{
  "message": "error message"
}

Getting a User's Watchlist

Request

To get the logged in user's watchlist, you will make the following request.

GET https://stockstalker.tk/api/user/watchlist

Response

Success status code: 200

A successful response will return the user's watchlist. An example response is as follows:

{
  "watchlist": [
    {
      "_id": "6052a534e808fd0e72580cf2",
      "symbol": "AAPL",
      "companyName": "Apple, Inc. - Common Stock"
    },
    {
      "_id": "6052a534e808fd0e72580cf2",
      "symbol": "TSLA",
      "companyName": "Tesla - Common Stock"
    }
  ]
}

Types:

  • watchlist: List containing objects
    • _id: String (representing objectId)
    • symbol: String
    • companyName: String

Error status code: 403

An erroneous response will resolve with a status code of 403, and send the error message.

{
  "message": "error message"
}
`

Predictions

Let's face it - the most interesting thing about StockStalker is the ability to receive predictions about the potential performance of a stock.

Getting a Prediction for a Stock

Request

GET https://stockstalker.tk/api/stock/prediction/:symbol

Example request url:

GET http://stockstalker.tk/api/stock/prediction/aapl

GET http://stockstalker.tk/api/stock/prediction/AAPL

Response

Success status code: 200

A successful response will return a status code of 200, and the prediction or list of predictions for the given stock.

The current version of StockStalker does not return predictions if there is no news that day for the given stock. If there are no headlines matching your desired company's stock keywords, our prediction model will return a neutral prediction.

Example neutral response:

{
    "prediction": 2
}

Types:

  • prediction:Integer

When there are headlines about a given company, StockStalker will return to you a list of predictions. The most common prediction is the most likely for the day. Each headline generates a prediction.

Example response when headlines are present for the day:

{
    "prediction": [0, 1, 1, 1, 1]
}

Types:

  • prediction: List of integers

Error status code: 500

When there is an error, a status code of 500 will be returned, along with the error message.

Example error response:

{
    "message": "This is an error message"
}

Running Tests

To run tests simple run:

make test

If any of the tests fail or if the tests do not have at least 90% coverage the command will exit with a status code of 1.

backend's People

Contributors

dependabot[bot] avatar snyk-bot avatar starlightromero avatar tsamantanis avatar yinnyc avatar

Stargazers

 avatar

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.