Skip to content
This repository has been archived by the owner on Jul 19, 2020. It is now read-only.

Commit

Permalink
Develop/main (#12)
Browse files Browse the repository at this point in the history
* extend tests for RUZ.person_lessons

* [skip ci] move to v1.0.1

* change url for API v2

* [skip ci] move to v1.1.0
  • Loading branch information
hell03end authored Oct 28, 2017
1 parent d00b965 commit 315afa0
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 39 deletions.
44 changes: 33 additions & 11 deletions ruz/RUZ.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from urllib import error, parse, request

from ruz.utils import (EMAIL_DOMAINS, EMAIL_PATTERN, REQUEST_SCHEMA,
RUZ_API_ENDPOINTS, RUZ_API_URL, Logger, log, none_safe)
RUZ_API2_ENDPOINTS, RUZ_API2_URL, RUZ_API_ENDPOINTS,
RUZ_API_URL, Logger, log, none_safe)


class RUZ(object):
Expand All @@ -24,7 +25,7 @@ class RUZ(object):
>>> api = RUZ()
"""

def __init__(self, strict_v1: bool=True, **kwargs):
def __init__(self, strict_v1: bool=False, **kwargs):
"""
:param strict_v1 - force usage of api v1.
Expand Down Expand Up @@ -65,8 +66,12 @@ def __init__(self, strict_v1: bool=True, **kwargs):
)
self._logger = Logger(str(self.__class__))
self._url2 = self._url
self._endpoints2 = self._endpoints
self._v = 1
if not strict_v1:
self._url2 += r"v2/"
self._url2 = kwargs.pop('base_url', RUZ_API2_URL)
self._endpoints2 = kwargs.pop('endpoints', RUZ_API2_ENDPOINTS)
self._v = 2
super(RUZ, self).__init__()

@property
Expand Down Expand Up @@ -106,12 +111,12 @@ def v(self) -> int:
Usage
-----
>>> RUZ().v
>>> RUZ(strict_v1=True).v
1
>>> RUZ(strict_v1=False).v
2
"""
return 2 if self._url2[-2] == "2" else 1
return self._v

@property
def domains(self) -> tuple:
Expand Down Expand Up @@ -139,6 +144,20 @@ def endpoints(self) -> dict:
"""
return deepcopy(self._endpoints)

@property
def endpoints2(self) -> dict:
"""
Collection HSE API endpoints for API v2
Usage
-----
>>> isinstance(RUZ().endpoints2, dict)
True
>>> RUZ().endpoints2 is not RUZ().endpoints2
True
"""
return deepcopy(self._endpoints2)

@staticmethod
def is_student(email: str) -> bool:
"""
Expand Down Expand Up @@ -180,11 +199,14 @@ def _make_url(self, endpoint: str, data: dict=None, v: int=1) -> str:
>>> RUZ()._make_url("schedule", data={'email': 123})
http://92.242.58.221/ruzservice.svc/personLessons?email=123
"""
url = self._url if v == 1 or self.v == 1 else self._url2
url = self._url
endpoint = self._endpoints[endpoint]
if v != 1 and self.v != 1:
url = self._url2
endpoint = self._endpoints2[endpoint]
if data:
return "{}{}?{}".format(url, self._endpoints[endpoint],
parse.urlencode(data))
return "{}{}".format(url, self._endpoints[endpoint])
return "{}{}?{}".format(url, endpoint, parse.urlencode(data))
return "{}{}".format(url, endpoint)

@log
def _request(self, endpoint: str, data: dict=None) -> HTTPResponse:
Expand All @@ -201,7 +223,7 @@ def _request(self, endpoint: str, data: dict=None) -> HTTPResponse:
...
urllib.error.HTTPError: HTTP Error 400: Bad Request
"""
if self.v == 2:
if self._v == 2:
# api v2 may be unreachable for some API methods
try:
return request.urlopen(self._make_url(endpoint, data, v=2))
Expand Down Expand Up @@ -239,7 +261,7 @@ def _verify_schema(self, endpoint: str, **params) -> None:
params.get('receiverType', 3),
params.pop('check_online', False)
)
endpoint = self._endpoints[endpoint]
endpoint = self._endpoints[endpoint] # it's ok to use only v1 here
schema = self._schema.get(endpoint)
if schema is None:
raise KeyError("Wrong endpoint: '{}'".format(endpoint))
Expand Down
4 changes: 2 additions & 2 deletions ruz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
-----
from ruz import RUZ
api = RUZ()
assert api.v == 1
assert api.v == 2
assert api.person_lessons("mymail@edu.hse.ru")
"""

Expand All @@ -15,5 +15,5 @@
from ruz.utils import REQUEST_SCHEMA, RESPONSE_SCHEMA, EMAIL_DOMAINS

