-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrev.py
More file actions
77 lines (64 loc) · 2.37 KB
/
rev.py
File metadata and controls
77 lines (64 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import sqlite3
import requests
class APIClient:
"""Simulate an external API Client"""
def get_user_data(self, username, email, age):
response= requests.get(f"http://api.example.com/users?username={username}&email={email}&age={age}")
if response.status_code == 200:
return response.json()
raise ValueError("API request failed")
class DatabaseClient:
def __init__(self, db_name= 'users.db'):
self.db_name= db_name
self._create_table()
def _create_table(self):
"""CREATING the table to store the data fetched from API"""
conn= sqlite3.connect(self.db_name)
cursor= conn.cursor()
cursor.execute(
'''CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT,
email TEXT,
age INT CHECK(age >= 0)
)
'''
)
conn.commit()
conn.close()
def save_user(self, username, email, age):
conn= sqlite3.connect(self.db_name)
cursor= conn.cursor()
cursor.execute(
"INSERT INTO users (username, email, age) VALUES (?, ?, ?)", (username, email, age)
)
conn.commit()
#fetching the inserted data
cursor.execute(
"SELECT username, email, age FROM users WHERE email= ?", (username, email, age)
)
user_get= cursor.fetchone()
conn.close()
if user_get:
return {
'username':user_get[0],
'email': user_get[1],
'age': user_get[2]
}
class UserServices:
def __init__(self, api_client, db_client):
self.api_client= api_client
self.db_client= db_client
def register_user(self, username, email, age):
"""Affirming the age is greater than or equal to 0"""
if age < 0:
raise ValueError("Age must be non-negative")
"""Fetch from the API, save to DB and return the username as a uppercase"""
api_get= self.api_client.get_user_data(username, email, age)
user_get= self.db_client.save_user(api_get['username'], api_get['email'], api_get['age'])
return user_get['username'].upper()
#result format
# api= APIClient()
# db= DatabaseClient()
# service= UserServices(api, db)
# result= service.register_user('username', 'email', 'age')