Metadata-Version: 2.4
Name: combocurve-api-helper
Version: 2.7.0
Summary: A library mapped to ComboCurve's API.
Author-email: "David S. Fulford" <petbox.dev@gmail.com>
Maintainer-email: "David S. Fulford" <petbox.dev@gmail.com>
License-Expression: MIT
Project-URL: Homepage, https://github.com/petbox-dev/combocurve-api-helper.git
Project-URL: Repository, https://github.com/petbox-dev/combocurve-api-helper.git
Project-URL: Issues, https://github.com/petbox-dev/combocurve-api-helper/issues
Keywords: combocurve-api-helper,combocurve,cc,api
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Science/Research
Classifier: Intended Audience :: Education
Classifier: Intended Audience :: Developers
Classifier: Natural Language :: English
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering
Classifier: Topic :: Scientific/Engineering :: Mathematics
Classifier: Topic :: Software Development :: Libraries
Classifier: Typing :: Typed
Requires-Python: >=3.9.13
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: more-itertools
Requires-Dist: pydantic<3,>=2
Requires-Dist: pyjwt[crypto]>=2
Requires-Dist: requests
Requires-Dist: typing-extensions
Requires-Dist: urllib3
Dynamic: license-file

# ComboCurve API Helper `combocurve-api-helper`

A utility library mapped to ComboCurve's API.

## Petroleum Engineering Toolbox

