Conditions | 15 |
Total Lines | 70 |
Code Lines | 44 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like libs.analytics.Analytics.analyze() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | import ipaddress |
||
118 | async def analyze(self, data: dict): |
||
119 | """ |
||
120 | Do analysis from URL sent by message with databases |
||
121 | |||
122 | :param data: dict from message decoded |
||
123 | :return: dict to response |
||
124 | """ |
||
125 | url = url_normalize(data.get("url")) |
||
126 | url_hash = sha256(url.encode("utf-8")).hexdigest() |
||
127 | |||
128 | try: |
||
129 | response = requests.get(url) |
||
130 | except requests.exceptions.ConnectionError as e: |
||
131 | return { |
||
132 | "status": 403, |
||
133 | "reason": str(e) |
||
134 | } |
||
135 | |||
136 | if response.status_code != 200: |
||
137 | return { |
||
138 | "status": 404, |
||
139 | "http_code": response.status_code |
||
140 | } |
||
141 | |||
142 | if "text/html" not in response.headers["content-type"]: |
||
143 | return { |
||
144 | "status": 405 |
||
145 | } |
||
146 | |||
147 | url = response.url |
||
148 | |||
149 | host = urlparse(url).hostname if urlparse( |
||
150 | url).hostname != "localhost" else "127.0.0.1" |
||
151 | if (validators.ipv4(host) or validators.ipv6(host)) and ipaddress.ip_address(host).is_private: |
||
152 | return { |
||
153 | "status": 403, |
||
154 | "reason": "forbidden" |
||
155 | } |
||
156 | |||
157 | cache = self.data_control.find_result_cache_by_url_hash(url_hash) |
||
158 | |||
159 | if cache is not None: |
||
160 | score = cache |
||
161 | |||
162 | elif self.data_control.check_trustlist(url): |
||
163 | score = 1 |
||
164 | |||
165 | elif self.data_control.check_trust_domain(host): |
||
166 | score = 1 |
||
167 | |||
168 | elif self.data_control.check_blacklist(url): |
||
169 | score = 0 |
||
170 | |||
171 | elif self.data_control.check_warnlist(url): |
||
172 | score = 0.5 |
||
173 | |||
174 | elif self.safe_browsing.lookup([url]): |
||
175 | score = 0 |
||
176 | self.data_control.mark_as_blacklist(url) |
||
177 | |||
178 | else: |
||
179 | score = await self._deep_analyze(url) |
||
180 | |||
181 | if cache is None: |
||
182 | self.data_control.upload_result_cache(url_hash, score) |
||
183 | |||
184 | return { |
||
185 | "status": 200, |
||
186 | "url": url, |
||
187 | "trust_score": score |
||
188 | } |
||
231 |