@@ -515,7 +515,7 @@ def proxy_client(monkeypatch):
515
515
reimplemented to not need a response or response-like object.
516
516
"""
517
517
518
- class Response :
518
+ class MockResponse :
519
519
"""Mock response class to satisfy some requirements of tests."""
520
520
521
521
# NOTE: The methods tested that rely on this class should be reimplemented
@@ -524,39 +524,68 @@ def __init__(self, status_code=200, json=None):
524
524
self .json = json or {}
525
525
self .status_code = status_code
526
526
self .content_type = (
527
- "application/json"
528
- if str (status_code )[0 ] not in ("4" , "5" )
529
- else "application/problem+json"
527
+ "application/json" if status_code < 400 else "application/problem+json"
530
528
)
531
529
532
- class _proxy :
530
+ class ProxyClient :
531
+ def _handle__get__stacks__id (self , path ):
532
+ revision_id = path .lstrip ("/stacks/" )
533
+ json_response = legacy_api_stacks .get (revision_id )
534
+ if isinstance (json_response , HttpResponse ):
535
+ # In some cases, an actual response object is returned.
536
+ return json_response
537
+ # In other cases, just the data is returned, and it should be
538
+ # mapped to a response.
539
+ return MockResponse (json = json .loads (json .dumps (json_response )))
540
+
541
+ def _handle__get__transplants__id (self , path ):
542
+ stack_revision_id = path .lstrip ("/transplants?stack_revision_id=" )
543
+ result = legacy_api_transplants .get_list (
544
+ stack_revision_id = stack_revision_id
545
+ )
546
+ if isinstance (result , tuple ):
547
+ # For these endpoints, some responses contain different status codes
548
+ # which are represented as the second item in a tuple.
549
+ json_response , status_code = result
550
+ return MockResponse (
551
+ json = json .loads (json .dumps (json_response )),
552
+ status_code = status_code ,
553
+ )
554
+ # In the rest of the cases, the returned result is a response object.
555
+ return result
556
+
557
+ def _handle__post__transplants__dryrun (self , ** kwargs ):
558
+ json_response = legacy_api_transplants .dryrun (kwargs ["json" ])
559
+ return MockResponse (json = json .loads (json .dumps (json_response )))
560
+
561
+ def _handle__post__transplants (self , path , ** kwargs ):
562
+ try :
563
+ json_response , status_code = legacy_api_transplants .post (kwargs ["json" ])
564
+ except ProblemException as e :
565
+ # Handle exceptions and pass along the status code to the response object.
566
+ if e .json_detail :
567
+ return MockResponse (json = e .json_detail , status_code = e .status_code )
568
+ return MockResponse (json = e .args , status_code = e .status_code )
569
+ except Exception :
570
+ # TODO: double check that this is a thing in legacy?
571
+ # Added this due to a validation error (test_transplant_wrong_landing_path_format)
572
+ return MockResponse (json = ["error" ], status_code = 400 )
573
+ return MockResponse (
574
+ json = json .loads (json .dumps (json_response )), status_code = status_code
575
+ )
576
+
577
+ def _handle__put__landing_jobs__id (self , path , ** kwargs ):
578
+ job_id = int (path .lstrip ("/landing_jobs/" ))
579
+ json_response = legacy_api_landing_jobs .put (job_id , kwargs ["json" ])
580
+ return MockResponse (json = json .loads (json .dumps (json_response )))
581
+
533
582
def get (self , path , * args , ** kwargs ):
534
583
"""Handle various get endpoints."""
535
584
if path .startswith ("/stacks/D" ):
536
- revision_id = path .lstrip ("/stacks/" )
537
- json_response = legacy_api_stacks .get (revision_id )
538
- if isinstance (json_response , HttpResponse ):
539
- # In some cases, an actual response object is returned.
540
- return json_response
541
- # In other cases, just the data is returned, and it should be
542
- # mapped to a response.
543
- return Response (json = json .loads (json .dumps (json_response )))
585
+ return self ._handle__get__stacks__id (path )
544
586
545
587
if path .startswith ("/transplants?" ):
546
- stack_revision_id = path .lstrip ("/transplants?stack_revision_id=" )
547
- result = legacy_api_transplants .get_list (
548
- stack_revision_id = stack_revision_id
549
- )
550
- if isinstance (result , tuple ):
551
- # For these endpoints, some responses contain different status codes
552
- # which are represented as the second item in a tuple.
553
- json_response , status_code = result
554
- return Response (
555
- json = json .loads (json .dumps (json_response )),
556
- status_code = status_code ,
557
- )
558
- # In the rest of the cases, the returned result is a response object.
559
- return result
588
+ return self ._handle__get__transplants__id (path )
560
589
561
590
def post (self , path , ** kwargs ):
562
591
"""Handle various post endpoints."""
@@ -565,26 +594,10 @@ def post(self, path, **kwargs):
565
594
monkeypatch .setattr ("lando.api.legacy.auth.request" , mock_request )
566
595
567
596
if path .startswith ("/transplants/dryrun" ):
568
- json_response = legacy_api_transplants .dryrun (kwargs ["json" ])
569
- return Response (json = json .loads (json .dumps (json_response )))
597
+ return self ._handle__post__transplants__dryrun (** kwargs )
570
598
571
599
if path == "/transplants" :
572
- try :
573
- json_response , status_code = legacy_api_transplants .post (
574
- kwargs ["json" ]
575
- )
576
- except ProblemException as e :
577
- # Handle exceptions and pass along the status code to the response object.
578
- if e .json_detail :
579
- return Response (json = e .json_detail , status_code = e .status_code )
580
- return Response (json = e .args , status_code = e .status_code )
581
- except Exception :
582
- # TODO: double check that this is a thing in legacy?
583
- # Added this due to a validation error (test_transplant_wrong_landing_path_format)
584
- return Response (json = ["error" ], status_code = 400 )
585
- return Response (
586
- json = json .loads (json .dumps (json_response )), status_code = status_code
587
- )
600
+ return self ._handle__post__transplants (path , ** kwargs )
588
601
589
602
def put (self , path , ** kwargs ):
590
603
"""Handle put endpoints."""
@@ -593,8 +606,6 @@ def put(self, path, **kwargs):
593
606
monkeypatch .setattr ("lando.api.legacy.auth.request" , mock_request )
594
607
595
608
if path .startswith ("/landing_jobs/" ):
596
- job_id = int (path .lstrip ("/landing_jobs/" ))
597
- json_response = legacy_api_landing_jobs .put (job_id , kwargs ["json" ])
598
- return Response (json = json .loads (json .dumps (json_response )))
609
+ return self ._handle__put__landing_jobs__id (path , ** kwargs )
599
610
600
- return _proxy ()
611
+ return ProxyClient ()
0 commit comments