summaryrefslogtreecommitdiffstats
path: root/testapi/testapi-client/testapiclient/httpClient.py
blob: f8683b9d83b1bdbfeaaefcad6719139866293518 (plain)
1
2
3
4
5
6
7
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
import json

import requests


class HTTPClient():

    __instance = None
    headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}

    @staticmethod
    def get_Instance():
        """ Static access method. """
        if HTTPClient.__instance is None:
            HTTPClient()
        return HTTPClient.__instance

    def __init__(self):
        """ Virtually private constructor. """
        if HTTPClient.__instance is not None:
            raise Exception("This class is a singleton!")
        else:
            HTTPClient.__instance = self

    def get(self, url):
        r = requests.get(url)
        if r.status_code == 200:
            return r.json()
        else:
            return r.text

    def post(self, url, session, data):
        r = session.post(url,
                         data=json.dumps(data),
                         headers=HTTPClient.headers)
        return r

    def put(self, url, session, data):
        r = session.put(url,
                        data=json.dumps(data),
                        headers=HTTPClient.headers)
        return r.text

    def delete(self, url, session, *args):
        if(args.__len__ > 0):
            r = session.delete(url,
                               data=json.dumps(args[0]),
                               headers=HTTPClient.headers)
        else:
            r = session.delete(url)
        return r.text