API Reference: CrossClient
Source code in src/crosscontract/crossclient/crossclient.py
11 12 13 14 15 16 17 18 19 20 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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 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 134 135 136 137 138 139 140 141 142 143 144 145 146 | |
__init__(username, password, base_url=DEFAULT_URL, verify=True)
Initialize the client with authentication.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
username
|
str
|
The username for authentication. |
required |
password
|
str
|
The password for authentication. |
required |
base_url
|
str
|
If provided, use this domain instead of the default DEFAULT_URL. The domain must include the protocol (e.g., http:// or https://). Example: "http://example.com". Defaults to DEFAULT_URL: "https://backend.sweet-cross.ch". Trailing slashes are stripped internally. |
DEFAULT_URL
|
verify
|
bool
|
Whether to verify SSL certificates. Defaults to True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
CrossClient |
None
|
An instance of the authenticated client. |
Source code in src/crosscontract/crossclient/crossclient.py
authenticate()
Authenticate with the server and retrieve an access token.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The authentication token. |
Source code in src/crosscontract/crossclient/crossclient.py
close()
delete(endpoint, **kwargs)
Send a DELETE request to the specified endpoint.
get(endpoint, **kwargs)
patch(endpoint, json=None, **kwargs)
Send a PATCH request to the specified endpoint.
post(endpoint, json=None, **kwargs)
Send a POST request to the specified endpoint.
request(method, endpoint, **kwargs)
Send an HTTP request to the specified endpoint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str
|
The HTTP method (e.g., 'GET', 'POST'). |
required |
endpoint
|
str
|
The API endpoint to send the request to. |
required |
**kwargs
|
Any
|
Additional arguments to pass to the request. |
{}
|
Returns:
| Type | Description |
|---|---|
Response
|
httpx.Response: The response from the server. |
Source code in src/crosscontract/crossclient/crossclient.py
Entry point for operations on the collection of contracts.
Source code in src/crosscontract/crossclient/services/contract_service.py
15 16 17 18 19 20 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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | |
__init__(client)
Initialize the ContractService. The ContractService is responsible for managing contracts on the CROSS platform. It provides methods to create, retrieve, list, and delete contracts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
CrossClient
|
The CrossClient instance to use for API calls. |
required |
Source code in src/crosscontract/crossclient/services/contract_service.py
change_status(name, status)
Change the status of a contract. Allowable status transitions are enforced by the CROSS platform. The allowable statuses are: 1. Draft 2. Active 3. Suspended 4. Retired Allowed transitions: - Draft -> Active - Active -> Suspended - Suspended -> Active - Active -> Retired - Suspended -> Retired
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the contract to change status. |
required |
status
|
Literal['Draft', 'Active', 'Suspended', 'Retired']
|
The new status for the contract. |
required |
Raises:
| Type | Description |
|---|---|
HTTPStatusError
|
If the request fails. |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The updated status of the contract. |
Source code in src/crosscontract/crossclient/services/contract_service.py
create(contract, activate=False)
Create a new contract on the CROSS platform
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
contract
|
CrossContract
|
The contract data to create. |
required |
activate
|
bool
|
Whether to activate the contract upon creation. Defaults to False. |
False
|
Raises:
| Type | Description |
|---|---|
HTTPStatusError
|
If the request fails. |
Returns:
| Name | Type | Description |
|---|---|---|
ContractResource |
ContractResource
|
The created contract object. |
Source code in src/crosscontract/crossclient/services/contract_service.py
delete(name, hard=False)
Delete a contract by name if it exists. A contract can only be deleted if: 1. Contract is in "Draft" status 2. Contract status is "Retired" and the data associated with the contract is deleted
If hard is set to True, the contract and all associated data will be deleted.
Note: This is a dangerous operation and should be used with caution. Usually,
admin rights are required.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the contract to delete. |
required |
hard
|
bool
|
Whether to perform a hard delete (including data). Note: This is a dangerous operation and should be used with caution. it will delete all data associated with the contract. Usually, admin rights are required. |
False
|
Source code in src/crosscontract/crossclient/services/contract_service.py
get(name)
Get contract from the CROSS platform by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the contract. |
required |
Raises:
| Type | Description |
|---|---|
HTTPStatusError
|
If the request fails. |
Returns:
| Name | Type | Description |
|---|---|---|
ContractResource |
ContractResource
|
The contract resource object. |
Source code in src/crosscontract/crossclient/services/contract_service.py
get_list()
Lists all available contracts as ContractResource objects.
Returns:
| Type | Description |
|---|---|
dict[str, ContractResource]
|
dict[str, ContractResource]: Dictionary of contract resources keyed by contract name. |
Source code in src/crosscontract/crossclient/services/contract_service.py
overview()
Get a DataFrame with an overview of all contracts, their status, and metadata.
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: DataFrame containing contract overviews. |
Source code in src/crosscontract/crossclient/services/contract_service.py
A contract that is related to contract on the CROSS platform.
ContractResources are read-only wrappers around the actual contract data that is stored on the CROSS platform. They provide lazy loading of the contract details and methods to interact with the contract, such as adding data.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
The name of the contract. |
status |
str
|
The status of the contract. |
contract |
CrossContract
|
The full contract details. |
service |
ContractService
|
The ContractService instance used for API calls. |
Source code in src/crosscontract/crossclient/services/contract_resource.py
14 15 16 17 18 19 20 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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 | |
contract
property
The full contract details as a CrossContract object.
This property uses lazy loading to fetch the contract details from the CROSS platform only when accessed for the first time.
Returns:
| Name | Type | Description |
|---|---|---|
CrossContract |
CrossContract
|
The full contract details. |
__init__(service, status, name=None, contract=None)
Initialize the ContractResource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
service
|
ContractService
|
The ContractService instance to use for API calls. |
required |
name
|
str | None
|
The name of the contract. Required if contract is not provided. |
None
|
contract
|
CrossContract | None
|
The CrossContract instance. If not provided, the contract details will be fetched lazily when accessed. |
None
|
Source code in src/crosscontract/crossclient/services/contract_resource.py
add_data(data, validate=True)
Add data for the contract on in the CROSS platform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
The data to be added. |
required |
validate
|
bool
|
Whether to validate the data against the contract schema before uploading. Defaults to True. |
True
|
Raises:
| Type | Description |
|---|---|
validationError
|
If the data does not conform to the contract schema. |
Source code in src/crosscontract/crossclient/services/contract_resource.py
change_status(status)
Change the status of the contract.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
status
|
Literal['Draft', 'Active', 'Suspended', 'Retired']
|
The new status for the contract. |
required |
Source code in src/crosscontract/crossclient/services/contract_resource.py
drop_data()
get_data(columns=None, filters=None, unique=False)
Get data for the contract from the CROSS platform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
columns
|
list[str] | None
|
Optional list of columns to retrieve. If None, all columns are retrieved. |
None
|
filters
|
dict[str, str] | None
|
Optional dictionary of filters to apply. The keys are column names and the values are the filter values. Currently, only equality filters are supported and only one value per filter. |
None
|
unique
|
bool
|
Whether to return only unique rows. |
False
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: The data associated with the contract. |
Source code in src/crosscontract/crossclient/services/contract_resource.py
get_foreign_key_values()
Get the existing foreign key values for the contract from the CROSS platform. This is needed if you want to perform foreign key validation including existing values, i.e., to ensure referential integrity of the foreign keys across both existing and new data.
Returns:
| Type | Description |
|---|---|
dict[tuple, list[tuple]] | None
|
dict[tuple, list[tuple]] | None: A dictionary where the keys are tuples representing the foreign key fields, and the values are lists of tuples representing the existing foreign key values. Returns None if the contract does not have foreign keys defined or if there are no existing foreign key values. |
Source code in src/crosscontract/crossclient/services/contract_resource.py
get_primary_key_values()
Get the existing primary key values for the contract from the CROSS platform. This is needed if you want to perform primary key validation including existing values, i.e., to ensure uniqueness of the primary key across both existing and new data.
Returns:
| Type | Description |
|---|---|
list[tuple] | None
|
list[tuple]: A list of tuples representing the existing primary key values. |
Returns:
| Type | Description |
|---|---|
list[tuple] | None
|
list[tuple] | None: A list of tuples representing the existing primary key values. Returns None if the contract does not have a primary key defined or if there are no existing primary key values. |
Source code in src/crosscontract/crossclient/services/contract_resource.py
refresh()
Fetch the full contract details from the CROSS platform.
Source code in src/crosscontract/crossclient/services/contract_resource.py
validate_dataframe(df, skip_primary_key_validation=True, skip_foreign_key_validation=True, lazy=True)
Validate a DataFrame against the schema of the contract. It allows to provide existing primary key and foreign key values for validation. If provided, the primary key uniqueness is checked against the union of the existing and the DataFrame values. Similarly, foreign key integrity is checked against the union of existing and DataFrame values in case of self-referencing foreign keys.
The validation is performed including primary key and foreign key checks that may require fetching existing key values from the CROSS platform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
The DataFrame to validate. |
required |
skip_primary_key_validation
|
bool
|
If True, skip primary key validation. Default is False. |
True
|
skip_foreign_key_validation
|
bool
|
If True, skip foreign key validation. Default is False. |
True
|
lazy
|
bool
|
If True, collect all validation errors and raise them together. If False, raise the first validation error encountered. Default is True. |
True
|
Raises:
| Type | Description |
|---|---|
ValidationError
|
If the DataFrame does not conform to the schema. |