Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 96 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,101 @@ only valid for a certain amount of time (perhaps an hour). After
that time, a backend cleanup job will come through and automatically
remove the reservation.

### Adding a new fungible item without a SKU

Used for adding fungible items without a SKU. This creates an item SKU.

```
POST /api/fungible
<body application/json> {
amount_in_stock: int | undefined
color: str | undefined
description: str | undefined
shipping_weight_grams: int | undefined
unit_price_cents: int | undefined
}
```

### Adding or updating a fungible item with a SKU

Used for adding or updating fungible items by their SKU. If the SKU exists, the item is updated else it is added.

```
PUT /api/fungible/<sku>
<body application/json> {
amount_in_stock: int | undefined
color: str | undefined
description: str | undefined
shipping_weight_grams: int | undefined
unit_price_cents: int | undefined
}
```

### Retrieving a fungible item by SKU

```
GET /api/fungible/<sku>
```

### Fungible add quantity

Used for adding quantity to an item stock by SKU.

```
POST /api/fungible/<sku>/add/<quantity>
```

### Fungible remove quantity

Used for removing quantity from an item stock by SKU.

```
POST /api/fungible/<sku>/remove/<quantity>
```

### Fungible reserve quantity

Used to reserve a quantity for a user to purchase later.

```
POST /api/fungible/<sku>/reserve/<quantity>
```

### Adding or updating a non-fungible item with a serial number and SKU

Used for adding or updating non-fungible items by their serial number and SKU. If the serial number and SKU exist, the item or SKU type are updated else it is added.

```
PUT /api/non-fungible/<sku>/<sn>
<body application/json> {
color: str | undefined
description: str | undefined # This is a SKU parent type attribute
notes: str | undefined
price_cents: int | undefined
shipping_weight_grams: int | undefined # This is a SKU parent type attribute
sold: bool | undefined
}
```

### Retrieving a non-fungible item by SKU and serial number

```
GET /api/non-fungible/<sku>/<sn>
```

### Reserving a non-fungible item for a sale

Used to reserve a non-fungible item for sale. This sets a UTC datetime on the reserved field for later expiration.
```
PUT /api/non-fungible/<sku>/<sn>/reservation
```

### Removing a non-fungible item reservation

```
DELETE /api/non-fungible/<sku>/<sn>/reservation
```

## Product Searches

TBD
Expand All @@ -72,4 +167,4 @@ TBD

## Fulfillment

TBD
TBD
Empty file added bitcoinstore/api/__init__.py
Empty file.
181 changes: 181 additions & 0 deletions bitcoinstore/api/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
from flask import Blueprint, request
from bitcoinstore.api.handlers.get_fungible \
import get_fungible as get_fungible_handler
from bitcoinstore.api.handlers.get_non_fungible \
import get_non_fungible as get_non_fungible_handler
from bitcoinstore.api.handlers.post_fungible \
import post_fungible as post_fungible_handler
from bitcoinstore.api.handlers.post_fungible_add_remove \
import post_fungible_add_remove as post_fungible_add_remove_handler
from bitcoinstore.api.handlers.put_fungible \
import put_fungible as put_fungible_handler
from bitcoinstore.api.handlers.post_fungible_reserve \
import post_fungible_reserve as post_fungible_reserve_handler
from bitcoinstore.api.handlers.put_non_fungible \
import put_non_fungible as put_non_fungible_handler
from bitcoinstore.api.handlers.put_non_fungible_reserve \
import put_non_fungible_reserve as put_non_fungible_reserve_handler
from bitcoinstore.api.handlers.delete_non_fungible_reserve \
import delete_non_fungible_reserve as delete_non_fungible_reserve_handler


api = Blueprint("api", __name__)


"""
get_fungible()
Used for retriving fungible items by sku.
"""
@api.get("/api/fungible/<string:sku>")
def get_fungible(sku):
try:
return get_fungible_handler(sku)
except Exception as e:
print(e)
return "Internal server error", 500


"""
get_non_fungible()
Used for retriving non-fungible items by sku and sn.
"""
@api.get("/api/non-fungible/<string:sku>/<string:sn>")
def get_non_fungible(sku, sn):
try:
return get_non_fungible_handler(sku, sn)
except Exception as e:
print(e)
return "Internal server error", 500


"""
post_fungible()
Used for adding fungible items without a sku.
This creates an item sku.
<body application/json> {
amount_in_stock: int | undefined
color: str | undefined
description: str | undefined
shipping_weight_grams: int | undefined
unit_price_cents: int | undefined
}
"""
@api.post("/api/fungible")
def post_fungible():
try:
properties = request.json
return post_fungible_handler(properties)
except Exception as e:
print(e)
return "Internal server error", 500

