Skip to content

Commit

Permalink
Merge pull request #111 from AlanTaranti/homolog
Browse files Browse the repository at this point in the history
Homolog -> Master
  • Loading branch information
AlanTaranti committed Sep 19, 2022
2 parents e410739 + e1e506c commit 4f0b9e8
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 38 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Changelog
========================================

v0.1.4 - 2022-09-18
----------------------------------------
- Fix [#88](https://github.com/AlanTaranti/sidrapy/issues/88) - Fixed connection to IBGE service, that don't support [RFC 5746](https://www.rfc-editor.org/rfc/rfc5746) - Obrigado [@nicmorais](https://github.com/nicmorais)

v0.1.3 - 2022-05-26
----------------------------------------
- Added support for disable ssl verification
Expand Down
2 changes: 1 addition & 1 deletion __version__
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.3
0.1.4
2 changes: 1 addition & 1 deletion src/sidrapy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .table import get_table

VERSION = "0.1.3"
VERSION = "0.1.4"
6 changes: 3 additions & 3 deletions src/sidrapy/resources/handler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Dict

import requests
from .http_client import HttpClient

ENDPOINT_BASE = "https://apisidra.ibge.gov.br"

Expand Down Expand Up @@ -56,7 +56,6 @@ def get(
classifications: Dict[str, str] = None,
period: str = None,
header: str = None,
verify_ssl: bool = True,
):
url = get_url(
table_code,
Expand All @@ -70,7 +69,8 @@ def get(
header,
)

response = requests.get(url, verify=verify_ssl)
with HttpClient.get_legacy_session() as session:
response = session.get(url)

if not response.ok:
raise ValueError(response.text)
Expand Down
29 changes: 29 additions & 0 deletions src/sidrapy/resources/http_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import requests
import urllib3
import ssl


class HttpClient:
@staticmethod
def get_legacy_session():
ctx = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
ctx.options |= 0x4 # OP_LEGACY_SERVER_CONNECT
session = requests.session()
session.mount("https://", HttpAdapter(ctx))
return session


class HttpAdapter(requests.adapters.HTTPAdapter):
# "Transport adapter" that allows us to use custom ssl_context.

def __init__(self, ssl_context=None, **kwargs):
self.ssl_context = ssl_context
super().__init__(**kwargs)

def init_poolmanager(self, connections, maxsize, block=False):
self.poolmanager = urllib3.poolmanager.PoolManager(
num_pools=connections,
maxsize=maxsize,
block=block,
ssl_context=self.ssl_context,
)
6 changes: 1 addition & 5 deletions src/sidrapy/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def get_table(
period: str = None,
header: str = None,
format: str = "pandas",
verify_ssl: bool = True,
**kwargs # noqa
):
"""Realiza a busca da tabela no SIDRA
Expand Down Expand Up @@ -63,9 +63,6 @@ def get_table(
- 'pandas' - Retorna uma pandas Dataframe
- 'list' - Retorna uma lista
verify_ssl : bool, optional (padrão=True)
Habilita/Desabilita a verificação ssl
Returns
-------
- list
Expand Down Expand Up @@ -134,7 +131,6 @@ def get_table(
classifications,
period,
header,
verify_ssl,
)

if format == "pandas":
Expand Down
78 changes: 73 additions & 5 deletions tests/integration/test_basic.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import requests
from src import sidrapy
from src.sidrapy.resources.http_client import HttpClient


def test_connection():
url = sidrapy.resources.handler.ENDPOINT_BASE
r = requests.get(url, timeout=10, verify=False)
with HttpClient.get_legacy_session() as session:
r = session.get(url, timeout=10)
assert r.status_code == 200


def test_sample_request():
url = sidrapy.resources.handler.ENDPOINT_BASE
# api docs here: http://api.sidra.ibge.gov.br/home/ajuda
url += "/values/t/1612/p/2018/v/allxp/n1/1/d/m/h/y"
r = requests.get(url, timeout=10, verify=False)
with HttpClient.get_legacy_session() as session:
r = session.get(url, timeout=10)
assert r.status_code == 200
sample_response = [
{
Expand Down Expand Up @@ -118,7 +120,74 @@ def test_single_classification():
categories="39324",
period="202002",
format="list",
verify_ssl=False,
)

expected_response = [
{
"NC": "Nível Territorial (Código)",
"NN": "Nível Territorial",
"MC": "Unidade de Medida (Código)",
"MN": "Unidade de Medida",
"V": "Valor",
"D1C": "Brasil (Código)",
"D1N": "Brasil",
"D2C": "Semestre (Código)",
"D2N": "Semestre",
"D3C": "Grupos de capacidade útil (Código)",
"D3N": "Grupos de capacidade útil",
"D4C": "Variável (Código)",
"D4N": "Variável",
"D5C": "Tipo de unidade armazenadora (Código)",
"D5N": "Tipo de unidade armazenadora",
},
{
"NC": "1",
"NN": "Brasil",
"MC": "1020",
"MN": "Unidades",
"V": "6731",
"D1C": "1",
"D1N": "Brasil",
"D2C": "202002",
"D2N": "2º semestre 2020",
"D3C": "39324",
"D3N": "Total",
"D4C": "152",
"D4N": "Número de estabelecimentos",
"D5C": "114630",
"D5N": "Total",
},
{
"NC": "1",
"NN": "Brasil",
"MC": "1017",
"MN": "Toneladas",
"V": "153406525",
"D1C": "1",
"D1N": "Brasil",
"D2C": "202002",
"D2N": "2º semestre 2020",
"D3C": "39324",
"D3N": "Total",
"D4C": "153",
"D4N": "Capacidade útil",
"D5C": "114630",
"D5N": "Total",
},
]

assert data == expected_response


def test_single_classification():
data = sidrapy.get_table(
table_code="5459",
territorial_level="1",
ibge_territorial_code="all",
classification="11278",
categories="39324",
period="202002",
format="list",
)

expected_response = [
Expand Down Expand Up @@ -186,7 +255,6 @@ def test_multiple_classification():
classifications={"11278": "33460", "166": "3067,3327"},
period="202002",
format="list",
verify_ssl=False,
)

print(data)
Expand Down
23 changes: 0 additions & 23 deletions tests/unit/test_handler.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
from typing import Dict
from random import randint, random
from unittest.mock import Mock
from unittest.mock import patch
from uuid import uuid4

import pytest
from src import sidrapy


Expand Down Expand Up @@ -82,23 +79,3 @@ def test_get_url():
for k, v in value.items():
assert k in url
assert v in url


def test_get_ok():
mock_response = Mock()
mock_response.ok = True
with patch("src.sidrapy.resources.handler.requests") as mock_request:
kwargs = random_args_classification()
mock_request.get.return_value = mock_response
response = sidrapy.resources.handler.get(**kwargs)
assert response is mock_response.json()


def test_get_not_ok():
mock_response = Mock()
mock_response.ok = False
with patch("src.sidrapy.resources.handler.requests") as mock_request:
kwargs = random_args_classification()
mock_request.get.return_value = mock_response
with pytest.raises(ValueError):
sidrapy.resources.handler.get(**kwargs)

0 comments on commit 4f0b9e8

Please sign in to comment.