Skip to content

Config


pyravelry.settings.RavelrySettings

Bases: BaseSettings

Initializes the settings from which to call the Ravelry API.

Attributes:

Name Type Description
auth_tuple tuple[str, str]

formatted Revelry auth tuple for httpx.

Parameters:

Name Type Description Default
ravelry_auth_username str
required
ravelry_auth_api_key SecretStr
required
base_url HttpUrl
HttpUrl('https://api.ravelry.com/')
Source code in src/pyravelry/settings.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class RavelrySettings(BaseSettings):
    """Initializes the settings from which to call the Ravelry API.

    Attributes:
        ravelry_auth_username (str): username for Ravelry.
        ravelry_auth_api_key (SecretStr): read-only API token for accessing Ravelry.
        auth_tuple (tuple[str, str]): formatted Revelry auth tuple for httpx.
    """

    # Use the 'read-only' credentials from your Ravelry App control panel
    ravelry_auth_username: str = Field(..., alias="RAVELRY_USERNAME")
    ravelry_auth_api_key: SecretStr = Field(..., alias="RAVELRY_API_KEY")

    # The API requires SSL (HTTPS) for all requests
    base_url: HttpUrl = HttpUrl("https://api.ravelry.com")

    # Configuration to read from a .env file
    model_config = SettingsConfigDict(env_file=".env", extra="ignore")

    @property
    def auth_tuple(self) -> tuple[str, str]:
        """Returns credentials in the (username, password) format for httpx."""
        return (
            self.ravelry_auth_username,
            self.ravelry_auth_api_key.get_secret_value(),
        )

pyravelry.settings.RavelrySettings.auth_tuple property

Returns credentials in the (username, password) format for httpx.


pyravelry.client.RavelryClient

Client to get data from the revelry api.

Attributes:

Name Type Description
settings RavelrySettings

Authentication for the Ravelry API.

_http SyncCacheClient

Httpx client with persistent caching

color_families ColorFamiliesResource

Color Families endpoint

fiber_categories FiberCategoriesResource

Fiber Categories endpoint

yarn_weights YarnWeightsResource

Yarn Weights endpoint

search SearchResource

Search endpoint

fiber_attributes FiberAttributesResource

Fiber Attributes endpoint

yarn_companies YarnCompaniesResource

Yarn Companies endpoint

Source code in src/pyravelry/client.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class RavelryClient:
    """Client to get data from the revelry api.

    Attributes:
        settings (RavelrySettings): Authentication for the Ravelry API.
        _http (SyncCacheClient): Httpx client with persistent caching
        color_families (ColorFamiliesResource): Color Families endpoint
        fiber_categories (FiberCategoriesResource): Fiber Categories endpoint
        yarn_weights (YarnWeightsResource): Yarn Weights endpoint
        search (SearchResource): Search endpoint
        fiber_attributes (FiberAttributesResource): Fiber Attributes endpoint
        yarn_companies (YarnCompaniesResource): Yarn Companies endpoint
    """

    def __init__(self, settings: RavelrySettings) -> None:
        """Instantiates a revelry httpx client.

        Args:
            settings (RavelrySettings): Authentication and other settings.
        """
        self.settings = settings
        # Initialize the persistent httpx client with auth
        self._http = SyncCacheClient(
            base_url=str(settings.base_url),
            auth=settings.auth_tuple,
            timeout=10.0,
        )
        self.color_families = ColorFamiliesResource(self._http)
        self.fiber_categories = FiberCategoriesResource(self._http)
        self.yarn_weights = YarnWeightsResource(self._http)
        self.yarn_attributes = YarnAttributesResource(self._http)
        self.search = SearchResource(self._http)
        self.fiber_attributes = FiberAttributesResource(self._http)
        self.yarn_companies = YarnCompaniesResource(self._http)
        self.people = PeopleResource(self._http)
        self.current_user = CurrentUserResource(self._http)

    def close(self) -> None:
        """Closes the httpx client."""
        self._http.close()

    def __enter__(self) -> Self:
        """Method to enter context manager."""
        return self

    def __exit__(
        self,
        exc_type: Optional[type[BaseException]],
        exc_val: Optional[BaseException],
        exc_tb: Optional[TracebackType],
    ) -> None:
        """Method to exit context manager, ensuring the client is closed."""
        self.close()

pyravelry.client.RavelryClient.__enter__()

Method to enter context manager.

Source code in src/pyravelry/client.py
62
63
64
def __enter__(self) -> Self:
    """Method to enter context manager."""
    return self

pyravelry.client.RavelryClient.__exit__(exc_type, exc_val, exc_tb)

Method to exit context manager, ensuring the client is closed.

Source code in src/pyravelry/client.py
66
67
68
69
70
71
72
73
def __exit__(
    self,
    exc_type: Optional[type[BaseException]],
    exc_val: Optional[BaseException],
    exc_tb: Optional[TracebackType],
) -> None:
    """Method to exit context manager, ensuring the client is closed."""
    self.close()

pyravelry.client.RavelryClient.__init__(settings)

Instantiates a revelry httpx client.

Parameters:

Name Type Description Default
settings RavelrySettings

Authentication and other settings.

required
Source code in src/pyravelry/client.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
def __init__(self, settings: RavelrySettings) -> None:
    """Instantiates a revelry httpx client.

    Args:
        settings (RavelrySettings): Authentication and other settings.
    """
    self.settings = settings
    # Initialize the persistent httpx client with auth
    self._http = SyncCacheClient(
        base_url=str(settings.base_url),
        auth=settings.auth_tuple,
        timeout=10.0,
    )
    self.color_families = ColorFamiliesResource(self._http)
    self.fiber_categories = FiberCategoriesResource(self._http)
    self.yarn_weights = YarnWeightsResource(self._http)
    self.yarn_attributes = YarnAttributesResource(self._http)
    self.search = SearchResource(self._http)
    self.fiber_attributes = FiberAttributesResource(self._http)
    self.yarn_companies = YarnCompaniesResource(self._http)
    self.people = PeopleResource(self._http)
    self.current_user = CurrentUserResource(self._http)

pyravelry.client.RavelryClient.close()

Closes the httpx client.

Source code in src/pyravelry/client.py
58
59
60
def close(self) -> None:
    """Closes the httpx client."""
    self._http.close()