Skip to content

Commit

Permalink
Merge pull request #8 from SamuelGuillemet/develop
Browse files Browse the repository at this point in the history
🚀 RELEASE: Bump to 1.0.0
  • Loading branch information
SamuelGuillemet authored Jul 10, 2023
2 parents aac3b85 + e2c988d commit 76e6dd3
Show file tree
Hide file tree
Showing 15 changed files with 335 additions and 126 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,14 @@ cern_ms_client = CERNMSApiClient.init_from_dotenv()
#### **events**:

1. get all events
2. get a single event using indico id
2. get a single event using zoom id
3. create an event
4. update an event using indico id
5. delete an event using indico id
4. update an event using zoom id
5. delete an event using zoom id

You will find useful the `EventParameters` and `PartialEventParameters` classes, which will help you to create the events.

- `INDICO_EVENT_ID` is the id of the event in the indico system which is **mandatory** to create an event.
- `ZOOM_ID` is the id of the zoom meeting, which can be found inside the url of a meeting link, is **mandatory** to create an event.
- `USER_ID` is the email of the Zoom Room.

```python
Expand All @@ -149,23 +149,23 @@ from ms_python_client.utils.event_generator import (EventParameters, PartialEven
cern_ms_client = CERNMSApiClient.init_from_dotenv()

USER_ID = os.getenv("USER_ID") # Which is the email of the Zoom Room
INDICO_EVENT_ID = os.getenv("INDICO_EVENT_ID")
ZOOM = os.getenv("ZOOM")

event_parameters = EventParameters(
subject="Test meeting",
start_time="2021-10-01T12:00:00",
end_time="2021-10-01T13:00:00",
timezone="Europe/Zurich",
indico_event_id=INDICO_EVENT_ID,
zoom_id=ZOOM,
zoom_url="https://cern.zoom.us/******",
)

partial_event_parameters = PartialEventParameters(
indico_event_id=INDICO_EVENT_ID,
zoom_id=ZOOM,
end_time="2021-10-01T14:00:00",
) # You can update only the end_time of the event for example

cern_ms_client.events.create_event(USER_ID, event_parameters)
cern_ms_client.events.update_event_by_indico_id(USER_ID, partial_event_parameters)
cern_ms_client.events.delete_event_by_indico_id(USER_ID, INDICO_EVENT_ID)
cern_ms_client.events.update_event_by_zoom_id(USER_ID, partial_event_parameters)
cern_ms_client.events.delete_event_by_zoom_id(USER_ID, ZOOM)
```
44 changes: 27 additions & 17 deletions ms_python_client/api_client.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import logging
from typing import Any, Mapping, Optional

from requests import RequestException, Response, Session
from requests.adapters import HTTPAdapter
from urllib3 import Retry

logger = logging.getLogger("ms_python_client")

_Headers = Mapping[str, str]
_Data = Mapping[str, Any]


class ApiClient:
def __init__(self, api_base_url: str):
Expand All @@ -22,21 +26,21 @@ def __init__(self, api_base_url: str):
self.session.mount("http://", adapter)
self.session.mount("https://", adapter)

def build_headers(self, extra_headers=None) -> dict:
def build_headers(self, extra_headers: Optional[_Headers] = None) -> dict:
"""Create the headers for a request appending the ones in the params
Args:
extra_headers (dict): Dict of headers that will be appended to the default ones
extra_headers (dict): Mapping of headers that will be appended to the default ones
Returns:
dict: All the headers
"""
headers = {"Content-type": "application/json"}
headers: dict[str, str] = {}
if extra_headers:
headers.update(extra_headers)
return headers

def make_get_request(self, api_path: str, headers: dict) -> Response:
def make_get_request(self, api_path: str, headers: _Headers) -> Response:
"""Makes a GET request using requests
Args:
Expand All @@ -48,7 +52,7 @@ def make_get_request(self, api_path: str, headers: dict) -> Response:
"""
response = None
full_url = self.api_base_url + api_path
logger.debug("GET %s", api_path)
logger.info("GET %s", api_path)
try:
response = self.session.get(full_url, headers=headers, timeout=self.timeout)
response.raise_for_status()
Expand All @@ -65,23 +69,25 @@ def make_get_request(self, api_path: str, headers: dict) -> Response:
)
return response

def make_post_request(self, api_path: str, headers=None, data=None) -> Response:
def make_post_request(
self, api_path: str, headers: _Headers, json: Optional[_Data] = None
) -> Response:
"""Makes a POST request using requests
Args:
api_path (str): The URL path
headers (dict): The headers of the request
data (dict): The body of the request
json (dict): The body of the request
Returns:
Response: The response of the request
"""
response = None
full_url = self.api_base_url + api_path
logger.debug("POST %s", api_path)
logger.info("POST %s", api_path)
try:
response = self.session.post(
full_url, headers=headers, data=data, timeout=self.timeout
full_url, headers=headers, json=json, timeout=self.timeout
)
response.raise_for_status()
except RequestException as e:
Expand All @@ -97,23 +103,25 @@ def make_post_request(self, api_path: str, headers=None, data=None) -> Response:
)
return response

