Remove the network utils `fetch_host_ip_and_country` and `fetch_country_by_ip`

The library that was used starting to require an API key.
matrix-appservice
Jason Robinson 2020-12-20 02:59:17 +02:00
rodzic 26e6e4d04d
commit 836e19b40d
5 zmienionych plików z 7 dodań i 60 usunięć

Wyświetl plik

@ -2,7 +2,10 @@
## [unreleased]
* No changes yet
### Removed
* Removed the network utils `fetch_host_ip_and_country` and `fetch_country_by_ip` due to the
library that was used starting to require an API key.
## [0.20.0] - 2020-12-20

Wyświetl plik

@ -277,9 +277,7 @@ Diaspora
Network
.......
.. autofunction:: federation.utils.network.fetch_country_by_ip
.. autofunction:: federation.utils.network.fetch_document
.. autofunction:: federation.utils.network.fetch_host_ip_and_country
.. autofunction:: federation.utils.network.send_document
Protocols

Wyświetl plik

@ -5,17 +5,8 @@ from requests import HTTPError
from requests.exceptions import SSLError, RequestException
from federation.utils.network import (
fetch_document, USER_AGENT, send_document, fetch_country_by_ip, fetch_host_ip_and_country, fetch_host_ip)
@patch('federation.utils.network.ipdata', autospec=True)
class TestFetchCountryByIp:
def test_calls_ip_api_endpoint(self, mock_ipdata):
mock_lookup = Mock(lookup=Mock(return_value={'status': 200, 'response': {'country_code': 'DE'}}))
mock_ipdata.IPData.return_value = mock_lookup
country = fetch_country_by_ip('127.0.0.1')
mock_lookup.lookup.assert_called_once_with('127.0.0.1')
assert country == 'DE'
fetch_document, USER_AGENT, send_document, fetch_host_ip,
)
class TestFetchDocument:
@ -113,16 +104,6 @@ class TestFetchHostIp:
mock_get_ip.assert_called_once_with('domain.local')
class TestFetchHostIpAndCountry:
@patch('federation.utils.network.fetch_country_by_ip', autospec=True, return_value='FI')
@patch('federation.utils.network.fetch_host_ip', autospec=True, return_value='127.0.0.1')
def test_calls(self, mock_get_ip, mock_fetch_country):
result = fetch_host_ip_and_country('domain.local')
assert result == ('127.0.0.1', 'FI')
mock_get_ip.assert_called_once_with('domain.local')
mock_fetch_country.assert_called_once_with('127.0.0.1')
class TestSendDocument:
call_args = {"timeout": 10, "headers": {'user-agent': USER_AGENT}}

Wyświetl plik

@ -3,11 +3,10 @@ import datetime
import logging
import re
import socket
from typing import Optional, Tuple
from typing import Optional
from urllib.parse import quote
import requests
from ipdata import ipdata
from requests.exceptions import RequestException, HTTPError, SSLError
from requests.exceptions import ConnectionError
from requests.structures import CaseInsensitiveDict
@ -31,26 +30,6 @@ def fetch_content_type(url: str) -> Optional[str]:
return response.headers.get('Content-Type')
def fetch_country_by_ip(ip):
"""
Fetches country code by IP
Returns empty string if the request fails in non-200 code.
Uses the ipdata.co service which has the following rules:
* Max 1500 requests per day
See: https://ipdata.co/docs.html#python-library
"""
iplookup = ipdata.IPData()
data = iplookup.lookup(ip)
if data.get('status') != 200:
return ''
return data.get('response', {}).get('country_code', '')
def fetch_document(url=None, host=None, path="/", timeout=10, raise_ssl_errors=True, extra_headers=None):
"""Helper method to fetch remote document.
@ -128,19 +107,6 @@ def fetch_host_ip(host: str) -> str:
return ip
def fetch_host_ip_and_country(host: str) -> Tuple:
"""
Fetch ip and country by host
"""
ip = fetch_host_ip(host)
if not host:
return '', ''
country = fetch_country_by_ip(ip)
return ip, country
def parse_http_date(date):
"""
Parse a date format as specified by HTTP RFC7231 section 7.1.1.1.

Wyświetl plik

@ -34,7 +34,6 @@ setup(
"cssselect>=0.9.2",
"dirty-validators>=0.3.0",
"lxml>=3.4.0",
"ipdata>=3.0",
"iteration_utilities",
"jsonschema>=2.0.0",
"pycryptodome>=3.4.10",