Commit 683e4c66 authored by Guilhem Saurel's avatar Guilhem Saurel
Browse files

Robotpkg.extended_target + requests → httpx

parent dafff9de
...@@ -231,7 +231,7 @@ def webhook(request: HttpRequest) -> HttpResponse: ...@@ -231,7 +231,7 @@ def webhook(request: HttpRequest) -> HttpResponse:
""" """
# validate ip source # validate ip source
forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR').split(', ')[0] forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR').split(', ')[0]
# networks = requests.get('https://api.github.com/meta').json()['hooks'] # Fails if API rate limit exceeded # networks = httpx.get('https://api.github.com/meta').json()['hooks'] # Fails if API rate limit exceeded
networks = ['185.199.108.0/22', '140.82.112.0/20'] networks = ['185.199.108.0/22', '140.82.112.0/20']
if not any(ip_address(forwarded_for) in ip_network(net) for net in networks): if not any(ip_address(forwarded_for) in ip_network(net) for net in networks):
logger.warning('not from github IP') logger.warning('not from github IP')
......
import logging import logging
import requests import httpx
from django.core.management import call_command from django.core.management import call_command
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
...@@ -15,7 +15,7 @@ class Command(BaseCommand): ...@@ -15,7 +15,7 @@ class Command(BaseCommand):
def handle(self, *args, **options): def handle(self, *args, **options):
logger.info('updating licenses') logger.info('updating licenses')
for data in requests.get(LICENSES).json()['licenses']: for data in httpx.get(LICENSES).json()['licenses']:
License.objects.get_or_create(spdx_id=data['licenseId'], License.objects.get_or_create(spdx_id=data['licenseId'],
defaults={ defaults={
'name': data['name'], 'name': data['name'],
......
# Generated by Django 3.2 on 2021-04-29 15:02
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('rainboard', '0053_ferrum'),
]
operations = [
migrations.AddField(
model_name='robotpkg',
name='extended_target',
field=models.ManyToManyField(to='rainboard.Target'),
),
]
...@@ -12,7 +12,7 @@ from django.utils import timezone ...@@ -12,7 +12,7 @@ from django.utils import timezone
from django.utils.dateparse import parse_datetime from django.utils.dateparse import parse_datetime
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
import requests import httpx
import git import git
from autoslug import AutoSlugField from autoslug import AutoSlugField
...@@ -87,10 +87,10 @@ class Forge(Links, NamedModel): ...@@ -87,10 +87,10 @@ class Forge(Links, NamedModel):
def api_req(self, url='', name=None, page=1): def api_req(self, url='', name=None, page=1):
logger.debug(f'requesting api {self} {url}, page {page}') logger.debug(f'requesting api {self} {url}, page {page}')
try: try:
return requests.get(self.api_url() + url, {'page': page}, verify=self.verify, headers=self.headers()) return httpx.get(self.api_url() + url, {'page': page}, verify=self.verify, headers=self.headers())
except requests.exceptions.ConnectionError: except httpx.exceptions.ConnectionError:
logger.error(f'requesting api {self} {url}, page {page} - SECOND TRY') logger.error(f'requesting api {self} {url}, page {page} - SECOND TRY')
return requests.get(self.api_url() + url, {'page': page}, verify=self.verify, headers=self.headers()) return httpx.get(self.api_url() + url, {'page': page}, verify=self.verify, headers=self.headers())
def api_data(self, url=''): def api_data(self, url=''):
req = self.api_req(url) req = self.api_req(url)
...@@ -491,12 +491,12 @@ class Repo(TimeStampedModel): ...@@ -491,12 +491,12 @@ class Repo(TimeStampedModel):
def api_req(self, url='', name=None, page=1): def api_req(self, url='', name=None, page=1):
logger.debug(f'requesting api {self.forge} {self.namespace} {self} {url}, page {page}') logger.debug(f'requesting api {self.forge} {self.namespace} {self} {url}, page {page}')
try: try:
return requests.get(self.api_url() + url, {'page': page}, return httpx.get(self.api_url() + url, {'page': page},
verify=self.forge.verify, verify=self.forge.verify,
headers=self.forge.headers()) headers=self.forge.headers())
except requests.exceptions.ConnectionError: except httpx.exceptions.ConnectionError:
logger.error(f'requesting api {self.forge} {self.namespace} {self} {url}, page {page} - SECOND TRY') logger.error(f'requesting api {self.forge} {self.namespace} {self} {url}, page {page} - SECOND TRY')
return requests.get(self.api_url() + url, {'page': page}, return httpx.get(self.api_url() + url, {'page': page},
verify=self.forge.verify, verify=self.forge.verify,
headers=self.forge.headers()) headers=self.forge.headers())
...@@ -810,6 +810,8 @@ class Robotpkg(NamedModel): ...@@ -810,6 +810,8 @@ class Robotpkg(NamedModel):
same_py = models.BooleanField(default=True) same_py = models.BooleanField(default=True)
extended_target = models.ManyToManyField(Target)
def main_page(self): def main_page(self):
if self.category != 'wip': if self.category != 'wip':
return f'{RPKG_URL}/robotpkg/{self.category}/{self.name}' return f'{RPKG_URL}/robotpkg/{self.category}/{self.name}'
...@@ -821,7 +823,7 @@ class Robotpkg(NamedModel): ...@@ -821,7 +823,7 @@ class Robotpkg(NamedModel):
def update_images(self): def update_images(self):
py3s = [False, True] if self.name.startswith('py-') else [False] py3s = [False, True] if self.name.startswith('py-') else [False]
debugs = [False, True] debugs = [False, True]
for target in Target.objects.active(): for target in list(Target.objects.active()) + list(self.extended_target.all()):
for py3 in py3s: for py3 in py3s:
for debug in debugs: for debug in debugs:
if target.py2_available or py3 or not self.name.startswith('py-'): if target.py2_available or py3 or not self.name.startswith('py-'):
...@@ -954,7 +956,7 @@ class Image(models.Model): ...@@ -954,7 +956,7 @@ class Image(models.Model):
headers = {} headers = {}
if not self.robotpkg.project.public: if not self.robotpkg.project.public:
image_name = self.get_image_name().split('/', maxsplit=1)[1].split(':')[0] image_name = self.get_image_name().split('/', maxsplit=1)[1].split(':')[0]
token = requests.get(f'{self.robotpkg.project.main_forge.url}/jwt/auth', { token = httpx.get(f'{self.robotpkg.project.main_forge.url}/jwt/auth', {
'client_id': 'docker', 'client_id': 'docker',
'offline_token': True, 'offline_token': True,
'service': 'container_registry', 'service': 'container_registry',
...@@ -962,7 +964,7 @@ class Image(models.Model): ...@@ -962,7 +964,7 @@ class Image(models.Model):
}, },
auth=('gsaurel', self.robotpkg.project.main_forge.token)).json()['token'] auth=('gsaurel', self.robotpkg.project.main_forge.token)).json()['token']
headers['Authorization'] = f'Bearer {token}' headers['Authorization'] = f'Bearer {token}'
r = requests.get(self.get_image_url(), headers=headers) r = httpx.get(self.get_image_url(), headers=headers)
if r.status_code == 200: if r.status_code == 200:
self.image = r.json()['fsLayers'][0]['blobSum'].split(':')[1][:12] self.image = r.json()['fsLayers'][0]['blobSum'].split(':')[1][:12]
self.created = parse_datetime(json.loads(r.json()['history'][0]['v1Compatibility'])['created']) self.created = parse_datetime(json.loads(r.json()['history'][0]['v1Compatibility'])['created'])
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment