Skip to content

Commit

Permalink
Create and use a type alias for valid Endpoint classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
terjekv committed Sep 16, 2023
1 parent 5ae174b commit 5175e69
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
9 changes: 8 additions & 1 deletion cvmfsscraper/http_get_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import re
from datetime import datetime
from enum import Enum
from typing import Any, Dict, List, Optional
from typing import Any, Dict, List, Optional, Union

from pydantic import BaseModel, Field, field_validator, model_validator

Expand Down Expand Up @@ -299,3 +299,10 @@ def __init__(self, path: str, model_class: type[BaseModel]):
"""Initialize the endpoint."""
self.path = path
self.model_class = model_class


# Dynamically creating this list based on the Endpoints enum values
# is not supported by mypy et al, so we have to do it manually.
EndpointClassesType = Union[
GetCVMFSPublished, GetCVMFSRepositoriesJSON, GetCVMFSStatusJSON, GetGeoAPI
]
8 changes: 3 additions & 5 deletions cvmfsscraper/server.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
"""Server class for cvmfs-server-metadata."""

import json
from typing import Dict, List, Union
from typing import Dict, List
from urllib import error, request

from cvmfsscraper.constants import GeoAPIStatus
from cvmfsscraper.http_get_models import (
EndpointClassesType,
Endpoints,
GetCVMFSPublished,
GetCVMFSRepositoriesJSON,
GetCVMFSStatusJSON,
GetGeoAPI,
RepositoryOrReplica,
)
Expand Down Expand Up @@ -213,9 +213,7 @@ def fetch_endpoint(
endpoint: Endpoints,
repo: str = "data",
geoapi_servers: str = GEOAPI_SERVERS,
) -> Union[
GetCVMFSPublished, GetCVMFSRepositoriesJSON, GetCVMFSStatusJSON, GetGeoAPI
]:
) -> EndpointClassesType:
"""Fetch and process a specified URL endpoint.
This function reads the content of a specified URL and ether returns a validated
Expand Down

0 comments on commit 5175e69

Please sign in to comment.