import json
import requests


class api_fucntion:
    def __init__(self):
        self.url = 'http://localhost:5001'
        self.headers = {'Content-Type': 'application/json'}
        #self.headers_with_token = {'Content-Type': 'application/x-www-form-urlencoded', 'Authorization': ''}
        self.access_token = None
        self.refresh_token = None

    def headers_with_token(self, token):
        return {'Content-Type': 'application/json', 'Authorization': token}

    def get_access_token(self, username, password):
        data = {'username': username, 'password': password}
        response = requests.post(self.url + '/loginTable', headers=self.headers, data=json.dumps(data))
        #print(response.json())
        if response.status_code == 200:
            self.access_token = 'Bearer ' + response.json()['access_token']
            self.refresh_token = 'Bearer ' + response.json()['refresh_token']
        return response.json()['data']

    def refresh_access_token(self):
        response = requests.post(self.url + '/refresh', headers=self.headers_with_token(self.refresh_token))
        self.access_token = 'Bearer '+response.json()['access_token']
        if response.status_code == 200:
            #print('Successfully refreshed access token')
            return True
        else:
            #print('Failed to refresh access token')
            return False

    def request_get(self, uri, **kwargs):
        response = requests.get(self.url + uri, headers=self.headers_with_token(self.access_token))
        data = response.json()
        print(type(data))
        if response.status_code == 401 and data['msg'] == 'Token has expired':
            if self.refresh_access_token():
                response = requests.get(self.url + uri, headers=self.headers_with_token(self.access_token))
                data = response.json()
            else:
                data['msg'] = 'Failed to refresh access token'
        return data

    def request_post(self, uri, **kwargs):
        response = requests.post(self.url + uri, headers=self.headers_with_token(self.access_token), data=json.dumps(kwargs))
        data = response.json()
        if response.status_code == 401 and data['msg'] == 'Token has expired':
            if self.refresh_access_token():
                response = requests.post(self.url + uri, headers=self.headers_with_token(self.access_token), data=json.dumps(kwargs))
                data = response.json()
            else:
                data['msg'] = 'Failed to refresh access token'
        return data

    def request_get_without_token(self, uri):
        response = requests.get(self.url + uri, headers=self.headers)
        data = response.json()
        if response.status_code == 200:
            return data
        else:
            data['msg'] = 'Failed to get data'
            return data

    def request_post_without_token(self, uri, **kwargs):
        response = requests.post(self.url + uri, headers=self.headers, data=json.dumps(kwargs))
        data = response.json()
        if response.status_code == 200:
            return data
        else:
            data['msg'] = 'Failed to post data'
            return data

api_fc = api_fucntion()



