Giter Club home page Giter Club logo

zotbinsapi's Introduction

ZotBins Internal API

This API is hosted by pythonanywhere.com and the following requests are currently available:

Table of Contents

  1. Add Observation
  2. Get Observation
  3. Count Observation
  4. Add Barcode
  5. Get Barcode
  6. Add Image
  7. View Image
  8. Get Image List
  9. Get Observation Stats as CSV

Add Observation

Endpoint: base URL + /observation/add
Method: POST
Body example:

[
    {
        "timestamp": "2020-02-01 9:30:01",
        "payload":
        {
            "weight": 100
        },
        "sensor_id": "ZBin2",
        "type": 2
    }
]

Description:

  • Adds new observation(s) to the database, currently takes 3 different observation types (2=weight, 3=distance, 5=frequency)

Get Observation

Endpoint: base URL + /observation/get
Method: GET
Params:

  • sensor_id
  • start_timestamp
  • end_timestamp

Description:

  • Fetches observation(s) recorded by a sensor within a specific timeframe
  • Sensor ID # can be found in our Google drive > NOTES-Milestones-Tasks > ZotBinsID Tracker
  • Sensor ID format = "ZBin" + sensor ID # + ("B" if breakbeam sensor, "D" if ultrasonic sensor)

Count Observation

Endpoint: base URL + /observation/count
Method: GET
Params:

  • sensor_id
  • start_timestamp
  • end_timestamp

Description:

  • Fetches a sum for the observation(s) recorded by a sensor within a specific timeframe
  • Sensor ID # can be found in our Google drive > NOTES-Milestones-Tasks > ZotBinsID Tracker
  • Sensor ID format = "ZBin" + sensor ID # + ("B" if breakbeam sensor, "D" if ultrasonic sensor)

Add Barcode

Endpoint: base URL + /barcode/add
Method: POST
Body example:

[
    {
        "name": "Fiji Water Bottle",
        "type": "water bottle",
        "barcode": 123456789012,
        "wasteBin": "recycling",
        "instructions": "Wrapper and cap in landfill, bottle in recycling"
    }
]

Description: Adds new barcodes(s) to the database

Get Barcode

Endpoint: base URL + /barcode/get
Method: GET
Params:

  • barcode

Description: Get the item, item type, and instructions on how to properly dispose of it

Add Image

Endpoint: base URL + /image/add
Method: GET
Body example:

import requests
import json

url = "BASE_URL/image/add"

with open(YOUR_FILE_PATH, 'rb') as f:
    r = requests.post(url, files={"file": f})

Description: Adds an image to the filesystem. The purpose of the images is to collect waste related data. You can also interact with the HTML UI to post an image on base URL + /image/add

View Image

Endpoint: base URL + /uploads/
Method: GET
Description: This API allows you read access to the image file in our server if the image exists.

Get Image List

Endpoint: base URL + /observation/get/image-list
Method: GET
Return Example:

{
  "imageNames": [
    "2020-04-21_092647_ZBin7.jpg",
    "2020-04-21_122626_ZBin7.jpg",
    "2020-04-16_144716_ZBin7.jpg",
    "2020-04-14_223731_ZBin7.jpg",
    "2020-04-18_230215_ZBin7.jpg",
    "2020-04-18_205732_ZBin7.jpg"]
}

Description: This API allows you to view all the collected images we have from the ZotBins system.

Get Observation Stats as CSV

Endpoint: base URL + /observation/stats
Method: Get
Body Example:

{
    "sensor_id":"ZBin3B",
    "start_timestamp":"2020-02-04"
    "end_timestamp":"2020-02-05"
   
}

Request Example:

https://zotbins.pythonanywhere.com/observation/stats?sensor_id=ZBin3B&start_timestamp=2020-02-04&end_timestamp=2020-02-05

Description: This API allows you to download the stats of the bin data as a CSV file from a given bin at a certain time range.

Additional info:

  • This API is the same format as the old TIPPERS API
    • Checkout the postman collection file in this repo (ZotBinsAPI.postman_collection.json) for request examples.
  • ZotBinsAPI.py is the script that runs the flask app which acts as a middle man between user (us) and the database.
    • It requires two extra files that aren't included in this repo: config.py (contains db info) and queries.py (contains sql queries)

zotbinsapi's People

Contributors

gracechoe avatar okyang avatar jessechong avatar alpinet avatar mlafreni avatar patrickanguyen avatar aishwab1 avatar aishwaryabh avatar caojoshua avatar

Stargazers

 avatar  avatar

Watchers

Primal Pappachan avatar James Cloos avatar Danny avatar

Forkers

patrickanguyen

zotbinsapi's Issues

Add mock data API for web/mobile team

An issue that the web/mobile team has been facing is in regards to the API calls. Since we aren't collecting data now, the web and mobile team are just hard coding values.

We should add a mock data API so that they can simulate API calls. When we start collecting real data, it will be easy to switch the API call over. So this way, the code will be ready-- we just need to change the endpoint and some params if needed. Will be a useful tool.

Issues with "Nonexistent Barcodes"

Hi, please view the following lines for the "get barcode" API -

ZotBinsAPI/ZotBinsAPI.py

Lines 227 to 239 in af17e16

barcode = request.args.get("barcode")
with con.cursor() as cur:
cur.execute(barcodeQueries.get_query, (barcode, ))
res = cur.fetchone()
if not res:
name = None
myType = None
barcode = barcode
wasteBin = None
instructions = None
cur.execute(barcodeQueries.insert_query, (name, myType, barcode, wasteBin, instructions))
con.commit()
return "added empty barcode"

When I try to "GET" a nonexistent barcode, the code will automatically populate an entry with NULL entries for all except the barcode num (e.g., if I try to get the barcode "xxxxx" it will add that into the DB with type, name, etc. set to NULL). This is fine!

However, there is no way to edit this barcode anymore... if you use the "barcode/add" call with the same barcode num in order to modify it, then you get an error ((1062, "Duplicate entry 'xxxxxx' for key 'PRIMARY'")). So, the only thing I can do is call "GET" on an all-NULL entry.

I have 3 suggestions (choose 1, not all):
(1) Modify the "barcode/get" API to not insert a NULL'd entry, OR
(2) Modify the "barcode/add" API to update an existing entry when using a duplicate PRIMARY key, OR
(3) Create a new endpoint called "barcode/update" that allows for the updating of entries.

Let me know what you think. I think (3) is the best option, because you don't need to modify the "get" or "add" endpoints. I think (2) is a bad design choice (getting the error message is a good thing) and (1) is debatable (maybe just return a message without adding a NULL'd entry?)

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.