diff --git a/README.md b/README.md index 33b9642..fe8dbd8 100755 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ http://developer.omie.com.br/service-list/ - [Delphi](https://github.com/omiexperience/api-examples/tree/master/delphi) - [DotNet](https://github.com/omiexperience/api-examples/tree/master/dotnet) - [PHP](https://github.com/omiexperience/api-examples/tree/master/php) + - [Python](https://github.com/iepsenn/api-examples/tree/master/python) ## Como testar as API's diff --git a/python/README.md b/python/README.md new file mode 100644 index 0000000..cd58cb6 --- /dev/null +++ b/python/README.md @@ -0,0 +1,19 @@ +# Extraindo dados da API utilizando Python + +## Pré-requisitos +O único prerequisito pode ser instalado com o seguinte comando: +``` +pip install PyYAML +``` + +## Arquivos de configuração +* `keys.yaml` + Chaves da API a serem usadas no extrator. +* `routes.yaml` + Rotas e método a ser acessado. + +## Execução +Basta mudar os parâmetros nos arquivos de configuração e rodar o script `extractor.py` + + +[Link](https://github.com/iepsenn/omie-extractor) para extrator de dados um pouco mais completo. diff --git a/python/extractor.py b/python/extractor.py new file mode 100644 index 0000000..f8bc57d --- /dev/null +++ b/python/extractor.py @@ -0,0 +1,47 @@ +import requests +import yaml + +url = 'https://app.omie.com.br/api/v1/' + +# Lendo arquivos de configuração +with open('./keys.yaml') as file: + keys = yaml.load(file, Loader=yaml.FullLoader) + +with open('./routes.yaml') as file: + routes = yaml.load(file, Loader=yaml.FullLoader) + +# Atribuindo chaves da api para variaveis +app_key = keys['app_key'] +app_secret = keys['app_secret'] + +route = routes['route'] +call = routes['call'] + +# Parâmetros para requisição +params = { + "call": "{}".format(call), + "app_key": "{}".format(app_key), + "app_secret": "{}".format(app_secret), + "param": [ + { + "registros_por_pagina": 10, + "apenas_importado_api": "N" + } + ] +} + +# Formata o url para pegar os dados +url_api = '{}{}?JSON={}'.format( + url, + route, + str(params).replace('\'', "\"") +) + +# Fazendo a request pra página +response = requests.get(url_api) + +# Testa se deu tudo certo +if response.status_code == 200: + print(response.json()[list(response.json().keys())[-1]]) +else: + print('Requisição falhou') diff --git a/python/keys.yaml b/python/keys.yaml new file mode 100644 index 0000000..aa57715 --- /dev/null +++ b/python/keys.yaml @@ -0,0 +1,2 @@ +app_key: 1560731700 +app_secret: "226dcf372489bb45ceede61bfd98f0f1" diff --git a/python/routes.yaml b/python/routes.yaml new file mode 100644 index 0000000..360b097 --- /dev/null +++ b/python/routes.yaml @@ -0,0 +1,2 @@ +route: "geral/produtos/" +call: "ListarProdutos"