mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-24 10:31:05 +00:00
Add BookWyrm user-agent to http requests
This allows other software to identify BookWyrm in calls, as well as will allow BookWyrm to differentiate between calls done from other fediverse software and BookWyrm to answer with specific BookWyrm data.
This commit is contained in:
parent
26c0880fe4
commit
fb10cb35ad
3 changed files with 17 additions and 3 deletions
|
@ -3,7 +3,7 @@ import json
|
||||||
from django.utils.http import http_date
|
from django.utils.http import http_date
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from bookwyrm import models
|
from bookwyrm import models, settings
|
||||||
from bookwyrm.activitypub import ActivityEncoder
|
from bookwyrm.activitypub import ActivityEncoder
|
||||||
from bookwyrm.tasks import app
|
from bookwyrm.tasks import app
|
||||||
from bookwyrm.signatures import make_signature, make_digest
|
from bookwyrm.signatures import make_signature, make_digest
|
||||||
|
@ -79,6 +79,7 @@ def sign_and_send(sender, data, destination):
|
||||||
'Digest': digest,
|
'Digest': digest,
|
||||||
'Signature': make_signature(sender, destination, now, digest),
|
'Signature': make_signature(sender, destination, now, digest),
|
||||||
'Content-Type': 'application/activity+json; charset=utf-8',
|
'Content-Type': 'application/activity+json; charset=utf-8',
|
||||||
|
'User-Agent': settings.USER_AGENT,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
if not response.ok:
|
if not response.ok:
|
||||||
|
|
|
@ -8,7 +8,7 @@ import requests
|
||||||
from requests import HTTPError
|
from requests import HTTPError
|
||||||
from requests.exceptions import SSLError
|
from requests.exceptions import SSLError
|
||||||
|
|
||||||
from bookwyrm import activitypub, models
|
from bookwyrm import activitypub, models, settings
|
||||||
|
|
||||||
|
|
||||||
class ConnectorException(HTTPError):
|
class ConnectorException(HTTPError):
|
||||||
|
@ -42,6 +42,7 @@ class AbstractMinimalConnector(ABC):
|
||||||
'%s%s' % (self.search_url, query),
|
'%s%s' % (self.search_url, query),
|
||||||
headers={
|
headers={
|
||||||
'Accept': 'application/json; charset=utf-8',
|
'Accept': 'application/json; charset=utf-8',
|
||||||
|
'User-Agent': settings.USER_AGENT,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
if not resp.ok:
|
if not resp.ok:
|
||||||
|
@ -196,6 +197,7 @@ def get_data(url):
|
||||||
url,
|
url,
|
||||||
headers={
|
headers={
|
||||||
'Accept': 'application/json; charset=utf-8',
|
'Accept': 'application/json; charset=utf-8',
|
||||||
|
'User-Agent': settings.USER_AGENT,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
except RequestError:
|
except RequestError:
|
||||||
|
@ -213,7 +215,12 @@ def get_data(url):
|
||||||
def get_image(url):
|
def get_image(url):
|
||||||
''' wrapper for requesting an image '''
|
''' wrapper for requesting an image '''
|
||||||
try:
|
try:
|
||||||
resp = requests.get(url)
|
resp = requests.get(
|
||||||
|
url,
|
||||||
|
headers={
|
||||||
|
'User-Agent': settings.USER_AGENT,
|
||||||
|
},
|
||||||
|
)
|
||||||
except (RequestError, SSLError):
|
except (RequestError, SSLError):
|
||||||
return None
|
return None
|
||||||
if not resp.ok:
|
if not resp.ok:
|
||||||
|
|
|
@ -3,6 +3,8 @@ import os
|
||||||
|
|
||||||
from environs import Env
|
from environs import Env
|
||||||
|
|
||||||
|
import requests
|
||||||
|
|
||||||
env = Env()
|
env = Env()
|
||||||
DOMAIN = env('DOMAIN')
|
DOMAIN = env('DOMAIN')
|
||||||
|
|
||||||
|
@ -150,3 +152,7 @@ STATIC_URL = '/static/'
|
||||||
STATIC_ROOT = os.path.join(BASE_DIR, env('STATIC_ROOT', 'static'))
|
STATIC_ROOT = os.path.join(BASE_DIR, env('STATIC_ROOT', 'static'))
|
||||||
MEDIA_URL = '/images/'
|
MEDIA_URL = '/images/'
|
||||||
MEDIA_ROOT = os.path.join(BASE_DIR, env('MEDIA_ROOT', 'images'))
|
MEDIA_ROOT = os.path.join(BASE_DIR, env('MEDIA_ROOT', 'images'))
|
||||||
|
|
||||||
|
USER_AGENT = "%s (BookWyrm/%s; +https://%s/)" % (requests.utils.default_user_agent(),
|
||||||
|
"0.1", # TODO: change 0.1 into actual version
|
||||||
|
DOMAIN)
|
||||||
|
|
Loading…
Reference in a new issue