rest_utils.py 1.53 KB
Newer Older
liuzhe-lz's avatar
liuzhe-lz committed
1
2
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.
3
4
5
6
7
8
9
10

import requests

def rest_get(url, timeout):
    '''Call rest get method'''
    try:
        response = requests.get(url, timeout=timeout)
        return response
11
12
    except Exception as e:
        print('Get exception {0} when sending http get to url {1}'.format(str(e), url))
13
14
        return None

15
def rest_post(url, data, timeout, rethrow_exception=False):
16
17
18
19
20
    '''Call rest post method'''
    try:
        response = requests.post(url, headers={'Accept': 'application/json', 'Content-Type': 'application/json'},\
                                 data=data, timeout=timeout)
        return response
21
    except Exception as e:
22
23
        if rethrow_exception is True:
            raise
24
        print('Get exception {0} when sending http post to url {1}'.format(str(e), url))
25
26
27
28
29
30
31
32
        return None

def rest_put(url, data, timeout):
    '''Call rest put method'''
    try:
        response = requests.put(url, headers={'Accept': 'application/json', 'Content-Type': 'application/json'},\
                                data=data, timeout=timeout)
        return response
33
34
    except Exception as e:
        print('Get exception {0} when sending http put to url {1}'.format(str(e), url))
35
36
37
38
39
40
41
        return None

def rest_delete(url, timeout):
    '''Call rest delete method'''
    try:
        response = requests.delete(url, timeout=timeout)
        return response
42
43
    except Exception as e:
        print('Get exception {0} when sending http delete to url {1}'.format(str(e), url))
44
        return None