I'm trying to create an endpoint to get a json dataset in the database "DSITE1" that i've stored in the live ravendb server "http://live-test.ravendb.net", but it's not working.
from fastapi import FastAPI
from pyravendb.store import document_store
app = FastAPI()
document_store = document_store.DocumentStore(urls=["http://live-test.ravendb.net"], database="DSITE1")
document_store.initialize()
@app.get("/json_data/")
def json_data():
with document_store.open_session() as session:
data = session.load("dicts/1-A")
return data
Here's an example of original json data :
[
{
"id": 1,
"name": "Widget",
"price": 10.99
},
{
"id": 2,
"name": "Gadget",
"price": 24.95
},
{
"id": 3,
"name": "Doodad",
"price": 5.99
}
]
Here's the json data after storage in the database DSITE1 :
{
{
"id": 1,
"name": "Widget",
"price": 10.99
},
{
"id": 2,
"name": "Gadget",
"price": 24.95
},
{
"id": 3,
"name": "Doodad",
"price": 5.99
},
"Id": "dicts/1-A",
"@metadata": {
"@collection": "dicts",
"Raven-Python-Type": "builtins.dict"
}
}
Here's how i've stored it in the live server :
from ravendb import DocumentStore
import json
document_store = DocumentStore(urls=["http://live-test.ravendb.net"], database="DSITE1")
document_store.initialize()
with open(path\site1.json", "r") as json_file:
json_data = json.load(json_file)
with document_store.open_session() as session:
for data in json_data:
session.store(data)
session.save_changes()
document_store.close()
In your RavenDB instance dont exist DSITE1 database.
But for example if u choose DBGZ database and one of products by ID it's working fine.
Code:
from fastapi import FastAPI
from pyravendb.store import document_store
app = FastAPI()
# select DBGZ DB for example
document_store = document_store.DocumentStore(urls=["http://live-test.ravendb.net"], database="DBGZ")
document_store.initialize()
@app.get("/json_data/")
def json_data():
with document_store.open_session() as session:
# select product by product ID
data = session.load("products/77-A")
return data
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="localhost", port=8080)
You can make the product id in FastAPI path parameters.
For example:
@app.get("/json_data/{product_id}")
def json_data(product_id: str):
print(product_id)
with document_store.open_session() as session:
# select product by product ID
data = session.load(product_id)
return data