Skip to content

Api request

Class for connecting to trading server.

Basic function of fetching data from API server.

Source code in onequant/api/request.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class ApiRequest:
    """Class for connecting to trading server.

    Basic function of fetching data from API server.
    """

    def __init__(self, url):
        """Initializes the ApiRequest class with a given url.

        Args:
            url (str): The url to be used for the API request.
        """
        self.url = url
        self.token = None
        self.headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 \
            (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
        }

    def login(self, username, password):
        """Logs in to the API with the given username and password.

        Args:
            username (str): The username to be used for the login.
            password (str): The password to be used for the login.

        Returns:
            str: The token for the logged in user.
        """
        data = {'username': username, 'password': password, 'autoLogin': False, 'type': 'pc'}

        response = requests.post(url=self.url + '/system/login/login', json=data, headers=self.headers)
        if 'Set-Cookie' in response.headers:
            import re

            match = re.search(r'satoken=([\w-]+);', response.headers['Set-Cookie'])
            if match:
                self.token = 'satoken=' + match.group(1)
                self.headers['Cookie'] = self.token

        return self.token

    def request(self, method, router, params=None, data=None, json=None):
        """Sends a request to the API with the given parameters.

        Args:
            method (str): The HTTP method to be used for the request.
            router (str): The router to be used for the request.
            params (dict, optional): The parameters to be used for the request. Defaults to None.
            data (dict, optional): The data to be used for the request. Defaults to None.
            json (dict, optional): The json to be used for the request. Defaults to None.

        Raises:
            AssertionError: If an unsupported request method is used.

        Returns:
            dict: The response from the API in json format.
        """
        assert method in ['get', 'post', 'put', 'delete'], 'Unsupported request method'

        response = requests.request(
            method=method, url=self.url + router, params=params, data=data, json=json, headers=self.headers
        )

        return response.json()

__init__(url)

Initializes the ApiRequest class with a given url.

Parameters:

Name Type Description Default
url str

The url to be used for the API request.

required
Source code in onequant/api/request.py
14
15
16
17
18
19
20
21
22
23
24
25
def __init__(self, url):
    """Initializes the ApiRequest class with a given url.

    Args:
        url (str): The url to be used for the API request.
    """
    self.url = url
    self.token = None
    self.headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 \
        (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
    }

login(username, password)

Logs in to the API with the given username and password.

Parameters:

Name Type Description Default
username str

The username to be used for the login.

required
password str

The password to be used for the login.

required

Returns:

Name Type Description
str

The token for the logged in user.

Source code in onequant/api/request.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
def login(self, username, password):
    """Logs in to the API with the given username and password.

    Args:
        username (str): The username to be used for the login.
        password (str): The password to be used for the login.

    Returns:
        str: The token for the logged in user.
    """
    data = {'username': username, 'password': password, 'autoLogin': False, 'type': 'pc'}

    response = requests.post(url=self.url + '/system/login/login', json=data, headers=self.headers)
    if 'Set-Cookie' in response.headers:
        import re

        match = re.search(r'satoken=([\w-]+);', response.headers['Set-Cookie'])
        if match:
            self.token = 'satoken=' + match.group(1)
            self.headers['Cookie'] = self.token

    return self.token

request(method, router, params=None, data=None, json=None)

Sends a request to the API with the given parameters.

Parameters:

Name Type Description Default
method str

The HTTP method to be used for the request.

required
router str

The router to be used for the request.

required
params dict

The parameters to be used for the request. Defaults to None.

None
data dict

The data to be used for the request. Defaults to None.

None
json dict

The json to be used for the request. Defaults to None.

None

Raises:

Type Description
AssertionError

If an unsupported request method is used.

Returns:

Name Type Description
dict

The response from the API in json format.

Source code in onequant/api/request.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
def request(self, method, router, params=None, data=None, json=None):
    """Sends a request to the API with the given parameters.

    Args:
        method (str): The HTTP method to be used for the request.
        router (str): The router to be used for the request.
        params (dict, optional): The parameters to be used for the request. Defaults to None.
        data (dict, optional): The data to be used for the request. Defaults to None.
        json (dict, optional): The json to be used for the request. Defaults to None.

    Raises:
        AssertionError: If an unsupported request method is used.

    Returns:
        dict: The response from the API in json format.
    """
    assert method in ['get', 'post', 'put', 'delete'], 'Unsupported request method'

    response = requests.request(
        method=method, url=self.url + router, params=params, data=data, json=json, headers=self.headers
    )

    return response.json()