---

 [![PyPi Version](https://img.shields.io/pypi/v/combocurve-api-helper.svg "PyPi Version")](https://github.com/petbox-dev/combocurve-api-helper)

 [Open in Visual Studio Code](https://open.vscode.dev/petbox-dev/combocurve-api-helper)

## Features

`combocurve-api-helper` wraps the ComboCurve REST API behind a single
`ComboCurveAPI` entrypoint that composes one mixin per resource area:

- **Projects, scenarios, wells** — list / create / update / delete, plus custom
  columns.
- **Production** — daily and monthly volumes.
- **Forecasts & type curves** — read forecasts, write forecast parameters, and
  `put_forecast_parameters_batched()` for parallel, chunked (25 well x phase per
  request), 207-aware bulk writes that return a `BatchWriteResult` (per-record
  `success_count` / `failed_count` / `ok`, results in original payload order). A
  failed chunk marked `may_have_applied` may still have been written: check the
  server before sending it again.
- **Forecast runs** — submit a forecast run as an async job and poll its status.
- **Econ models** — CREATE / UPDATE / DELETE for econ-model types (project
  per-type and generic; company generics), plus an exact, invertible
  API <-> CSV column mapping for 11 econ-model types.
- **Econ-model assignments** — assign / unassign models to wells per scenario
  qualifier, and read the scenario assignment grid.
- **Lookup tables** — scenario, type-curve, and scenario-assignment CRUD.
- **Econ runs** — trigger scenario economics and read results.
- **Directional** — directional survey access.
- **Counts** — a `count_*` method for every list route returns the number of
  matching documents from a HEAD request, without fetching them.
- **Resilient transport** — a request timeout, and automatic retry with backoff on
  HTTP 429 (honoring `Retry-After`). A GET or HEAD is also retried after a transient
  gateway error (502 / 503 / 504) or a lost connection; a write is sent again only when
  it never reached the server, so it is never applied twice.

Method docstrings carry an `Example response:` block and a link to the matching
`docs.api.combocurve.com` operation (see [Docstring examples](#docstring-examples)).
See [CHANGELOG.md](CHANGELOG.md) for release history.

### Installation

Install from Python package repository:

```bash
python -m pip install combocurve-api-helper
```

or install directly from GitHub:

```bash
python -m pip install git+https://github.com/petbox-dev/combocurve-api-helper.git@main
```

### Setup

Create a credential on your company's **API & Sync** page in ComboCurve and download the ZIP
(the download link works once). Unzip it into `~/.combocurve`. The files can sit directly in
`~/.combocurve` or in the `combocurve/` folder the ZIP creates:

- `api-key.txt`
- `api-credentials.json` (OAuth client credentials, the current type), or
  `api-service-account-key.json` (the legacy service account)

```python
from combocurve_api_helper import ComboCurveAPI

api = ComboCurveAPI()  # reads ~/.combocurve
api = ComboCurveAPI.from_credentials_directory('path/to/unzipped/download')  # any other directory
```

Each download has its own API key; keep `api-key.txt` with the credential file it came with.
OAuth tokens are fetched from ComboCurve and cached for their lifetime of one hour. A refused token
request raises `ComboCurveAuthError`. Example files are in `./config-examples/`.

`api-credentials.example.json`:
```json
{
  "type": "combocurve_oauth2_client",
  "client_id": "<client_id>",
  "client_secret": "<client_secret>",
  "token_uri": "https://api.combocurve.com/v1/oauth/token",
  "audience": "https://api.combocurve.com"
}
```

#### Older layout (deprecated, removed in a 3.x release, not before 2027-01-31)

Before 2.5.0 the package read `~/.combocurve/combocurve.json` (the credential JSON, renamed) and
`~/.combocurve/cc-api.config.json` (the API key wrapped in JSON), and
`ComboCurveAPI.from_alternate_config(combocurve_json_path, cc_api_config_json_path)` read them from
another directory. Both still work, with a `DeprecationWarning`, and `combocurve.json` may now hold
either credential type.

<br>

`cc-api.config.example.json`:
```json
{
    "apikey": "<apikey>"
}
 ```

<br>

`combocurve.example.json`:
```json
{
  "type": "service_account",
  "project_id": "beta-combocurve",
  "private_key_id": "<private_key_id>",
  "private_key": "<private_key>",
  "client_email": "<client_email>",
  "client_id": "<client_id>",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "<client_x509_cert_url>"
}

 ```

## Usage

```python
from combocurve_api_helper import ComboCurveAPI

# Credentials are read from ~/.combocurve (see Setup above)
api = ComboCurveAPI()

# List projects, then scenarios and forecasts within one
projects = api.get_projects(filters={'name': 'Example Project'})
project_id = api.extract_id(projects)

scenarios = api.get_scenarios(project_id)
forecasts = api.get_forecasts(project_id)

# Bulk-write forecast parameters in parallel: chunked and 207-aware
result = api.put_forecast_parameters_batched(project_id, forecast_id, records)
if not result.ok:
    print(f'{result.failed_count} of {len(result.results)} records failed')
```

### Econ models to / from CSV

Each econ-model type has an invertible mapper looked up by its PascalCase `econModelType`.
The library exposes per-type convenience functions that convert a whole **multi-model** CSV
(the shape ComboCurve exports) directly to and from lists of API dicts, so no `csv` boilerplate
is needed. `to_csv`/`from_csv` work on strings; `read_csv`/`write_csv` (on the mapper) work on
file paths.

```python
from combocurve_api_helper import ComboCurveAPI
from combocurve_api_helper.econ_models import (
    expenses_to_csv,
    expenses_from_csv,
    get_expenses_mapper,
)

api = ComboCurveAPI()

# --- ComboCurve -> CSV file ---
models = [api.get_expenses_model_by_id(project_id, model_id)]
get_expenses_mapper().write_csv('expenses.csv', models)

# --- CSV file -> ComboCurve ---
payloads = get_expenses_mapper().read_csv('expenses.csv')  # list[dict], one per model
api.post_expenses_models(project_id, payloads)  # or put_expenses_models(...)

# --- In-memory string round trip ---
csv_text = expenses_to_csv(models)
same_models = expenses_from_csv(csv_text)
```

For finer-grained, per-model control, drop to the row level. `to_row_dicts` flattens a
**single** model to its CSV-column-keyed row dicts (no header, no file); `from_row_dicts` rebuilds
one model from rows. Use these when you want to drive the `csv` writer yourself, edit or filter
individual rows, or handle models one at a time. The whole-file helpers above are built on them,
and `mapper.columns` is the exact CSV header.

```python
import csv
from combocurve_api_helper import ComboCurveAPI
from combocurve_api_helper.econ_models import get_expenses_mapper

api = ComboCurveAPI()
mapper = get_expenses_mapper()
model = api.get_expenses_model_by_id(project_id, model_id)  # one Expenses econ-model API dict

# --- one model -> row dicts -> your own CSV file ---
with open('expenses.csv', 'w', newline='') as f:
    writer = csv.DictWriter(f, fieldnames=mapper.columns)  # columns == the CSV header
    writer.writeheader()
    writer.writerows(mapper.to_row_dicts(model))  # one model's line-item rows

# --- row dicts -> one model ---
with open('expenses.csv', newline='') as f:
    rows = list(csv.DictReader(f))
payload = mapper.from_row_dicts(rows)  # reconstruct a single API payload
```

## Contributing

1. Fork the repository:
    - On this GitHub page, click "Fork" to create a copy under your own account.

2. Clone the forked repo onto on your machine:
    ```sh
    git clone https://github.com/<your-username>/combocurve-api-helper.git
    cd combocurve-api-helper
    ```

3. Set the upstream remote:
    - This allows you to fetch updates from the original repository:

    ```sh
    git remote add upstream https://github.com/petbox-dev/combocurve-api-helper.git
    ```

4. Install the package in editable mode, with the `dev` dependency group:

    ```sh
    make install
    ```

    `make install` runs `pip install -e . --group dev --config-settings editable_mode=compat`.
    `--group` needs pip 25.1 or later.

5. Create a new branch:
    - Always create a new branch for your feature or fix
    - A common convention is to name the branch your GitHub username, a forward slash, and a brief description of the work you're doing

    ```sh
    git checkout -b <your-username>/<your-branch-name>
    ```

6. Make your changes, and commit:
    - After adding your files, or making edits, ensure the gate succeeds, then commit your changes

    ```sh
    make test

    git add .
    git commit -m "<description of changes>"
    ```

    `make test` runs `ruff check`, `ruff format --check` and `mypy` over `src tests scripts`, then `pytest`.
    Without `make` (Windows PowerShell or CMD), run `scripts/test.ps1` or `scripts/test.bat`; they run the same checks.

7. Push to your fork:

    ```sh
    git push origin <the-name-of-your-branch>
    ```

8. Create a pull request:

    - Go to ["Pull Requests" tab](https://github.com/petbox-dev/combocurve-api-helper/compare) in this repo, and click "compare across forks"
    - Choose your branch as the source and keep the default `main` branch as the target
        - ie: `petbox-dev/combocurve-api-helper` (`main`) `<-` `your-username/combocurve-api-helper` (`your-branch-name`)
    - Fill the title and description with a summary of the proposed changes
    - Request a review from `@dsfulf`

### Make targets

| Target | Action |
|---|---|
| `make help` | List the targets |
| `make install` | Install editable into the current environment, with the `dev` dependency group |
| `make lint` | `ruff check`, `ruff format --check`, `mypy` over `src tests scripts` |
| `make test` | `make lint`, then `pytest` |
| `make build` | `make clean`, then `python -m build` into an empty `dist/` |
| `make publish` | `make test`, `make build`, then `twine upload dist/*` to PyPI (maintainers only) |
| `make clean` | Remove `build/`, `dist/` and `src/combocurve_api_helper.egg-info/` |

The Makefile uses POSIX shell commands (`rm -rf`, `echo -e`, `column`). On Windows, run it from Git Bash or WSL.

## Docstring examples

The `Example response:` / `Example data:` blocks in method docstrings are
**generated from the ComboCurve Postman collection** — do not edit them by hand.
The collection's `<type>` placeholders (`<string>`, `<number>`, `<boolean>`, …)
are filled with realistic, deterministic spoof values (numbers as numbers, bools
as bools, ISO dates, ObjectId-like ids), so a docstring shows the response's
key/value shape without a live API call. Refresh after the API changes:

```sh
python scripts/generate_docstrings.py          # rewrite in place
python scripts/generate_docstrings.py --check   # verify; exit 1 if stale, 2 if unreachable
```

Each method is matched to its operation by the `docs.api.combocurve.com/api/<slug>`
link in its docstring, where `<slug>` is the collection item name. (The OpenAPI
spec at `storage.googleapis.com/beta-combocurve-api-docs/openapi-spec.yaml` has
*real* example values, but is an older/less-complete snapshot — missing ~52
operations as of 2026-07 — so the collection is used instead.)

## Authors

- David Fulford

## License
MIT License

Copyright (c) 2026 Petroleum Engineering Toolbox

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.
