Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/suporte dotenv #1

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 91 additions & 58 deletions BlingV3.py
Original file line number Diff line number Diff line change
@@ -1,125 +1,152 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Set 4 2023.

@author: MatheusBruno
"""
from dotenv import dotenv_values

import datetime
import requests
import base64
import os
import datetime
from time import sleep


class BlingV3():
"""Facilitate and obtain the Bling API V3 Access Token."""

def parmentHeader(self, path : str):
def parmentHeader(self, path: str = None):
"""
Enter the path of the txt file with the client_id and client_secret credentials
Enter the path of the txt file with the client_id and client_secret.

Example:
-------
client_id:sequence of numbers,
client_secret:sequence of numbers

:Usage:
::
Bling().parmentHeader('/home/user/document/credential.txt')
or
Bling().parmentHeader() whit .env
"""
global header

credential = None
if path:
credential = None

with open(path, 'r') as file:
credential = file.read()
file.close()
with open(path, 'r') as file:
credential = file.read()
file.close()

listCredential = credential.split(',')
listCredential = credential.split(',')

listCredential[0] = listCredential[0].replace("client_id:", "")
listCredential[1] = listCredential[1].replace(
"\nclient_secret:", ""
)
else:
listCredential = [
dotenv_values()["BLING_CLIENT_ID"],
dotenv_values()["BLING_CLIENT_SECRET"]
]

listCredential[0] = listCredential[0].replace("client_id:","")
listCredential[1] = listCredential[1].replace("\nclient_secret:","")

credentialbs4 = f"{listCredential[0]}:{listCredential[1]}"
credentialbs4 = credentialbs4.encode('ascii')
credentialbs4 = base64.b64encode(credentialbs4)
credentialbs4 = bytes.decode(credentialbs4)

header = {
'Accept':'1.0',
'Accept': '1.0',
'Authorization': f'Basic {credentialbs4}'
}

def paramentCode(self, code : str):
def paramentCode(self, code: str):
"""
provide url code.
Provide url code.

:Usage:
::
Bling().paramentCode("8337d4fd498508b9225b695f3bdf0ad086fb8bcc")
"""

global dice
dice = {
'grant_type':'authorization_code',
'grant_type': 'authorization_code',
'code': code
}

def tokenApi(self):
"""
Return a list of objects containing the api data in case of right
[access_toke, expires_in, token_type, scope, refresh_token]
in case of any error
[error]
------------------------------------------------------------------
If successful, a file will be created with the credentials and time.
Return a list of objects containing the api data in case of right.

:Usage:
::
[access_toke, expires_in, token_type, scope, refresh_token]
in case of any error
[error]
------------------------------------------------------------------
If successful, a file will be created with the credentials and time.

:Usage:
::

obj = Bling().tokenApi()
obj = Bling().tokenApi()
"""
api = requests.post('https://www.bling.com.br/Api/v3/oauth/token', headers=header, json=dice)
api = requests.post(
'https://www.bling.com.br/Api/v3/oauth/token',
headers=header, json=dice)
situationStatusCode = api.status_code
print(api.status_code)
api = api.json()



if situationStatusCode == 400:
return api
else:
path = os.getcwd()
dateNow = datetime.date.today()
apiHoursNow = ((int(api['expires_in'])/60)/60)
systemHoursNow = datetime.datetime.now()
hoursExpiration = ( int(systemHoursNow.strftime("%H")) + apiHoursNow )

hoursExpiration = (
int(systemHoursNow.strftime("%H")) + apiHoursNow
)

if os.path.isdir(f"{path}/credential"):
pass
else:
os.mkdir(f'{path}/credential')

with open(f"{path}/credential/dice.txt", 'w') as file:
file.write(f"access_token:{api['access_token']},expires_in:{api['expires_in']},hoursExpiration:{hoursExpiration},dateExpiration:{dateNow},refresh_token:{api['refresh_token']}")
file.write(f"""access_token:{api['access_token']},
\rexpires_in:{api['expires_in']},
\rhoursExpiration:{hoursExpiration},
\rdateExpiration:{dateNow},
\rrefresh_token:{api['refresh_token']}""")
file.close()

return {
'access_token' : api['access_token'],
'expires_in' : api['expires_in'],
'token_type' : api['token_type'],
'scope' : api['scope'],
'refresh_token' : api['refresh_token']
'access_token': api['access_token'],
'expires_in': api['expires_in'],
'token_type': api['token_type'],
'scope': api['scope'],
'refresh_token': api['refresh_token']
}

def refreshToken(self, refresh_token : str):

def refreshToken(self, refresh_token: str):
"""
Return the new access token and update the file with the credentials.
Return the new access token and update the file with the credentials.

:Usage:
::
obj = Bling().refreshToken("access_token")
:Usage:
::
obj = Bling().refreshToken("access_token")
"""


dice = {
'grant_type':'refresh_token',
'refresh_token':refresh_token
'grant_type': 'refresh_token',
'refresh_token': refresh_token
}

apiStatus = requests.post('https://www.bling.com.br/Api/v3/oauth/token', headers=header, json=dice)
apiStatus = requests.post(
'https://www.bling.com.br/Api/v3/oauth/token',
headers=header, json=dice
)

api = apiStatus.json()

Expand All @@ -130,22 +157,28 @@ def refreshToken(self, refresh_token : str):
dateNow = datetime.date.today()
apiHoursNow = ((int(api['expires_in'])/60)/60)
systemHoursNow = datetime.datetime.now()
hoursExpiration = ( int(systemHoursNow.strftime("%H")) + apiHoursNow )

hoursExpiration = (
int(systemHoursNow.strftime("%H")) + apiHoursNow
)

if os.path.isdir(f"{path}/credential"):
pass
else:
os.mkdir(f'{path}/credential')

with open(f"{path}/credential/dice.txt", 'w') as file:
file.write(f"access_token:{api['access_token']},expires_in:{api['expires_in']},hoursExpiration:{hoursExpiration},dateExpiration:{dateNow},refresh_token:{api['refresh_token']}")
file.write(f"""access_token:{api['access_token']},
\rexpires_in:{api['expires_in']},
\rhoursExpiration:{hoursExpiration},
\rdateExpiration:{dateNow},
\rrefresh_token:{api['refresh_token']}""")
file.close()

return {
'access_token' : api['access_token'],
'expires_in' : api['expires_in'],
'token_type' : api['token_type'],
'scope' : api['scope'],
'refresh_token' : api['refresh_token']
'access_token': api['access_token'],
'expires_in': api['expires_in'],
'token_type': api['token_type'],
'scope': api['scope'],
'refresh_token': api['refresh_token']
}