def make_patch_request(self, api_path: str, headers=None, data=None) -> Response:
def make_patch_request(
self, api_path: str, headers: _Headers, json: Optional[_Data] = None
) -> Response:
"""Makes a PATCH request using requests
Args:
api_path (str): The URL path
headers (dict): The headers of the request
data (dict): The body of the request
json (dict): The body of the request
Returns:
Response: The response of the request
"""
response = None
full_url = self.api_base_url + api_path
logger.debug("PATCH %s", api_path)
logger.info("PATCH %s", api_path)
try:
response = self.session.patch(
full_url, headers=headers, data=data, timeout=self.timeout
full_url, headers=headers, json=json, timeout=self.timeout
)
response.raise_for_status()
except RequestException as e:
Expand All @@ -129,23 +137,25 @@ def make_patch_request(self, api_path: str, headers=None, data=None) -> Response
)
return response

def make_delete_request(self, api_path: str, headers=None, data=None) -> Response:
def make_delete_request(
self, api_path: str, headers: _Headers, json: Optional[_Data] = None
) -> Response:
"""Makes a DELETE request using requests
Args:
api_path (str): The URL path
headers (dict): The headers of the request
data (dict): The body of the request
json (dict): The body of the request
Returns:
Response: The response of the request
"""
response = None
full_url = self.api_base_url + api_path
logger.debug("DELETE %s", api_path)
logger.info("DELETE %s", api_path)
try:
response = self.session.delete(
full_url, headers=headers, data=data, timeout=self.timeout
full_url, headers=headers, json=json, timeout=self.timeout
)
response.raise_for_status()
except RequestException as e:
Expand Down
71 changes: 48 additions & 23 deletions ms_python_client/components/events/cern_events_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ def __init__(self, client: MSClientInterface) -> None:
self.events_component = EventsComponent(client)

def list_events(
self, user_id: str, parameters: Optional[Mapping[str, str]] = None
self,
user_id: str,
parameters: Optional[Mapping[str, str]] = None,
extra_headers: Optional[Mapping[str, str]] = None,
) -> dict:
"""List all the events of a user
Expand All @@ -39,34 +42,44 @@ def list_events(
Returns:
dict: The response of the request
"""
return self.events_component.list_events(user_id, parameters)
return self.events_component.list_events(user_id, parameters, extra_headers)

def get_event_by_indico_id(self, user_id: str, indico_id: str) -> dict:
def get_event_by_zoom_id(
self,
user_id: str,
zoom_id: str,
extra_headers: Optional[Mapping[str, str]] = None,
) -> dict:
"""Get an event of a user
Args:
indico_id (str): The event id
zoom_id (str): The event id
Returns:
dict: The response of the request
"""
parameters = {"$count": "true", "$filter": f"contains(subject,'{indico_id}')"}
response = self.events_component.list_events(user_id, parameters)
parameters = {"$count": "true", "$filter": f"contains(subject,'{zoom_id}')"}
response = self.events_component.list_events(user_id, parameters, extra_headers)

count = response.get("@odata.count", 0)
if count == 0:
raise NotFoundError(f"Event with indico id {indico_id} not found")
raise NotFoundError(f"Event with zoom id {zoom_id} not found")

if count > 1:
logger.warning(
"Found %s events with indico id %s. Returning the first one.",
"Found %s events with zoom id %s. Returning the first one.",
count,
indico_id,
zoom_id,
)

return response.get("value", [])[0]

def create_event(self, user_id: str, event: EventParameters) -> dict:
def create_event(
self,
user_id: str,
event: EventParameters,
extra_headers: Optional[Mapping[str, str]] = None,
) -> dict:
"""Create an event for a user
Args:
Expand All @@ -76,11 +89,14 @@ def create_event(self, user_id: str, event: EventParameters) -> dict:
Returns:
dict: The response of the request
"""
data = create_event_body(event)
return self.events_component.create_event(user_id, data)

def update_event_by_indico_id(
self, user_id: str, event: PartialEventParameters
json = create_event_body(event)
return self.events_component.create_event(user_id, json, extra_headers)

def update_event_by_zoom_id(
self,
user_id: str,
event: PartialEventParameters,
extra_headers: Optional[Mapping[str, str]] = None,
) -> dict:
"""Update an event for a user
Expand All @@ -91,19 +107,28 @@ def update_event_by_indico_id(
Returns:
dict: The response of the request
"""
data = create_partial_event_body(event)
event_id = self.get_event_by_indico_id(user_id, event["indico_event_id"])["id"]
return self.events_component.update_event(user_id, event_id, data)

def delete_event_by_indico_id(self, user_id: str, indico_id: str) -> None:
json = create_partial_event_body(event)
event_id = self.get_event_by_zoom_id(user_id, event["zoom_id"], extra_headers)[
"id"
]
return self.events_component.update_event(
user_id, event_id, json, extra_headers
)

def delete_event_by_zoom_id(
self,
user_id: str,
zoom_id: str,
extra_headers: Optional[Mapping[str, str]] = None,
) -> None:
"""Delete an event of a user
Args:
user_id (str): The user id
indico_id (str): The event id
zoom_id (str): The event id
Returns:
dict: The response of the request
"""
event_id = self.get_event_by_indico_id(user_id, indico_id)["id"]
self.events_component.delete_event(user_id, event_id)
event_id = self.get_event_by_zoom_id(user_id, zoom_id, extra_headers)["id"]
self.events_component.delete_event(user_id, event_id, extra_headers)
Loading

0 comments on commit 76e6dd3

Please sign in to comment.