Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pageviewapi/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from importlib.metadata import PackageNotFoundError, version
from typing import Any
from urllib.parse import quote, unquote

import requests

Expand Down Expand Up @@ -100,7 +101,7 @@ def per_article(
"""
args = PA_ARGS.format(
project=project,
page=page,
page=_quote_article_name(page),
start=start,
end=end,
access=access,
Expand Down Expand Up @@ -199,3 +200,7 @@ def _api(end_point: str, args: str, api_url: str = API_BASE_URL) -> dict[str, An
else:
response.raise_for_status()
return {} # unreachable, satisfies type checker


def _quote_article_name(page: str) -> str:
return quote(unquote(page), safe="")
24 changes: 24 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
ThrottlingException,
ZeroOrDataNotLoadedException,
_api,
_quote_article_name,
aggregate,
legacy_pagecounts,
per_article,
Expand Down Expand Up @@ -209,3 +210,26 @@ def test_api_returns_pageview_response(ok_response):
with patch("requests.get", return_value=ok_response):
result = per_article("en.wikipedia", "Paris", "20151106", "20151120")
assert isinstance(result, PageviewResponse)


def test_quote_article_name_plain():
assert _quote_article_name("Paris") == "Paris"


def test_quote_article_name_slash():
assert _quote_article_name("AC/DC") == "AC%2FDC"


def test_per_article_encodes_space_in_page_name(ok_response):
with patch("requests.get", return_value=ok_response) as mock_get:
per_article("en.wikipedia", "New York", "20200101", "20200131")
url = mock_get.call_args[0][0]
assert "New%20York" in url
assert "New York" not in url


def test_per_article_encodes_slash_in_page_name(ok_response):
with patch("requests.get", return_value=ok_response) as mock_get:
per_article("en.wikipedia", "AC/DC", "20200101", "20200131")
url = mock_get.call_args[0][0]
assert "AC%2FDC" in url
Loading