Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 22 additions & 21 deletions examples/randomuser-sqlite.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
#!/usr/bin/env python3
# coding: utf-8

import json # https://docs.python.org/3/library/json.html
import requests # https://github.com/kennethreitz/requests
import records # https://github.com/kennethreitz/records
import json
import requests
import records

# randomuser.me generates random 'user' data (name, email, addr, phone number, etc)
r = requests.get('http://api.randomuser.me/0.6/?nat=us&results=10')
j = r.json()['results']
# Fetch random user data from randomuser.me API
response = requests.get('http://api.randomuser.me/0.6/?nat=us&results=10')

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Add a timeout parameter and use HTTPS.

The requests.get call has no timeout, so it can block indefinitely if the server is unresponsive (Ruff S113). Also, prefer https:// over http:// to avoid transmitting data in plaintext.

Proposed fix
-response = requests.get('http://api.randomuser.me/0.6/?nat=us&results=10')
+response = requests.get('https://api.randomuser.me/0.6/?nat=us&results=10', timeout=30)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
response = requests.get('http://api.randomuser.me/0.6/?nat=us&results=10')
response = requests.get('https://api.randomuser.me/0.6/?nat=us&results=10', timeout=30)
🧰 Tools
🪛 Ruff (0.14.14)

[error] 9-9: Probable use of requests call without timeout

(S113)

🤖 Prompt for AI Agents
In `@examples/randomuser-sqlite.py` at line 9, The requests.get call uses http and
has no timeout; change the URL to use HTTPS and add a timeout parameter (e.g.,
timeout=10) to prevent indefinite blocking—update the call to the requests.get
invocation in this file to use
"https://api.randomuser.me/0.6/?nat=us&results=10" and include a suitable
timeout argument.

user_data = response.json()['results']

# Valid SQLite URL forms are:
# sqlite:///:memory: (or, sqlite://)
# sqlite:///relative/path/to/file.db
# sqlite:////absolute/path/to/file.db
# Database connection string
DATABASE_URL = 'sqlite:///users.db'

# records will create this db on disk if 'users.db' doesn't exist already
db = records.Database('sqlite:///users.db')
# Initialize the database
db = records.Database(DATABASE_URL)

# Create the 'persons' table
db.query('DROP TABLE IF EXISTS persons')
db.query('CREATE TABLE persons (key int PRIMARY KEY, fname text, lname text, email text)')

for rec in j:
user = rec['user']
name = user['name']
db.query('CREATE TABLE persons (key INTEGER PRIMARY KEY, fname TEXT, lname TEXT, email TEXT)')

# Insert user data into the 'persons' table
for record in user_data:
user = record['user']
key = user['registered']
fname = name['first']
lname = name['last']
fname = user['name']['first']
lname = user['name']['last']
email = user['email']
db.query('INSERT INTO persons (key, fname, lname, email) VALUES(:key, :fname, :lname, :email)',
key=key, fname=fname, lname=lname, email=email)
db.query(
'INSERT INTO persons (key, fname, lname, email) VALUES (:key, :fname, :lname, :email)',
key=key, fname=fname, lname=lname, email=email
)

# Retrieve and print the contents of the 'persons' table as CSV
rows = db.query('SELECT * FROM persons')
print(rows.export('csv'))
1 change: 0 additions & 1 deletion records.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,6 @@ def transaction(self):
tx.commit()
except:
tx.rollback()
raise
finally:
conn.close()

Expand Down
Loading