Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/stripe/api_resources/abstract/custom_method.py: 62%

29 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

1from __future__ import absolute_import, division, print_function 

2 

3from stripe import util 

4from stripe.six.moves.urllib.parse import quote_plus 

5 

6 

7def custom_method(name, http_verb, http_path=None, is_streaming=False): 

8 if http_verb not in ["get", "post", "delete"]: 8 ↛ 9line 8 didn't jump to line 9, because the condition on line 8 was never true

9 raise ValueError( 

10 "Invalid http_verb: %s. Must be one of 'get', 'post' or 'delete'" 

11 % http_verb 

12 ) 

13 if http_path is None: 

14 http_path = name 

15 

16 def wrapper(cls): 

17 def custom_method_request(cls, sid, **params): 

18 url = "%s/%s/%s" % ( 

19 cls.class_url(), 

20 quote_plus(util.utf8(sid)), 

21 http_path, 

22 ) 

23 obj = cls._static_request(http_verb, url, **params) 

24 

25 # For list objects, we have to attach the parameters so that they 

26 # can be referenced in auto-pagination and ensure consistency. 

27 if "object" in obj and obj.object == "list": 

28 obj._retrieve_params = params 

29 

30 return obj 

31 

32 def custom_method_request_stream(cls, sid, **params): 

33 url = "%s/%s/%s" % ( 

34 cls.class_url(), 

35 quote_plus(util.utf8(sid)), 

36 http_path, 

37 ) 

38 return cls._static_request_stream(http_verb, url, **params) 

39 

40 if is_streaming: 40 ↛ 41line 40 didn't jump to line 41, because the condition on line 40 was never true

41 class_method_impl = classmethod(custom_method_request_stream) 

42 else: 

43 class_method_impl = classmethod(custom_method_request) 

44 

45 existing_method = getattr(cls, name, None) 

46 if existing_method is None: 46 ↛ 47line 46 didn't jump to line 47, because the condition on line 46 was never true

47 setattr(cls, name, class_method_impl) 

48 else: 

49 # If a method with the same name we want to use already exists on 

50 # the class, we assume it's an instance method. In this case, the 

51 # new class method is prefixed with `_cls_`, and the original 

52 # instance method is decorated with `util.class_method_variant` so 

53 # that the new class method is called when the original method is 

54 # called as a class method. 

55 setattr(cls, "_cls_" + name, class_method_impl) 

56 instance_method = util.class_method_variant("_cls_" + name)( 

57 existing_method 

58 ) 

59 setattr(cls, name, instance_method) 

60 

61 return cls 

62 

63 return wrapper