Skip to content

Commit

Permalink
chore: checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ivelin committed Aug 29, 2021
1 parent a7ea504 commit 839c2ed
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 25 deletions.
8 changes: 4 additions & 4 deletions src/ambianic/webapp/fastapi_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,10 @@ def get_config():
def get_config_source(source_id):
return jsonify(config_sources.get(source_id))

@app.put('/api/config/source/{source_id}')
def update_config_source(source: SensorSource, source_id: str):
config_sources.save(source_id, source)
return config_sources.get(source_id)
@app.put('/api/config/source')
def update_config_source(source: SensorSource):
config_sources.save(source)
return config_sources.get(source.id)

@app.delete('/api/config/source/{source_id}')
def delete_config_source(source_id: str):
Expand Down
14 changes: 2 additions & 12 deletions src/ambianic/webapp/server/config_sources.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

from fastapi import HTTPException
from fastapi import HTTPException, status
from ambianic import config
from pydantic import BaseModel

Expand All @@ -19,19 +19,9 @@ class SensorSource(BaseModel):
def get(source_id):
"""Retrieve a source by id"""
log.info("Get source_id=%s", source_id)

if not source_id:
raise HTTPException(status_code=400, detail="source id is empy")

if not isinstance(source_id, str):
raise HTTPException(status_code=400, detail="source id should be a string")

source = config.sources[source_id]

if source is None:
raise HTTPException(status_code=404, detail="source not found")


raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="source id not found")
return source


Expand Down
32 changes: 23 additions & 9 deletions tests/test_rest_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,26 +138,40 @@ def test_get_config(client):


def test_save_source(client):
rv = client.put('/api/config/source/test1', json={
src_target = json={
"id": "test1",
"uri": "test",
"type": "video",
"live": True
})
data = json.loads(rv.data)
}
rv = client.put('/api/config/source/test1', src_target)
data = rv.json()
assert data
assert data["id"] == "test1"
assert data["live"] == True
src_target["live"] = False
rv = client.put('/api/config/source/test1', src_target)
data = rv.json()
assert data
assert data["id"] == "test1"
assert data["id"] == "test1"
assert data["live"] == False


def test_delete_source(client):
rv = client.put('/api/config/source/test1', json={
src_target = json={
"id": "test1",
"uri": "test",
"type": "video",
"live": True
})
log.error(" rv ###### >>>> " + str(rv.json()))
assert rv.json() == {"id": "test1"}
}
rv = client.put('/api/config/source', src_target)
assert rv.json()["id"] == "test1"
rv = client.delete('/api/config/source/test1')
assert rv.json()["status"] == "success"
# attempting to delete the same source again should fail
rv = client.delete('/api/config/source/test1')
assert rv["status"] == "success"
assert rv.status_code == 404
assert rv.json() == {"detail": "source id not found"}


def test_ping(client):
Expand Down

0 comments on commit 839c2ed

Please sign in to comment.