13
13
from celery import Task
14
14
from django .conf import settings
15
15
from django .utils import timezone
16
+ from slumber import API
16
17
from slumber .exceptions import HttpClientError
17
18
18
- from readthedocs .api .v2 .client import api as api_v2
19
+ from readthedocs .api .v2 .client import setup_api
19
20
from readthedocs .builds import tasks as build_tasks
20
21
from readthedocs .builds .constants import (
21
22
ARTIFACT_TYPES ,
@@ -102,6 +103,9 @@ class TaskData:
102
103
build_pk : int = None
103
104
build_commit : str = None
104
105
106
+ # Slumber client to interact with the API v2.
107
+ api_client : API = None
108
+
105
109
start_time : timezone .datetime = None
106
110
# pylint: disable=unsubscriptable-object
107
111
environment_class : type [DockerBuildEnvironment ] | type [LocalBuildEnvironment ] = None
@@ -153,6 +157,8 @@ def before_start(self, task_id, args, kwargs):
153
157
# argument
154
158
self .data .version_pk = args [0 ]
155
159
160
+ self .data .api_client = setup_api ()
161
+
156
162
# load all data from the API required for the build
157
163
self .data .version = self .get_version (self .data .version_pk )
158
164
self .data .project = self .data .version .project
@@ -339,8 +345,10 @@ def sigint_received(*args, **kwargs):
339
345
340
346
def _check_concurrency_limit (self ):
341
347
try :
342
- response = api_v2 .build .concurrent .get (project__slug = self .data .project .slug )
343
- concurrency_limit_reached = response .get ('limit_reached' , False )
348
+ response = self .data .api_client .build .concurrent .get (
349
+ project__slug = self .data .project .slug
350
+ )
351
+ concurrency_limit_reached = response .get ("limit_reached" , False )
344
352
max_concurrent_builds = response .get (
345
353
'max_concurrent' ,
346
354
settings .RTD_MAX_CONCURRENT_BUILDS ,
@@ -390,6 +398,8 @@ def before_start(self, task_id, args, kwargs):
390
398
# anymore and we are not using it
391
399
self .data .environment_class = LocalBuildEnvironment
392
400
401
+ self .data .api_client = setup_api ()
402
+
393
403
self .data .build = self .get_build (self .data .build_pk )
394
404
self .data .version = self .get_version (self .data .version_pk )
395
405
self .data .project = self .data .version .project
@@ -427,7 +437,7 @@ def _reset_build(self):
427
437
# Reset build only if it has some commands already.
428
438
if self .data .build .get ("commands" ):
429
439
log .info ("Resetting build." )
430
- api_v2 .build (self .data .build ["id" ]).reset .post ()
440
+ self . data . api_client .build (self .data .build ["id" ]).reset .post ()
431
441
432
442
def on_failure (self , exc , task_id , args , kwargs , einfo ):
433
443
"""
@@ -598,7 +608,7 @@ def on_success(self, retval, task_id, args, kwargs):
598
608
# TODO: remove this condition and *always* update the DB Version instance
599
609
if "html" in valid_artifacts :
600
610
try :
601
- api_v2 .version (self .data .version .pk ).patch (
611
+ self . data . api_client .version (self .data .version .pk ).patch (
602
612
{
603
613
"built" : True ,
604
614
"documentation_type" : self .data .version .documentation_type ,
@@ -709,7 +719,7 @@ def update_build(self, state=None):
709
719
# self.data.build[key] = val.decode('utf-8', 'ignore')
710
720
711
721
try :
712
- api_v2 . build (self .data .build ['id' ]).patch (self .data .build )
722
+ self . data . api_client . build (self .data .build ["id" ]).patch (self .data .build )
713
723
except Exception :
714
724
# NOTE: we are updating the "Build" object on each `state`.
715
725
# Only if the last update fails, there may be some inconsistency
@@ -802,22 +812,15 @@ def save_build_data(self):
802
812
except Exception :
803
813
log .exception ("Error while saving build data" )
804
814
805
- @staticmethod
806
- def get_project (project_pk ):
807
- """Get project from API."""
808
- project_data = api_v2 .project (project_pk ).get ()
809
- return APIProject (** project_data )
810
-
811
- @staticmethod
812
- def get_build (build_pk ):
815
+ def get_build (self , build_pk ):
813
816
"""
814
817
Retrieve build object from API.
815
818
816
819
:param build_pk: Build primary key
817
820
"""
818
821
build = {}
819
822
if build_pk :
820
- build = api_v2 .build (build_pk ).get ()
823
+ build = self . data . api_client .build (build_pk ).get ()
821
824
private_keys = [
822
825
'project' ,
823
826
'version' ,
@@ -834,7 +837,7 @@ def get_build(build_pk):
834
837
# build has finished to reduce API calls.
835
838
def set_valid_clone (self ):
836
839
"""Mark on the project that it has been cloned properly."""
837
- api_v2 .project (self .data .project .pk ).patch (
840
+ self . data . api_client .project (self .data .project .pk ).patch (
838
841
{'has_valid_clone' : True }
839
842
)
840
843
self .data .project .has_valid_clone = True
0 commit comments