SandCage.list_files_service()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
1
# -*- coding: utf-8 -*-
2
import requests
3
import certifi
4
import os
5
6
class SandCage:
7
    API_ROOT = 'https://api.sandcage.com/'
8
    API_VERSION = '0.2'
9
    TIMEOUT = 30
10
    TRUSTED = certifi.where()
11
    
12
    def __init__(self, api_key=None):
13
        """Give your SandCage API Key as a constructor parameter
14
        This can be retrieved from https://www.sandcage.com/panel/api_key
15
        """
16
        
17
        self.api_key = api_key
18
        self.sandcage_api_endpoint = '{0}/{1}/'.format(self.API_ROOT,
19
                                                       self.API_VERSION)
20
        self.user_agent = '{0} - {1}'.format('SandCage', self.API_VERSION)
21
22
    def _get_post_data(self, payload=None):
23
        """Combine payload with API KEY and return the dictionary."""
24
        
25
        d = {'key': self.api_key}
26
        if payload:
27
            d.update(payload)
28
        return d
29
30
    def _update_callback(self, data, callback_endpoint=None):
31
        """Update data with callback_endpoint address."""
32
        
33
        if callback_endpoint:
34
            data.update({'callback_url': callback_endpoint})
35
36
    def call_endpoint(self, endpoint, post=False, data=None):
37
        """The call_endpoint method
38
39
        * This method sends http request to the endpoint
40
41
        * First parameter "endpoint" (string) is the url of the endpoint.
42
        * Second parameter "post" (boolean) indicates the http method type.
43
          True => POST
44
          False => GET
45
        * Third parameter "data" (dictionary) is the data to be sent.
46
        * Method's return type is Response object.
47
        """
48
        
49
        headers = {'User-Agent': self.user_agent}
50
        if post:
51
            r = requests.post(endpoint, headers=headers, timeout=self.TIMEOUT,
52
                              json=data, verify=self.TRUSTED)
53
        else:
54
            r = requests.get(endpoint, headers=headers, timeout=self.TIMEOUT,
55
                             verify=self.TRUSTED)
56
        return r
57
58
    def get_info_service(self, payload=None):
59
        """The "get-info" service
60
        
61
        * First parameter "payload" (dictionary) contains the data to be sent.
62
        * Method's return type is Response object.
63
        """
64
        return self.call_endpoint(self.sandcage_api_endpoint + 'get-info', post=True,
65
                         data=self._get_post_data(payload))
66
67
    def list_files_service(self, payload=None):
68
        """The "list-files" service
69
        
70
        * First parameter "payload" (dictionary) contains the data to be sent.
71
        * Method's return type is Response object.
72
        """
73
        return self.call_endpoint(self.sandcage_api_endpoint + 'list-files', post=True,
74
                         data=self._get_post_data(payload))
75
76
    def destroy_files_service(self, payload=None, callback_endpoint=None):
77
        """The "destroy-files" service
78
        
79
        * First parameter "payload" (dictionary) contains the data to be sent.
80
        * Second parameter "callback_endpoint" (string) is the url, where the 
81
          callback should be send to.
82
        * Method's return type is Response object.
83
        """
84
85
        data = self._get_post_data(payload)
86
        self._update_callback(data, callback_endpoint)
87
        return self.call_endpoint(self.sandcage_api_endpoint + 'destroy-files', post=True,
88
                         data=data)
89
90
    def schedule_files_service(self, payload=None, callback_endpoint=None):
91
        """The "schedule-tasks" service
92
93
        * First parameter "payload" (dictionary) contains the data to be sent.
94
        * Second parameter "callback_endpoint" (string) is the url, where the 
95
          callback should be send to.
96
        * Method's return type is Response object.
97
        """
98
         
99
        data = self._get_post_data(payload)
100
        self._update_callback(data, callback_endpoint)
101
        return self.call_endpoint(self.sandcage_api_endpoint + 'schedule-tasks',
102
                         post=True, data=data)
103
104