From 5023ce3ea3fa56c9f326a85566dd731de8079be5 Mon Sep 17 00:00:00 2001 From: Alauddin Maulana Hirzan Date: Tue, 6 Aug 2024 08:44:03 +0700 Subject: [PATCH] Update --- CouchDB Example/config.py | 3 + CouchDB Example/main.py | 119 ++++++++++++++++++++++++++++++++++++++ CouchDB Example/main.py~ | 2 + 3 files changed, 124 insertions(+) create mode 100644 CouchDB Example/config.py create mode 100644 CouchDB Example/main.py create mode 100644 CouchDB Example/main.py~ diff --git a/CouchDB Example/config.py b/CouchDB Example/config.py new file mode 100644 index 0000000..dec0f17 --- /dev/null +++ b/CouchDB Example/config.py @@ -0,0 +1,3 @@ +username: str = "admin" +password: str = "freebsd" +url: str = "https://[username]:[password]@maulanahirzan.ddns.net/couchdb" diff --git a/CouchDB Example/main.py b/CouchDB Example/main.py new file mode 100644 index 0000000..3bb6318 --- /dev/null +++ b/CouchDB Example/main.py @@ -0,0 +1,119 @@ +import config +import requests +import json +from time import time + +class Sofa: + def __init__(self): + # Config Based Params + self.username: str = config.username + self.password: str = config.password + self.url: str = config.url + self.url = self.url.replace("[username]", f"{self.username}") + self.url = self.url.replace("[password]", f"{self.password}") + # Empty Params + self.q_url: str = "None" + self.db_name: str = "None" + + def is_up(self): + # Assign Variables + self.q_url = f"{self.url}/_up" + # Query + with requests.get(self.q_url) as resp: + self.status = resp.status_code + # Finalization + if self.status == 200: + return 1 + else: + return 0 + + + # Database Funcs + def create_db(self, db_name: str): + # Assign Variables + self.db_name = db_name + self.q_url = f"{self.url}/{self.db_name}" + # Query + with requests.put(self.q_url) as resp: + self.status = resp.status_code + # Finalization + if self.status == 201: + return 1 + else: + return 0 + + def delete_db(self, db_name: str): + # Assign Variables + self.db_name = db_name + self.q_url = f"{self.url}/{self.db_name}" + # Query + with requests.delete(self.q_url) as resp: + self.status = resp.status_code + # Finalization + if self.status == 200: + return 1 + else: + return 0 + + # Document Funcs + def get_all_docs(self, db_name: str): + # Assign Variables + self.db_name = db_name + self.q_url = f"{self.url}/{self.db_name}/_all_docs" + # Query + with requests.get(self.q_url) as resp: + self.data = resp.json() + # Finalization + return self.data + + def insert_doc(self, db_name: str, doc_data: dict): + # Assign Variables + self.db_name = db_name + self.doc_id = str(int(time())) + self.doc_data = json.dumps(doc_data) # Convert to JSON + self.q_url = f"{self.url}/{self.db_name}/{self.doc_id}" + # Query + with requests.put(self.q_url, data=self.doc_data) as resp: + self.status = resp.status_code + # Finalization + if self.status == 201 or self.status == 202: + return 1 + else: + return 0 + + def read_doc(self, db_name: str, doc_id: str): + # Assign Variables + self.db_name = db_name + self.doc_id = doc_id + self.q_url = f"{self.url}/{self.db_name}/{self.doc_id}" + # Query + with requests.get(self.q_url) as resp: + self.status = resp.status_code + self.data = resp.json() + # Finalization + if self.status == 200: + return 1, self.data + else: + return 0, {} + + def truncate_db(self): + pass + + +# Testing Script +if __name__ == "__main__": + # Create Object + sofa = Sofa() + # Test Connectivity + status = sofa.is_up() + # Test Query All Docs + if status == True: + print("Server Online") + q_status, q_data = sofa.read_doc("pidata", "1722907282") + if q_status == True: + print("Query Success") + print(q_data) + else: + print("Query Failed") + else: + print("Server Offline") diff --git a/CouchDB Example/main.py~ b/CouchDB Example/main.py~ new file mode 100644 index 0000000..22b71a6 --- /dev/null +++ b/CouchDB Example/main.py~ @@ -0,0 +1,2 @@ +import config +