Skip to content

Commit

Permalink
Merge pull request #711 from kikeelectronico/refactor_rest_api
Browse files Browse the repository at this point in the history
Refactor rest api
  • Loading branch information
kikeelectronico authored Sep 1, 2024
2 parents 3b51723 + db69514 commit 78acb0c
Show file tree
Hide file tree
Showing 23 changed files with 789 additions and 973 deletions.
25 changes: 12 additions & 13 deletions back/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,7 @@ def __init__(self):

# BACKUP

def createBackupFile(self):
data = self.getBackupData()
file = open('../' + self.homewareFile, 'w')
file.write(json.dumps(data))
file.close()

def getBackupData(self):
def getBackup(self):
user = self.mongo_db["users"].find()[0]
oauth = self.mongo_db["oauth"].find()[0]
data = {
Expand Down Expand Up @@ -174,10 +168,7 @@ def getBackupData(self):
}
return data

def loadBackupFile(self):
file = open('../' + self.homewareFile, 'r')
data = json.load(file)
file.close()
def restoreBackup(self, data):
# Load the devices
for device in data['devices']:
device["_id"] = device["id"]
Expand Down Expand Up @@ -403,15 +394,15 @@ def validateUserToken(self, token):
ddbb_token = user_data["token"]
return token == ddbb_token

def googleSync(self, username, password, responseURL):
def googleSync(self, username, password):
username = headers['user']
password = headers['pass']
user_data = self.mongo_db["users"].find()[0]
ddbb_password_hash = user_data["password"]
ddbb_username = user_data["username"]
auth = False
if username == ddbb_username and bcrypt.checkpw(password.encode('utf-8'),ddbb_password_hash[2:-1].encode('utf-8')):
return responseURL
return self.redis.get("responseURL")
else:
return "fail"

Expand Down Expand Up @@ -469,6 +460,9 @@ def validateOauthCredentials(self, type, value):
if not type in ["client_id", "client_secret"]: return False
return self.mongo_db["settings"].find()[0][type] == value

def setResponseURL(self, url):
self.redis.set("responseURL", url)

# SETTINGS

def getSettings(self):
Expand Down Expand Up @@ -504,6 +498,11 @@ def updateSyncGoogle(self, status):
def getSyncDevices(self):
return self.mongo_db["settings"].find()[0]["sync_devices"]

def createServiceAccountKeyFile(self, serviceaccountkey):
with open("../files/google.json", "w") as file:
file.write(json.dumps(serviceaccountkey))
self.log('Info', 'A google auth file has been uploaded')

# SYSTEM

def getRedisStatus(self):
Expand Down
29 changes: 29 additions & 0 deletions back/errorResponses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from fastapi.responses import JSONResponse

FOUR_O_ONE = JSONResponse(status_code=401,
content = {
"error": "Authentication is needed",
"code": 401,
"note": "See the documentation https://homeware.enriquegomez.me/api-docs.html"
})

FOUR_O_THREE = JSONResponse(status_code=403,
content = {
"error": "Authentication fail",
"code": 403,
"note": "See the documentation https://homeware.enriquegomez.me/api-docs.html"
})

FOUR_O_FOUR = JSONResponse(status_code=404,
content = {
"error": "Not found",
"code": 404,
"note": "See the documentation https://homeware.enriquegomez.me/"
})

FOUR_O_O = JSONResponse(status_code=400,
content = {
"error": "Operation not supported",
"code": 400,
"note": "See the documentation https://homeware.enriquegomez.me/"
})
Loading

0 comments on commit 78acb0c

Please sign in to comment.