ListPackagesAction.run()   F
last analyzed

Complexity

Conditions 17

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
dl 0
loc 37
rs 2.7204
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like ListPackagesAction.run() 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
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
2
# contributor license agreements.  See the NOTICE file distributed with
3
# this work for additional pkg_information regarding copyright ownership.
4
# The ASF licenses this file to You under the Apache License, Version 2.0
5
# (the "License"); you may not use this file except in compliance with
6
# the License.  You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
16
import requests
17
import six.moves.http_client as http_client
18
19
from st2actions.runners import pythonrunner
20
21
__all__ = [
22
    'ListPackagesAction'
23
]
24
25
BASE_URL = 'https://%(api_token)s:@packagecloud.io/api/v1/repos/%(repo)s/packages.json'
26
MAX_PAGE_NUMBER = 100
27
28
29
class ListPackagesAction(pythonrunner.Action):
30
    def run(self, repo, package, distro_version, version, release, api_token, per_page=200):
31
        params = {'per_page': per_page}
32
        values = {'repo': repo, 'api_token': api_token}
33
        url = BASE_URL % values
34
35
        page = 1
36
        packages = []
37
38
        while page < MAX_PAGE_NUMBER:
39
            page_url = url + '?page=' + str(page)
40
            response = requests.get(url=page_url, params=params)
41
42
            if response.status_code != http_client.OK:
43
                raise Exception(response.text)
44
45
            packages += response.json()
46
47
            if len(packages) >= int(response.headers.get('Total', 0)):
48
                break
49
50
            page += 1
51
52
        if package:
53
            packages = [pkg_info for pkg_info in packages if pkg_info['name'] == package]
54
55
        if distro_version:
56
            packages = [
57
                pkg_info for pkg_info in packages if pkg_info['distro_version'] == distro_version
58
            ]
59
60
        if version:
61
            packages = [pkg_info for pkg_info in packages if pkg_info['version'] == version]
62
63
        if release:
64
            packages = [pkg_info for pkg_info in packages if pkg_info['release'] == release]
65
66
        return sorted(packages, key=lambda x: (x['version'], x['release']), reverse=True)
67