"""
post_fungible_add()
Used for adding quantity to an item by sku.
"""
@api.post("/api/fungible/<string:sku>/add/<int:quantity>")
def post_fungible_add(sku, quantity):
try:
return post_fungible_add_remove_handler(sku, quantity)
except Exception as e:
print(e)
return "Internal server error", 500


"""
post_fungible_remove()
Used for removing quantity from an item by sku.
"""
@api.post("/api/fungible/<string:sku>/remove/<int:quantity>")
def post_fungible_remove(sku, quantity):
try:
return post_fungible_add_remove_handler(sku, -quantity)
except Exception as e:
print(e)
return "Internal server error", 500


"""
post_fungible_reserve()
Used to reserve a quantity of fungible units.
"""
@api.post("/api/fungible/<string:sku>/reserve/<int:quantity>")
def post_fungible_reserve(sku, quantity) -> dict:
try:
return post_fungible_reserve_handler(sku, quantity)
except Exception as e:
print(e)
return "Internal server error", 500


"""
put_fungible()
Used for adding or updating fungible items by their sku.
If the sku exists, the item is updated else it is added.
<body application/json> {
amount_in_stock: int | undefined
color: str | undefined
description: str | undefined
shipping_weight_grams: int | undefined
unit_price_cents: int | undefined
}
"""
@api.put("/api/fungible/<string:sku>")
def put_fungible(sku) -> dict:
try:
properties = request.json
return put_fungible_handler(sku, properties)
except Exception as e:
print(e)
return "Internal server error", 500


"""
put_non_fungible()
Used for adding or updating non-fungible items by their serial number and sku.
If the serial number and sku exists, the item is updated else it is added.
<body application/json> {
color: str | undefined
description: str | undefined
notes: str | undefined
price_cents: int | undefined
shipping_weight_grams: int | undefined
sold: bool | undefined
}
"""
@api.put("/api/non-fungible/<string:sku>/<string:sn>")
def put_non_fungible(sku, sn):
try:
properties = request.json
return put_non_fungible_handler(sku, sn, properties)
except Exception as e:
print(e)
return "Internal server error", 500


"""
put_non_fungible_reserve()
Used to reserve the non-fungible item for sale.
This sets the reserved field by current datetime to later expire reservations.
"""
@api.put("/api/non-fungible/<string:sku>/<string:sn>/reservation")
def put_non_fungible_reserve(sku, sn):
try:
return put_non_fungible_reserve_handler(sku, sn)
except Exception as e:
print(e)
return "Internal server error", 500


"""
delete_non_fungible_reserve()
Used to reserve the non-fungible item for sale.
This sets the reserved field by current datetime to later expire reservations.
"""
@api.delete("/api/non-fungible/<string:sku>/<string:sn>/reservation")
def delete_non_fungible_reserve(sku, sn):
try:
return delete_non_fungible_reserve_handler(sku, sn)
except Exception as e:
print(e)
return "Internal server error", 500
50 changes: 50 additions & 0 deletions bitcoinstore/api/handlers/delete_non_fungible_reserve.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""
Reserves a non-fungible item for sale.
"""

from bitcoinstore.extensions import db
from bitcoinstore.api.models.NonFungibleItem import NonFungibleItem
from bitcoinstore.api.models.NonFungibleType import NonFungibleType

def delete_non_fungible_reserve(sku, sn) -> dict:

try:
type = db.session.query(NonFungibleType).get(sku)

if not type:
print("Here 1")
return "NonFungibleType: SKU does not exist", 404


item = db.session.query(NonFungibleItem).get(sn)

if not item:
print("Here 2")
return "NonFungibleItem: SN does not exist", 404

if item.get_sold() is True:
return "Item is already sold", 405
else:
item.set_reserved(False)

db.session.add(item)
db.session.commit()

item_summary = item.get_summary()
type_summary = type.get_summary()

return {
"sn": item_summary['sn'],
"color": item_summary['color'],
"description": type_summary['description'],
"notes": item_summary['notes'],
"price_cents": item_summary['price_cents'],
"reserved": item_summary['reserved'],
"shipping_weight_grams": type_summary['shipping_weight_grams'],
"sku": item_summary['sku'],
"sold": item_summary['sold']
}

except Exception as e:
print(e)
return {}
21 changes: 21 additions & 0 deletions bitcoinstore/api/handlers/get_fungible.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
Handles retrival of a fungible item by sku.
"""

import uuid
from bitcoinstore.extensions import db
from bitcoinstore.api.models.FungibleItem import FungibleItem

def get_fungible(sku) -> dict:

try:
item = db.session.query(FungibleItem).get(sku)

if not item:
return "FungibleItem: SKU does not exist"

return item.get_summary()

except Exception as e:
print(e)
return {}
Loading