rest_utils.py 2.41 KB
Newer Older
liuzhe-lz's avatar
liuzhe-lz committed
1
2
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.
Deshui Yu's avatar
Deshui Yu committed
3
4
5
6

import time
import requests
from .url_utils import check_status_url
7
from .constants import REST_TIME_OUT
8
from .common_utils import print_error
Deshui Yu's avatar
Deshui Yu committed
9

10
def rest_put(url, data, timeout, show_error=False):
Deshui Yu's avatar
Deshui Yu committed
11
12
13
14
15
    '''Call rest put method'''
    try:
        response = requests.put(url, headers={'Accept': 'application/json', 'Content-Type': 'application/json'},\
                                data=data, timeout=timeout)
        return response
16
17
18
    except Exception as exception:
        if show_error:
            print_error(exception)
Deshui Yu's avatar
Deshui Yu committed
19
20
        return None

21
def rest_post(url, data, timeout, show_error=False):
Deshui Yu's avatar
Deshui Yu committed
22
23
24
25
26
    '''Call rest post method'''
    try:
        response = requests.post(url, headers={'Accept': 'application/json', 'Content-Type': 'application/json'},\
                                 data=data, timeout=timeout)
        return response
27
28
29
    except Exception as exception:
        if show_error:
            print_error(exception)
Deshui Yu's avatar
Deshui Yu committed
30
31
        return None

32
def rest_get(url, timeout, show_error=False):
Deshui Yu's avatar
Deshui Yu committed
33
34
35
36
    '''Call rest get method'''
    try:
        response = requests.get(url, timeout=timeout)
        return response
37
38
39
    except Exception as exception:
        if show_error:
            print_error(exception)
Deshui Yu's avatar
Deshui Yu committed
40
41
        return None

42
def rest_delete(url, timeout, show_error=False):
Deshui Yu's avatar
Deshui Yu committed
43
44
45
46
    '''Call rest delete method'''
    try:
        response = requests.delete(url, timeout=timeout)
        return response
47
48
49
    except Exception as exception:
        if show_error:
            print_error(exception)
Deshui Yu's avatar
Deshui Yu committed
50
51
52
53
54
55
        return None

def check_rest_server(rest_port):
    '''Check if restful server is ready'''
    retry_count = 5
    for _ in range(retry_count):
56
        response = rest_get(check_status_url(rest_port), REST_TIME_OUT)
Deshui Yu's avatar
Deshui Yu committed
57
58
        if response:
            if response.status_code == 200:
59
                return True, response
Deshui Yu's avatar
Deshui Yu committed
60
            else:
61
                return False, response
Deshui Yu's avatar
Deshui Yu committed
62
63
        else:
            time.sleep(3)
64
    return  False, response
Deshui Yu's avatar
Deshui Yu committed
65
66
67
68

def check_rest_server_quick(rest_port):
    '''Check if restful server is ready, only check once'''
    response = rest_get(check_status_url(rest_port), 5)
69
70
71
72
73
74
    if response and response.status_code == 200:
        return True, response
    return False, None

def check_response(response):
    '''Check if a response is success according to status_code'''
Deshui Yu's avatar
Deshui Yu committed
75
76
77
    if response and response.status_code == 200:
        return True
    return False