Skip to content

Commit

Permalink
Replace requests with python-httpx
Browse files Browse the repository at this point in the history
  • Loading branch information
le717 committed Jun 8, 2024
1 parent ae24075 commit c3f1569
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 157 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ _Released TDB_
- Update minimum Python version to 3.11
- Update to Flask v3
- ~~Update SQLAlchemy to v2~~ _update pending_
- Replace `requests` with `python-httpx`
- Update all dependencies to their latest versions
- Switch to ruff for linting and formatting

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

### Auto-embed JavaScript

Starting with version v1.3.0, TODO: write me!
Starting with version 1.3.0, TODO: write me!

## Required Secrets

Expand Down
232 changes: 91 additions & 141 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ sys-vars = "^5.0.0"
gunicorn = "^22.0.0"
sqlalchemy = "^1.4.44"
flask-sqlalchemy = "^3.0.2"
requests = "^2.32.3"
flask-cors = "^4.0.1"
flask-db = "^0.4.1"
httpx = "^0.27.0"

[tool.poetry.group.dev]

Expand Down
17 changes: 8 additions & 9 deletions src/core/database/linkrot.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from typing import Literal, TypedDict

import requests
import httpx
import sys_vars

from src.core.logger import LINKROT
from src.core.database import weblink
from src.core.database.schema import RottedLinks, WebLink, db

from src.core.logger import LINKROT

__all__ = ["check_all", "check_one", "delete"]

Expand All @@ -26,20 +25,20 @@ class RotResult(TypedDict):
def __ping_url(url: str) -> bool:
"""Check a link for rotting."""
try:
r = requests.head(url)
r = httpx.head(url)
return r.status_code in (
requests.codes.ok,
requests.codes.created,
requests.codes.no_content,
requests.codes.not_modified,
httpx.codes.OK,
httpx.codes.CREATED,
httpx.codes.NO_CONTENT,
httpx.codes.NOT_MODIFIED,
)
except Exception:
return False


def __ping_wayback_machine(url: str) -> Literal[False] | str:
"""Check the Web Archive for an archived URL."""
r = requests.get(f"https://archive.org/wayback/available?url={url}").json()
r = httpx.get(f"https://archive.org/wayback/available?url={url}").json()
if not r["archived_snapshots"]:
return False
return r["archived_snapshots"]["closest"]["url"]
Expand Down
9 changes: 4 additions & 5 deletions src/core/logger.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from datetime import datetime
import logging
from datetime import datetime
from logging.handlers import RotatingFileHandler
from pathlib import Path

from flask import request
import requests
import httpx
import sys_vars

from flask import request

__all__ = ["LINKROT", "DiscordHandler", "file_handler"]

Expand Down Expand Up @@ -39,7 +38,7 @@ def format(self, record: logging.LogRecord) -> dict:
return {"content": _linkrot_formatter(record)}

def emit(self, record: logging.LogRecord) -> logging.LogRecord:
requests.post(
httpx.post(
self.url,
headers={"Content-type": "application/json"},
json=self.format(record),
Expand Down

0 comments on commit c3f1569

Please sign in to comment.