__author__ = "hell03end"
__version__ = (1, 0, 1)
__version__ = (1, 1, 0)
__all__ = ("RUZ", "REQUEST_SCHEMA", "RESPONSE_SCHEMA")
6 changes: 4 additions & 2 deletions ruz/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

from ruz.utils.decorators import none_safe
from ruz.utils.logging import Logger, log
from ruz.utils.schema import REQUEST_SCHEMA, RESPONSE_SCHEMA, RUZ_API_ENDPOINTS
from ruz.utils.schema import (REQUEST_SCHEMA, RESPONSE_SCHEMA,
RUZ_API_ENDPOINTS, RUZ_API2_ENDPOINTS)


RUZ_API_URL = r"http://92.242.58.221/ruzservice.svc/"
RUZ_API2_URL = r"https://www.hse.ru/api/"
EMAIL_PATTERN = r"\b[a-zA-Z0-9\._-]{2,}@([a-zA-Z]{2,}\.)?[a-zA-Z]{2,}\.ru\b"
EMAIL_DOMAINS = ("hse.ru", "edu.hse.ru")
EMAIL_DOMAINS = (r"hse.ru", r"edu.hse.ru")
50 changes: 28 additions & 22 deletions ruz/utils/schema.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
# collection of API endpoints (and their aliases)
RUZ_API_ENDPOINTS = {
'schedule': "personLessons",
'lessons': "personLessons",
'person_lessons': "personLessons",
'personLessons': "personLessons",
'groups': "groups",
'staffOfGroup': "staffOfGroup",
'staff_of_group': "staffOfGroup",
'streams': "streams",
'staffOfStreams': "staffOfStreams",
'staff_of_streams': "staffOfStreams",
'lecturers': "lecturers",
'auditoriums': "auditoriums",
'typeOfAuditoriums': "typeOfAuditoriums",
'type_of_auditoriums': "typeOfAuditoriums",
'kindOfWorks': "kindOfWorks",
'kind_of_works': "kindOfWorks",
'buildings': "buildings",
'faculties': "faculties",
'chairs': "chairs",
'subGroups': "subGroups",
'subgroups': "subGroups",
'sub_groups': "subGroups"
'schedule': r"personLessons",
'lessons': r"personLessons",
'person_lessons': r"personLessons",
'personLessons': r"personLessons",
'groups': r"groups",
'staffOfGroup': r"staffOfGroup",
'staff_of_group': r"staffOfGroup",
'streams': r"streams",
'staffOfStreams': r"staffOfStreams",
'staff_of_streams': r"staffOfStreams",
'lecturers': r"lecturers",
'auditoriums': r"auditoriums",
'typeOfAuditoriums': r"typeOfAuditoriums",
'type_of_auditoriums': r"typeOfAuditoriums",
'kindOfWorks': r"kindOfWorks",
'kind_of_works': r"kindOfWorks",
'buildings': r"buildings",
'faculties': r"faculties",
'chairs': r"chairs",
'subGroups': r"subGroups",
'subgroups': r"subGroups",
'sub_groups': r"subGroups"
}
RUZ_API2_ENDPOINTS = {
'schedule': r"timetable/lessons",
'lessons': r"timetable/lessons",
'person_lessons': r"timetable/lessons",
'personLessons': r"timetable/lessons"
}

# type rules to make request for API
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
setup(
name="hse_ruz",
packages=packages,
version="1.0.1",
version="1.1.0",
description="Python wrapper for HSE RUZ API",
long_description=open(os.path.join(os.path.dirname(__file__),
"README.rst")).read(),
Expand Down
4 changes: 3 additions & 1 deletion tests/test_RUZ.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import pytest
from ruz import EMAIL_DOMAINS, REQUEST_SCHEMA, RUZ
from ruz.utils import RUZ_API2_ENDPOINTS as RUZ_ENDPOINTS2
from ruz.utils import RUZ_API2_URL as RUZ_URL2
from ruz.utils import RUZ_API_ENDPOINTS as RUZ_ENDPOINTS
from ruz.utils import RUZ_API_URL as RUZ_URL
from ruz.utils.logging import Logger
Expand Down Expand Up @@ -101,11 +103,11 @@ def test_is_student(self):
assert excinfo

def test__make_url(self):
RUZ_URL2 = RUZ_URL + "v2/"
for key, endpoint in RUZ_ENDPOINTS.items():
assert self.api._make_url(key) == RUZ_URL + endpoint
assert self.api._make_url(key, v=2) == RUZ_URL + endpoint
assert self.api2._make_url(key, v=1) == RUZ_URL + endpoint
for key, endpoint in RUZ_ENDPOINTS2.items():
assert self.api2._make_url(key, v=2) == RUZ_URL2 + endpoint
with pytest.raises(KeyError):
self.api._make_url("")
Expand Down

0 comments on commit 315afa0

Please sign in to comment.