|
8 | 8 | except ImportError: |
9 | 9 | import json |
10 | 10 |
|
11 | | -ABS_IMPORT_PREFIX = 'pygithub3.requests' |
| 11 | +from pygithub3.exceptions import DoesNotExists, UriInvalid, ValidationError |
| 12 | +from pygithub3.resources.base import Raw |
12 | 13 |
|
| 14 | +ABS_IMPORT_PREFIX = 'pygithub3.requests' |
13 | 15 |
|
14 | | -class RequestNotFound(Exception): |
15 | | - pass |
16 | 16 |
|
| 17 | +class Body(object): |
17 | 18 |
|
18 | | -class RequestUriInvalid(Exception): |
19 | | - pass |
| 19 | + def __init__(self, content, schema): |
| 20 | + self.content = content |
| 21 | + self.schema = schema |
20 | 22 |
|
| 23 | + def dumps(self): |
| 24 | + if not self.content: |
| 25 | + return None |
| 26 | + return json.dumps(self.parse()) |
21 | 27 |
|
22 | | -class RequestValidationError(Exception): |
23 | | - pass |
| 28 | + def parse(self): |
| 29 | + if not self.schema: |
| 30 | + return self.content |
| 31 | + if not hasattr(self.content, 'items'): |
| 32 | + raise ValidationError("'%s' needs a content dictionary" |
| 33 | + % self.__class__.__name__) |
| 34 | + return {key: self.content[key] for key in self.schema |
| 35 | + if key in self.content} |
24 | 36 |
|
25 | 37 |
|
26 | 38 | class Request(object): |
27 | 39 | """ """ |
28 | 40 |
|
| 41 | + uri = '' |
| 42 | + resource = Raw |
| 43 | + body_schema = () |
| 44 | + |
29 | 45 | def __init__(self, args): |
30 | 46 | """ """ |
| 47 | + self.body = args.pop('body', None) |
31 | 48 | self.args = args |
32 | | - self.validate() |
33 | | - self.uri = self.set_uri() |
| 49 | + self.clean() |
34 | 50 |
|
35 | | - def validate(self): |
36 | | - raise NotImplementedError |
| 51 | + def clean(self): |
| 52 | + self.uri = self.clean_uri() or self.uri |
| 53 | + self.body = Body(self.clean_body(), self.body_schema) |
37 | 54 |
|
38 | | - def set_uri(self): |
39 | | - raise NotImplementedError |
| 55 | + def clean_body(self): |
| 56 | + return self.body |
40 | 57 |
|
41 | | - def get_data(self): |
42 | | - raise NotImplementedError |
| 58 | + def clean_uri(self): |
| 59 | + return None |
43 | 60 |
|
44 | | - def get_uri(self): |
45 | | - return str(self.uri).strip('/') |
46 | | - |
47 | | - def get_resource(self): |
48 | | - return getattr(self, 'resource', '') |
| 61 | + @property |
| 62 | + def resource(self): |
| 63 | + return self.resource |
49 | 64 |
|
50 | 65 | def __getattr__(self, name): |
51 | 66 | return self.args.get(name) |
52 | 67 |
|
53 | 68 | def __str__(self): |
54 | | - return self.get_uri() |
| 69 | + return self.populate_uri() |
| 70 | + |
| 71 | + def populate_uri(self): |
| 72 | + try: |
| 73 | + populated_uri = self.uri.format(**self.args) |
| 74 | + except KeyError: |
| 75 | + raise ValidationError( |
| 76 | + "'%s' request wasn't be able to populate the uri '%s' with " |
| 77 | + "'%s' args" % (self.__class__.__name__, self.uri, self.args)) |
| 78 | + return str(populated_uri).strip('/') |
55 | 79 |
|
56 | | - def _parse_simple_dict(self, to_parse): |
57 | | - if not hasattr(to_parse, 'items'): |
58 | | - raise RequestValidationError("'%s' needs a data dictionary" |
59 | | - % self.__class__.__name__) |
60 | | - update_params = { |
61 | | - valid_key: to_parse[valid_key] |
62 | | - for valid_key in self.valid |
63 | | - if valid_key in to_parse} |
64 | | - return update_params |
| 80 | + def get_body(self): |
| 81 | + return self.body.dumps() |
65 | 82 |
|
66 | 83 |
|
67 | 84 | class Factory(object): |
68 | 85 | """ """ |
69 | 86 |
|
70 | 87 | import_pattern = re.compile(r'^(\w+\.)+\w+$') |
71 | 88 |
|
72 | | - def __init__(self): |
73 | | - """ """ |
74 | | - self.args = {} |
75 | | - |
76 | | - def config_with(self, **kwargs): |
77 | | - self.args = kwargs |
78 | | - |
79 | | - def clear_config(self): |
80 | | - self.args = {} |
81 | | - |
82 | 89 | def __validate(func): |
83 | 90 | """ """ |
84 | 91 |
|
85 | | - def wrapper(self, request_uri): |
| 92 | + def wrapper(self, request_uri, **kwargs): |
86 | 93 | if not Factory.import_pattern.match(request_uri): |
87 | | - raise RequestUriInvalid("'%s' isn't valid form" % request_uri) |
88 | | - return func(self, request_uri.lower()) |
| 94 | + raise UriInvalid("'%s' isn't valid form" % request_uri) |
| 95 | + return func(self, request_uri.lower(), **kwargs) |
89 | 96 | return wrapper |
90 | 97 |
|
91 | 98 | def __dispatch(func): |
92 | 99 | """ """ |
93 | 100 |
|
94 | | - def wrapper(self, request_uri): |
| 101 | + def wrapper(self, request_uri, **kwargs): |
95 | 102 | module_chunk, s, request_chunk = request_uri.rpartition('.') |
96 | 103 | try: |
97 | 104 | # TODO: CamelCase and under_score support, now only Class Name |
98 | 105 | module = import_module('%s.%s' |
99 | 106 | % (ABS_IMPORT_PREFIX, module_chunk)) |
100 | 107 | request = getattr(module, request_chunk.capitalize()) |
101 | 108 | except ImportError: |
102 | | - raise RequestNotFound("'%s' module does not exists" |
| 109 | + raise DoesNotExists("'%s' module does not exists" |
103 | 110 | % module_chunk) |
104 | 111 | except AttributeError: |
105 | | - raise RequestNotFound( |
| 112 | + raise DoesNotExists( |
106 | 113 | "'%s' request doesn't exists into '%s' module" |
107 | 114 | % (request_chunk.capitalize(), module_chunk)) |
108 | | - return func(self, request) |
| 115 | + return func(self, request, **kwargs) |
109 | 116 | return wrapper |
110 | 117 |
|
111 | 118 | @__validate |
112 | 119 | @__dispatch |
113 | | - def __call__(self, request=''): |
114 | | - request = request(self.args) |
115 | | - self.clear_config() |
| 120 | + def __call__(self, request='', **kwargs): |
| 121 | + request = request(kwargs) |
116 | 122 | assert isinstance(request, Request) |
117 | 123 | return request |
0 commit comments