1

I want to separate my code with FastAPI.

My endpoint should receive a list of instance nodes and pass the list to NeoController.create_nodes.

Old Code(Working):

from ontologies.neo import Node
from typing import List

db_instance = "Instance:Edge:Datastore:AbstractDatastore:Docker:MongoDB"
ds_instance = "Instance:Edge:DataSource:Camera:IDS"
instance = [
    Node(db_instance, "MongoDB", attribute=['']),
    Node(ds_instance, "IDS", attribute=[''])
]
NeoController.create_nodes(instance)

New Code:

from ontologies.neo import Node
from typing import List

db_instance = "Instance:Edge:Datastore:AbstractDatastore:Docker:MongoDB"
ds_instance = "Instance:Edge:DataSource:Camera:IDS"
instance = [
    Node(db_instance, "MongoDB", attribute=['']),
    Node(ds_instance, "IDS", attribute=[''])
]
request = requests.post('http://127.0.0.1:8080/create/?instance=instance') #how to format the URL the right way?

API-Code:

from ontologies.neo import Node
from fastapi import FastAPI, Query
from typing import List

app=FastAPI(    
      title="Neo4jAPI",
)

@app.post("/create/")
def post(instance: List[Node] = Query(None)): #How to fix the Header, i am getting a Assertation Error
      NeoController.create_nodes(instance)
      return JSONResponse(instance)

Questions:

  • How to fix the URL-Request?
  • How to fix the API-Header?

I read some other questions/posts and code examples, but wasn't able to fix it.

Chris
  • 18,724
  • 6
  • 46
  • 80
Tim
  • 35
  • 4
  • 1
    Asked the other day: https://stackoverflow.com/a/76237301/11612918 – M.O. May 15 '23 at 10:47
  • 1
    You generally want to use a JSON body and not GET arguments to allow you include data with a proper structure - create a Pydantic model that matches what you're looking to include (looks like it should have fields like `instance: str`, `type: str` and `attributes: List[str]`). You then use these values to create your Node - you can't give it as a direct type in FastAPI, as FastAPI has no idea what `Node` from ontologies.neo` is (unless it's a pydantic model?). – MatsLindh May 15 '23 at 11:25
  • 1
    The following answers might also prove helpful to you: [this](https://stackoverflow.com/a/73982479/17865804), [this](https://stackoverflow.com/a/71039241/17865804) and [this](https://stackoverflow.com/a/70840850/17865804) – Chris May 15 '23 at 15:10

0 Answers0