Metadata-Version: 2.4
Name: gemini-tender
Version: 0.2.0
Summary: A Gemini (the protocol) low-level client, implemented in Python
Author-email: Maxime 'Epoc' Gross <contact.nospam@epoc.nospam.fr>
License-Expression: LicenseRef-DBAD
Project-URL: Documentation, https://github.com/EpocDotFr/tender?tab=readme-ov-file#usage
Project-URL: Repository, https://github.com/EpocDotFr/tender
Project-URL: Issues, https://github.com/EpocDotFr/tender/issues
Project-URL: Changelog, https://github.com/EpocDotFr/tender/releases
Keywords: asyncio,gemini-protocol,client,dependency-free
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Topic :: Software Development :: Libraries
Classifier: Topic :: Internet
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Requires-Python: >=3.14
Description-Content-Type: text/markdown
License-File: LICENSE.md
Dynamic: license-file

# Tender

A [Gemini](https://geminiprotocol.net/) (the protocol) low-level client, implemented in Python.

![Python versions](https://img.shields.io/pypi/pyversions/gemini-tender.svg) ![Version](https://img.shields.io/pypi/v/gemini-tender.svg) ![License](https://img.shields.io/pypi/l/gemini-tender.svg)

[PyPI](https://pypi.org/project/gemini-tender/) - [Documentation](https://github.com/EpocDotFr/tender?tab=readme-ov-file#usage) - [Source code](https://github.com/EpocDotFr/tender) - [Issue tracker](https://github.com/EpocDotFr/tender/issues) - [Changelog](https://github.com/EpocDotFr/tender/releases)

> [!TIP]
> See also: [Harbor](https://github.com/EpocDotFr/harbor), the accompanying server; and [Wayfarer](https://github.com/EpocDotFr/wayfarer), the accompanying [TUI](https://en.wikipedia.org/wiki/Text-based_user_interface) client.

## Prerequisites

  - Python >= 3.14

## Installation

Use your favorite Python package manager to install `gemini-tender`. Example using [Pipenv](https://pipenv.pypa.io/en/latest/):

```shell
pipenv install gemini-tender
```

## Usage

### Setting up the SSL context

The Gemini protocol *requires* usage of TLS >= 1.2. The first thing you thus have to do is to set up an SSL context.

A bare-minimum, Gemini-compliant context can be created and used right away:

```python
import tender

context = tender.create_default_ssl_context()
```

This method creates a preconfigured [ssl.SSLContext](https://docs.python.org/3.14/library/ssl.html#ssl.SSLContext) instance. However,
this context will *not* - by default - validate any certificate and will accept *any* certificate.

#### Using a client certificate

Create a default SSL context, and set your certificates using Python's methods:

```python
import tender

context = tender.create_default_ssl_context()
context.load_cert_chain('cert.pem', 'key.pem')
```

Note that you must of course use one context per identity.

### Sending requests

As simple as (all examples assume you already have an asyncio context):

```python
import tender

context = ...

try:
    response = await tender.request('gemini://geminiprotocol.net/', context)
except ValueError:
    # On invalid URL or malformed response
    ...
except OSError:
    # All other errors
    ...
```

A `tender.Response` instance will be returned.

#### Custom timeout

There's a 5-second timeout by default for the whole request process. You can change that with the third `timeout` argument:

```python
import tender

context = ...

try:
    response = await tender.request('gemini://geminiprotocol.net/', context, timeout=10) # 10 seconds
except TimeoutError: # Note it's an OSError subclass
    # Timeout reached
    ...
except ValueError:
    # On invalid URL or malformed response
    ...
except OSError:
    # All other errors
    ...
```

### Inspecting the response

Several attributes and methods in `tender.Response` are available:

  - `is_failure()` determines whether the status is a permanent or temporary failure
  - `is_input()`, `is_success()`, `is_redirect()`, `is_tempfail()`, `is_permfail()` and `is_auth()` determine the status group
  - `status` is the status as an integer
  - For input responses:
    - `prompt` is the prompt as a string
  - For success responses:
    - `body` is the raw response body as bytes
    - `mime_type` is a `tender.MimeType` instance (read below for more info)
    - `gemtext` may be a `tender.Gemtext` instance if the returned MIME type is `text/gemini`, or `None` instead. Read below for more info
  - For redirect responses:
    - `uri` is the redirect URI as a string
  - For temporary failure, permanent failure and auth responses:
    - `error_msg` is the error message as a string or `None` if there is none

#### Inspecting the response's MIME type

`tender.MimeType` is a named tuple, with the following attributes:

  - `media_type` - a string containing the actual MIME type (e.g. `text/gemini`)
  - `parameters` - a dictionary containing the associated parameters (e.g. `{'charset': 'utf-8', 'lang': 'fr'}`)

#### Reading the parsed gemtext

`tender.Gemtext` exposes the following attributes:

  - `lines` - a list of parsed lines constituting the gemtext document. This list may contain instances of the following named tuples and associated attributes:
    - `tender.Text` (`text` - string)
    - `tender.Link` (`url` - string, `label` - string or `None`)
    - `tender.Heading1` (`text` - string)
    - `tender.Heading2` (`text` - string)
    - `tender.Heading3` (`text` - string)
    - `tender.ListItem` (`text` - string)
    - `tender.Quote` (`text` - string)
    - `tender.Preformatted` (`text` - string, `alt` - string or `None`)
  - `lang` - a list of language codes, parsed from the response's MIME type, or `None` if there's none

> [!NOTE]
> Tender automatically registers the `gemini` scheme in `urllib.parse` upon importing. That will make functions like
> `urljoin()` work as expected.

## Development

### Getting source code and installing the package

Requires pip >= 25.1.

  1. Clone the repository
  2. From the package's root: `pip install --editable . --group dev`

### Releasing the package

  1. `__version__` in `src/tender/__init__.py` must be updated beforehand. It should adhere to [Semantic Versioning](https://semver.org/spec/v2.0.0.html)
  2. Create an associated Git tag (must exactly match the aforementioned version)
  3. `rm -rf dist/ && python -m build`
  4. `twine upload dist/*`
  5. An associated GitHub release must be created following the [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) format
