Skip to content

The server submodule

This module provides functions to interact with the metadata server web application.

  1. send new dataset metadata to the server
  2. update the resources on the server to match the local versions.

post_metadata(metadata, zenodo, server_resources)

Post the dataset metadata and zenodo metadata to the metadata server.

Parameters:

Name Type Description Default
metadata dict

The dataset metadata dictionary for a dataset

required
zenodo dict

The dataset metadata dictionary for a deposit

required
server_resources MetadataResources

The server resources to be used.

required

Returns:

Type Description
MetadataResponse

See here.

Source code in safedata_validator/server.py
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
def post_metadata(
    metadata: dict, zenodo: dict, server_resources: MetadataResources
) -> MetadataResponse:
    """Post the dataset metadata and zenodo metadata to the metadata server.

    Args:
        metadata: The dataset metadata dictionary for a dataset
        zenodo: The dataset metadata dictionary for a deposit
        server_resources: The server resources to be used.

    Returns:
        See [here][safedata_validator.server.MetadataResources].
    """

    # Get payload
    payload = {"metadata": metadata, "zenodo": zenodo}

    # post the metadata to the server
    return MetadataResponse(
        requests.post(
            f"{server_resources.api}/post_metadata",
            params=server_resources.token,
            json=payload,
            verify=server_resources.ssl_verify,
        )
    )

update_resources(server_resources)

Update the resources on the metadata server.

The metadata server provides the gazetteer, location aliases and any project IDs as part of the safedata R package workflow. The web server also uses those resources internally to provide information. This function is used to post the current resources to an API on the server that is used to refresh those reseources.

Parameters:

Name Type Description Default
server_resources MetadataResources

The server resources to be used.

required

Returns:

Type Description
MetadataResponse

See here.

Source code in safedata_validator/server.py
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
def update_resources(server_resources: MetadataResources) -> MetadataResponse:
    """Update the resources on the metadata server.

    The metadata server provides the gazetteer, location aliases and any project IDs as
    part of the safedata R package workflow. The web server also uses those resources
    internally to provide information. This function is used to post the current
    resources to an API on the server that is used to refresh those reseources.

    Args:
        server_resources: The server resources to be used.

    Returns:
        See [here][safedata_validator.server.MetadataResources].
    """

    # Get payload
    files = {
        "gazetteer": open(server_resources.resources.gaz_path, "rb"),
        "location_aliases": open(server_resources.resources.localias_path, "rb"),
    }

    if server_resources.resources.project_database is not None:
        files["project_database"] = open(
            server_resources.resources.project_database, "rb"
        )

    # post the resource files to the server
    return MetadataResponse(
        requests.post(
            f"{server_resources.api}/update_resources",
            params=server_resources.token,
            files=files,
        )
    )

MetadataResponse dataclass

Metadata server response processor.

This dataclass is a processor around requests.Response objects from calls to a metadata server. If the response is successful, it parses the returned data payload; otherwise it formats as much information as possible into an error message.

Source code in safedata_validator/server.py
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
@dataclass
class MetadataResponse:
    """Metadata server response processor.

    This dataclass is a processor around `requests.Response` objects from calls to a
    metadata server. If the response is successful, it parses the returned data payload;
    otherwise it formats as much information as possible into an error message.
    """

    response: InitVar[requests.Response]
    """The incoming response from a Zenodo API call."""
    ok: bool = field(init=False)
    """Was the response ok."""
    status_code: int = field(init=False)
    """The status code returned by the response."""
    json_data: dict = field(init=False, default_factory=lambda: dict())
    """The JSON data payload from a successful response."""
    error_message: str | None = field(init=False, default=None)
    """A formatted error message from a failed response."""

    def __post_init__(self, response: requests.Response) -> None:
        """Populate the ZenodoResponse object."""
        # Basic status
        self.ok = response.ok
        self.status_code = response.status_code
        # Now either populate json data or the error message
        if self.ok:
            self.json_data = response.json()
        else:
            self.error_message = response.text

__post_init__(response)

Populate the ZenodoResponse object.

Source code in safedata_validator/server.py
60
61
62
63
64
65
66
67
68
69
def __post_init__(self, response: requests.Response) -> None:
    """Populate the ZenodoResponse object."""
    # Basic status
    self.ok = response.ok
    self.status_code = response.status_code
    # Now either populate json data or the error message
    if self.ok:
        self.json_data = response.json()
    else:
        self.error_message = response.text

error_message = field(init=False, default=None) class-attribute instance-attribute

A formatted error message from a failed response.

json_data = field(init=False, default_factory=(lambda: dict())) class-attribute instance-attribute

The JSON data payload from a successful response.

ok = field(init=False) class-attribute instance-attribute

Was the response ok.

status_code = field(init=False) class-attribute instance-attribute

The status code returned by the response.

MetadataResources dataclass

Packaging for Metadata resources.

This dataclass is used to package the Metadata server specific elements of the configuration.

Source code in safedata_validator/server.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
@dataclass
class MetadataResources:
    """Packaging for Metadata resources.

    This dataclass is used to package the Metadata server specific elements of the
    configuration.
    """

    resources: Resources
    """A safedata_validator resources instance."""
    api: str = field(init=False)
    """The configured Zenodo API to be used."""
    token: dict[str, str] = field(init=False)
    """A dictionary providing the authentication token for the API."""

    def __post_init__(self) -> None:
        """Populate the post init attributes."""

        # Get the appropriate API and token
        self.api = self.resources.metadata.api
        self.token = {"access_token": self.resources.metadata.token}
        self.ssl_verify = self.resources.metadata.ssl_verify

__post_init__()

Populate the post init attributes.

Source code in safedata_validator/server.py
31
32
33
34
35
36
37
def __post_init__(self) -> None:
    """Populate the post init attributes."""

    # Get the appropriate API and token
    self.api = self.resources.metadata.api
    self.token = {"access_token": self.resources.metadata.token}
    self.ssl_verify = self.resources.metadata.ssl_verify

api = field(init=False) class-attribute instance-attribute

The configured Zenodo API to be used.

resources instance-attribute

A safedata_validator resources instance.

token = field(init=False) class-attribute instance-attribute

A dictionary providing the authentication token for the API.