Skip to content

Commit 6258466

Browse files
Merge pull request #125 from censys/adh/new-v2-endpoints
Add Support for new endpoints
2 parents 8918a2c + 45ffb4a commit 6258466

File tree

22 files changed

+1447
-173
lines changed

22 files changed

+1447
-173
lines changed

.flake8

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
max-line-length = 88
33
exclude =
44
docs,
5-
venv,
5+
.venv,
66
# No need to traverse our git directory
77
.git,
88
# There's no value in checking cache directories

.github/workflows/pypi.yml

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,13 @@ jobs:
1919
with:
2020
python-version: 3.7
2121

22-
- name: Test install
23-
run: |
24-
pip install -e .
25-
2622
- name: Install dependencies
2723
run: |
28-
python -m pip install --upgrade pip
29-
pip install setuptools wheel twine
24+
python -m pip install --upgrade pip poetry
25+
poetry install --no-dev
3026
3127
- name: Build and publish
32-
env:
33-
TWINE_USERNAME: __token__
34-
TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }}
3528
run: |
36-
python setup.py sdist bdist_wheel --universal
37-
twine upload dist/*
29+
poetry build -n
30+
poetry config pypi-token.pypi ${{ secrets.PYPI_TOKEN }}
31+
poetry publish

.github/workflows/python-ci.yml

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,35 @@ jobs:
2020
with:
2121
python-version: "3.x"
2222

23-
- name: Cache pip
23+
- name: Install poetry
24+
run: |
25+
python -m pip install --upgrade pip poetry
26+
poetry config virtualenvs.in-project true
27+
28+
- name: Cache poetry
29+
id: cached-poetry-dependencies
2430
uses: actions/cache@v2
2531
with:
2632
# This path is specific to Ubuntu
27-
path: ~/.cache/pip
33+
path: .venv
2834
# Look to see if there is a cache hit for the corresponding requirements file
29-
key: ${{ runner.os }}-pip-${{ hashFiles('setup.py') }}
30-
restore-keys: |
31-
${{ runner.os }}-pip-
32-
${{ runner.os }}-
35+
key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}
3336

3437
- name: Install dependencies
38+
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
3539
run: |
36-
python -m pip install --upgrade pip
37-
pip install ".[dev]"
40+
poetry install
3841
3942
- name: Lint with flake8
43+
if: startsWith(matrix.os, 'ubuntu')
4044
run: |
41-
# The GitHub editor is 127 chars wide
42-
flake8 --max-line-length=127
45+
poetry run flake8 --max-line-length=127
4346
4447
- name: Type checking with mypy
48+
if: startsWith(matrix.os, 'ubuntu')
4549
run: |
46-
mypy censys
50+
poetry run mypy censys
4751
4852
- name: Test with pytest
4953
run: |
50-
pytest
54+
poetry run pytest

.readthedocs.yml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,22 @@ sphinx:
1111

1212
# Optionally build your docs in additional formats such as PDF
1313
formats:
14-
- epub
1514
- pdf
1615

1716
# Optionally set the version of Python and requirements required to build your docs
1817
python:
19-
version: 3.7
18+
version: "3.8"
2019
install:
2120
- requirements: docs/requirements.txt
2221
- method: pip
2322
path: .
23+
24+
# Set a custom search rank over pages matching a pattern
25+
search:
26+
ranking:
27+
censys.asm.html: 5
28+
usage-asm.html: 5
29+
censys.search.v2.html: 5
30+
usage-v2.html: 5
31+
censys.search.v1.html: -1
32+
usage-v1.html: -1

MANIFEST.in

Lines changed: 0 additions & 1 deletion
This file was deleted.

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,12 @@ All contributions (no matter how small) are always welcome.
7373

7474
## Development
7575

76+
This project uses [poetry](https://python-poetry.org/) for dependency management. Please ensure you have [installed the latest version](https://python-poetry.org/docs/#installation).
77+
7678
```bash
7779
git clone git@github.com:censys/censys-python.git
7880
cd censys-python/
79-
pip install -e ".[dev]"
81+
poetry install
8082
```
8183

8284
## Testing

censys/common/types.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""Common types for the Censys Python SDK."""
2+
import datetime
3+
from typing import Union
4+
5+
Datetime = Union[str, datetime.date, datetime.datetime]

censys/common/utils.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""Common utilities for the Censys Python SDK."""
2+
3+
import datetime
4+
5+
from .types import Datetime
6+
7+
8+
def format_rfc3339(time: Datetime) -> str:
9+
"""Formats a datetime object into an RFC3339 string.
10+
11+
Args:
12+
time (Datetime): Datetime object to format.
13+
14+
Returns:
15+
str: RFC3339 formatted string.
16+
"""
17+
if isinstance(time, (datetime.date, datetime.datetime)):
18+
time = time.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
19+
return time

censys/common/version.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
"""Censys Version."""
2-
__version__ = "2.0.3"
2+
try:
3+
import importlib_metadata
4+
except ImportError:
5+
import importlib.metadata as importlib_metadata # type: ignore
6+
7+
__version__: str = importlib_metadata.version("censys")

censys/search/v2/api.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
"""Base for interacting with the Censys Search API."""
2-
import datetime
32
import os
43
from concurrent.futures import ThreadPoolExecutor, as_completed
5-
from typing import Dict, Iterable, Iterator, List, Optional, Type, Union
4+
from typing import Dict, Iterable, Iterator, List, Optional, Type
65

76
from requests.models import Response
87

@@ -13,6 +12,8 @@
1312
CensysExceptionMapper,
1413
CensysSearchException,
1514
)
15+
from censys.common.types import Datetime
16+
from censys.common.utils import format_rfc3339
1617

1718
Fields = Optional[List[str]]
1819

@@ -69,6 +70,7 @@ def __init__(
6970
self.view_path = f"/{self.INDEX_NAME}/"
7071
self.search_path = f"/{self.INDEX_NAME}/search"
7172
self.aggregate_path = f"/{self.INDEX_NAME}/aggregate"
73+
self.metadata_path = f"/metadata/{self.INDEX_NAME}"
7274

7375
def _get_exception_class( # type: ignore
7476
self, res: Response
@@ -222,7 +224,7 @@ def search(
222224
def view(
223225
self,
224226
document_id: str,
225-
at_time: Optional[Union[str, datetime.date, datetime.datetime]] = None,
227+
at_time: Optional[Datetime] = None,
226228
) -> dict:
227229
"""View document from current index.
228230
@@ -239,9 +241,7 @@ def view(
239241
"""
240242
args = {}
241243
if at_time:
242-
if isinstance(at_time, (datetime.date, datetime.datetime)):
243-
at_time = at_time.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
244-
args["at_time"] = at_time
244+
args["at_time"] = format_rfc3339(at_time)
245245

246246
return self._get(self.view_path + document_id, args)["result"]
247247

@@ -263,3 +263,11 @@ def aggregate(
263263
"""
264264
args = {"q": query, "field": field, "num_buckets": num_buckets}
265265
return self._get(self.aggregate_path, args)["result"]
266+
267+
def metadata(self) -> dict:
268+
"""Get current index metadata.
269+
270+
Returns:
271+
dict: The result set returned.
272+
"""
273+
return self._get(self.metadata_path)["result"]

0 commit comments

Comments
 (0)