Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
Upstream reference: c51bfc533f681ae2c21d20b35faff28340a4003e
  • Loading branch information
jwilner authored and benweint committed Nov 7, 2024
0 parents commit bc01d32
Show file tree
Hide file tree
Showing 161 changed files with 18,217 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Upload Python Package to PyPI when a Release is Created

on:
release:
types: [created]

jobs:
pypi-publish:
name: Publish release to PyPI
runs-on: ubuntu-latest
environment:
name: pypi
url: https://pypi.org/p/derapi
permissions:
id-token: write
contents: read
steps:
- uses: actions/checkout@v4
- uses: pypa/hatch@install
- run: hatch build
- uses: pypa/gh-action-pypi-publish@release/v1
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist/
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# changelog

## [0.1.0] - 2024-11-08

### Added

- Initialized the API client code
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License

Copyright (c) 2024-present Derapi (https://derapi.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# derapi

The `derapi` Python SDK provides access to the Derapi API for Python
applications -- fully typed with async support.

[![PyPI - Version](https://img.shields.io/pypi/v/derapi.svg)](https://pypi.org/project/derapi)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/derapi.svg)](https://pypi.org/project/derapi)

-----

## Table of Contents

- [Installation](#installation)
- [Usage](#usage)
- [License](#license)

## Installation

```shell
pip install derapi
```

## Usage

Client initialization:

```python
import os

import httpx
from derapi import AuthenticatedClient

def init_client() -> AuthenticatedClient:
token_resp = httpx.post(
"https://auth.derapi.com/oauth2/token",
auth=(os.environ["DERAPI_CLIENT_ID"], os.environ["DERAPI_CLIENT_SECRET"]),
data={"grant_type": "client_credentials"},
)
token_resp.raise_for_status()
token = token_resp.json()["access_token"]
return AuthenticatedClient(
base_url="https//api.derapi.com",
raise_on_unexpected_status=True,
token=token,
)
```

Usage:

```python
...

from derapi.api import list_sites

client = init_client()

for site in list_sites.sync_depaginated(client=client):
print(site.id)
```

## License

`derapi` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.
8 changes: 8 additions & 0 deletions derapi/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""A client library for accessing Derapi API Reference"""

from .client import AuthenticatedClient, Client

__all__ = (
"AuthenticatedClient",
"Client",
)
1 change: 1 addition & 0 deletions derapi/api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Contains methods for accessing the API"""
Empty file.
158 changes: 158 additions & 0 deletions derapi/api/batteries/get_battery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
from http import HTTPStatus
from typing import Any, Dict, Optional, Union

import httpx

from ... import errors
from ...client import AuthenticatedClient, Client
from ...models.get_battery_response import GetBatteryResponse
from ...types import Response


def _get_kwargs(
id: str,
) -> Dict[str, Any]:
_kwargs: Dict[str, Any] = {
"method": "get",
"url": f"/batteries/{id}",
}

return _kwargs


def _parse_response(
*, client: Union[AuthenticatedClient, Client], response: httpx.Response
) -> Optional[GetBatteryResponse]:
if response.status_code == 200:
response_200 = GetBatteryResponse.from_dict(response.json())

return response_200
if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None


def _build_response(
*, client: Union[AuthenticatedClient, Client], response: httpx.Response
) -> Response[GetBatteryResponse]:
return Response(
status_code=HTTPStatus(response.status_code),
content=response.content,
headers=response.headers,
parsed=_parse_response(client=client, response=response),
)


def sync_detailed(
id: str,
*,
client: Union[AuthenticatedClient, Client],
) -> Response[GetBatteryResponse]:
"""Returns Battery details
Returns details for a single Battery
Args:
id (str): Battery id
Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.
Returns:
Response[GetBatteryResponse]
"""

kwargs = _get_kwargs(
id=id,
)

response = client.get_httpx_client().request(
**kwargs,
)

return _build_response(client=client, response=response)


def sync(
id: str,
*,
client: Union[AuthenticatedClient, Client],
) -> Optional[GetBatteryResponse]:
"""Returns Battery details
Returns details for a single Battery
Args:
id (str): Battery id
Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.
Returns:
GetBatteryResponse
"""

return sync_detailed(
id=id,
client=client,
).parsed


async def asyncio_detailed(
id: str,
*,
client: Union[AuthenticatedClient, Client],
) -> Response[GetBatteryResponse]:
"""Returns Battery details
Returns details for a single Battery
Args:
id (str): Battery id
Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.
Returns:
Response[GetBatteryResponse]
"""

kwargs = _get_kwargs(
id=id,
)

response = await client.get_async_httpx_client().request(**kwargs)

return _build_response(client=client, response=response)


async def asyncio(
id: str,
*,
client: Union[AuthenticatedClient, Client],
) -> Optional[GetBatteryResponse]:
"""Returns Battery details
Returns details for a single Battery
Args:
id (str): Battery id
Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.
Returns:
GetBatteryResponse
"""

return (
await asyncio_detailed(
id=id,
client=client,
)
).parsed
Loading

0 comments on commit bc01d32

Please sign in to comment.