lbxtoolkit 2.2.7__tar.gz → 2.2.8__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: lbxtoolkit
3
- Version: 2.2.7
3
+ Version: 2.2.8
4
4
  Summary:
5
5
  Author: Cristiano P. Ferrari
6
6
  Author-email: cristiano.ferrari@lbx.com.br
@@ -166,6 +166,56 @@ Para uso em APIs com autenticação JWT (JSON Web Token), OAuth, Bearer Token Au
166
166
  return Err
167
167
  #
168
168
  #
169
+ def auth_token(self): # Autentica e abre sessão na API
170
+ if self.Credenciais['token']:
171
+ Token = self.Credenciais['token']
172
+ self.Headers.update({"UserAuthorization": f"token {Token}"})
173
+ else:
174
+ self.logger.critico(f'Falha de requisição api_rest.auth_token: Token não fornecido no dicionário de credenciais! Necessário informar chave da API em dicionário com chave "token" no instanciamento da classe', exit=1)
175
+
176
+ try:
177
+ self.Sessao = requests.Session()
178
+ Auth = self.Sessao.post(self.Url, headers=self.Headers, verify=self.Verify)
179
+
180
+ if 200 <= Auth.status_code <= 299:
181
+ self.Sessao.status_code = Auth.status_code
182
+ return self.Sessao
183
+ else:
184
+ self.logger.critico(f"Erro ao autenticar API: {self.Sessao.status_code} - {self.Sessao.text}")
185
+ raise Exception
186
+ except Exception as Err:
187
+ self.logger.critico(f"Falha ao autenticar API: {Err}. URL: {self.Url}")
188
+ return Err
189
+ #
190
+ #
191
+ def url_request(self, metodo, payload=None): # Efetua chamada diretamente à URL e não a EndPoint específico
192
+ self.ult_tempo_req = time()
193
+ self.Metodo = metodo.lower()
194
+ self.EndPoint = self.Url
195
+ self.Payload = payload
196
+ MetodosAceitos = ['post', 'get', 'patch', 'put']
197
+ if not any(element in self.Metodo for element in MetodosAceitos):
198
+ self.logger.critico(f'Método {self.Metodo} não previsto. Abortando chamada!', exit=1)
199
+ else:
200
+ ChamadaApi = f'self.Sessao.{self.Metodo}(self.Url, timeout=self.Timeout, headers=self.Headers, verify=self.Verify)' if self.Payload is None else f'self.Sessao.{self.Metodo}(self.Url, timeout=self.Timeout, headers=self.Headers, verify=self.Verify, data=self.Payload)'
201
+ self.controla_cadencia()
202
+ self.TempoUltReq = time()
203
+ try:
204
+ self.RetEndPoint = eval(ChamadaApi)
205
+ if self.RetEndPoint.status_code >= 500:
206
+ self.logger.critico(f'Erro [{self.RetEndPoint.status_code}] no lado servidor na resposta à requisição')
207
+ raise Exception
208
+ self.RetEndPoint.Espera = self.Espera ## adiona o tempo de espera ao retorno da API
209
+ self.RetEndPoint.Intervalo = self.Intervalo ## adiciona o intervalo entre chamada ao retorno da API
210
+ return self.RetEndPoint
211
+ except requests.exceptions.ReadTimeout as Err:
212
+ self.logger.critico(f'Excedido tempo limite {self.Timeout} para retorno do endpoint: {Err}\nEndpoint: {self.EndPoint}')
213
+ return Err
214
+ except Exception as Err:
215
+ self.logger.critico(f'Falha na chamada do endpoint: {Err}\nEndpoint: {self.EndPoint}\nCodigo retorno: {self.RetEndPoint.status_code}\nResposta:{self.RetEndPoint.text}')
216
+ return self.RetEndPoint
217
+ #
218
+ #
169
219
  def endpoint_json(self, endpoint, metodo, payload=None): # Interage com End Point
170
220
  self.ult_tempo_req = time()
171
221
  self.Metodo = metodo.lower()
@@ -182,7 +232,7 @@ Para uso em APIs com autenticação JWT (JSON Web Token), OAuth, Bearer Token Au
182
232
  try:
183
233
  self.RetEndPoint = eval(ChamadaApi)
184
234
  if self.RetEndPoint.status_code >= 500:
185
- self.logger.critico(f'Erro {self.RetEndPoint.status_code} na chamada do endpoint: {Err}\nEndpoint: {self.EndPoint}\nResposta: {self.RetEndPoint.text}')
235
+ self.logger.critico(f'Erro [{self.RetEndPoint.status_code}] no lado servidor na resposta à requisição')
186
236
  raise Exception
187
237
  self.RetEndPoint.Espera = self.Espera ## adiona o tempo de espera ao retorno da API
188
238
  self.RetEndPoint.Intervalo = self.Intervalo ## adiciona o intervalo entre chamada ao retorno da API
@@ -192,7 +242,7 @@ Para uso em APIs com autenticação JWT (JSON Web Token), OAuth, Bearer Token Au
192
242
  return Err
193
243
  except Exception as Err:
194
244
  self.logger.critico(f'Falha na chamada do endpoint: {Err}\nEndpoint: {self.EndPoint}\nCodigo retorno: {self.RetEndPoint.status_code}\nResposta:{self.RetEndPoint.text}')
195
- return Err
245
+ return self.RetEndPoint
196
246
  #
197
247
  #
198
248
  def trata_erro_sienge(CodRet, Retorno):
@@ -208,10 +208,10 @@ conexao.close()
208
208
  #
209
209
  def db_update(self, Query): # Retorna um cursor à partir de um select
210
210
  UpdRows = 0
211
- BlackList = ['INSERT ', 'DELETE ', 'SELECT ', 'CREATE ', 'DROP ', 'MERGE ', 'REPLACE ', 'CALL ', 'EXECUTE ']
211
+ BlackList = ['INSERT ', 'SELECT ', 'CREATE ', 'DROP ', 'MERGE ', 'REPLACE ', 'CALL ', 'EXECUTE ']
212
212
  if any(element in Query.upper() for element in BlackList):
213
213
  BlackListed = [element for element in BlackList if element in Query.upper()]
214
- self.logger.erro(f'{__name__}: Este método permite apenas updates. A query informada possui as seguintes palavras reservadas não aceitas: {BlackListed} e não foi executada!')
214
+ self.logger.erro(f'{__name__}: Este método permite apenas updates e deletes. A query informada possui as seguintes palavras reservadas não aceitas: {BlackListed} e não foi executada!')
215
215
  return None
216
216
  else:
217
217
  try:
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "lbxtoolkit"
3
- version = "2.2.7"
3
+ version = "2.2.8"
4
4
  description = ""
5
5
  authors = ["Cristiano P. Ferrari <cristiano.ferrari@lbx.com.br>"]
6
6
  readme = "README.md"
File without changes