From 0f561eedaf8b65e997ccae0fe211b7d84eb14167 Mon Sep 17 00:00:00 2001 From: iepsenn Date: Tue, 18 Feb 2020 00:21:30 -0300 Subject: [PATCH 1/3] adicionando exemplo em python e link no readme --- README.md | 1 + python/README.md | 17 ++++++++++++++++ python/extractor.py | 47 +++++++++++++++++++++++++++++++++++++++++++++ python/keys.yaml | 2 ++ python/routes.yaml | 2 ++ 5 files changed, 69 insertions(+) create mode 100644 python/README.md create mode 100644 python/extractor.py create mode 100644 python/keys.yaml create mode 100644 python/routes.yaml diff --git a/README.md b/README.md index 33b9642..eb55b48 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](python/Readme.md) ## Como testar as API's diff --git a/python/README.md b/python/README.md new file mode 100644 index 0000000..7ff776b --- /dev/null +++ b/python/README.md @@ -0,0 +1,17 @@ +## 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..57be9eb --- /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()) +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" From 47a2e5b563970697a9f2c45ba2acec05cf0348c5 Mon Sep 17 00:00:00 2001 From: iepsenn Date: Tue, 18 Feb 2020 00:23:41 -0300 Subject: [PATCH 2/3] fix link --- README.md | 2 +- python/README.md | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index eb55b48..fe8dbd8 100755 --- a/README.md +++ b/README.md @@ -32,7 +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](python/Readme.md) + - [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 index 7ff776b..cd58cb6 100644 --- a/python/README.md +++ b/python/README.md @@ -1,3 +1,5 @@ +# Extraindo dados da API utilizando Python + ## Pré-requisitos O único prerequisito pode ser instalado com o seguinte comando: ``` From e18187bec48931e348eebdf88e401284f9304d95 Mon Sep 17 00:00:00 2001 From: iepsenn Date: Wed, 19 Feb 2020 16:21:01 -0300 Subject: [PATCH 3/3] acessando corretamente os dados --- python/extractor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/extractor.py b/python/extractor.py index 57be9eb..f8bc57d 100644 --- a/python/extractor.py +++ b/python/extractor.py @@ -42,6 +42,6 @@ # Testa se deu tudo certo if response.status_code == 200: - print(response.json()) + print(response.json()[list(response.json().keys())[-1]]) else: print('Requisição falhou')