Skip to content

Commit

Permalink
chore: move format iso 8601 to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
thehappydinoa committed Jun 5, 2023
1 parent c33f3f0 commit 0863dca
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 46 deletions.
45 changes: 14 additions & 31 deletions censys/asm/clouds.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,8 @@
"""Interact with the Censys Clouds API."""
import datetime
from typing import Union

from ..common.types import Datetime
from ..common.utils import format_iso8601
from .api import CensysAsmAPI

Since = Union[str, datetime.date, datetime.datetime]


def format_since_date(since: Since) -> str:
"""Formats since date as ISO 8601 format.
Args:
since (Since): Date.
Returns:
str: ISO 8601 formatted date string.
"""
if isinstance(since, (datetime.date, datetime.datetime)):
return since.strftime("%Y-%m-%d")
return since


class Clouds(CensysAsmAPI):
"""Clouds API class."""
Expand All @@ -28,55 +11,55 @@ class Clouds(CensysAsmAPI):

def get_host_counts(
self,
since: Since,
since: Datetime,
) -> dict:
"""Retrieve host counts by cloud.
Hosts found after the date provided in the `since` parameter will be included in the new asset counts.
Args:
since (Since): Date to include hosts from.
since (Datetime): Date to include hosts from.
Returns:
dict: Host count result.
"""
since = format_since_date(since)
since = format_iso8601(since)
return self._get(f"{self.base_path}/hostCounts/{since}")

def get_domain_counts(self, since: Since) -> dict:
def get_domain_counts(self, since: Datetime) -> dict:
"""Retrieve domain counts by cloud.
Args:
since (Since): Date to include domains from.
since (Datetime): Date to include domains from.
Returns:
dict: Domain count result.
"""
since = format_since_date(since)
since = format_iso8601(since)
return self._get(f"{self.base_path}/domainCounts/{since}")

def get_object_store_counts(self, since: Since) -> dict:
def get_object_store_counts(self, since: Datetime) -> dict:
"""Retrieve object store counts by cloud.
Args:
since (Since): Date to include object stores from.
since (Datetime): Date to include object stores from.
Returns:
dict: Object store count result.
"""
since = format_since_date(since)
since = format_iso8601(since)
return self._get(f"{self.base_path}/objectStoreCounts/{since}")

def get_subdomain_counts(self, since: Since) -> dict:
def get_subdomain_counts(self, since: Datetime) -> dict:
"""Retrieve subdomain counts by cloud.
Args:
since (Since): Date to include subdomains from.
since (Datetime): Date to include subdomains from.
Returns:
dict: Subdomain count result.
"""
since = format_since_date(since)
since = format_iso8601(since)
return self._get(f"{self.base_path}/subdomainCounts/{since}")

def get_unknown_counts(self) -> dict:
Expand Down
14 changes: 14 additions & 0 deletions censys/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,17 @@ def format_rfc3339(time: Datetime) -> str:
if isinstance(time, (datetime.date, datetime.datetime)):
time = time.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
return time


def format_iso8601(time: Datetime) -> str:
"""Formats a datetime object into an ISO8601 string.
Args:
time (Datetime): Datetime object to format.
Returns:
str: ISO8601 formatted string.
"""
if isinstance(time, (datetime.date, datetime.datetime)):
return time.strftime("%Y-%m-%d")
return time
15 changes: 0 additions & 15 deletions tests/asm/test_clouds.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import datetime

import responses
from parameterized import parameterized

from ..utils import CensysTestCase
from .utils import V1_URL
from censys.asm.client import AsmClient
from censys.asm.clouds import format_since_date

TEST_COUNT_JSON = {
"totalAssetCount": 0,
Expand All @@ -24,17 +20,6 @@ def setUp(self):
super().setUp()
self.client = AsmClient(self.api_key)

@parameterized.expand(
[
["2021-01-01", "2021-01-01"],
[datetime.date(2021, 1, 1), "2021-01-01"],
[datetime.datetime(2021, 1, 1, 12, 15, 20, 40), "2021-01-01"],
]
)
def test_format_since_date(self, since, actual):
# Actual call/assertions
assert format_since_date(since) == actual

def test_get_host_counts(self):
# Setup response
self.responses.add(
Expand Down
31 changes: 31 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import datetime
import unittest

from parameterized import parameterized

from censys.common.utils import format_iso8601, format_rfc3339


class UtilsTest(unittest.TestCase):
@parameterized.expand(
[
["2021-01-01", "2021-01-01"],
[datetime.date(2021, 1, 1), "2021-01-01T00:00:00.000000Z"],
[
datetime.datetime(2021, 1, 1, 12, 15, 20, 40),
"2021-01-01T12:15:20.000040Z",
],
]
)
def test_format_rfc3339(self, since, actual):
assert format_rfc3339(since) == actual

@parameterized.expand(
[
["2021-01-01", "2021-01-01"],
[datetime.date(2021, 1, 1), "2021-01-01"],
[datetime.datetime(2021, 1, 1, 12, 15, 20, 40), "2021-01-01"],
]
)
def test_format_iso8601(self, since, actual):
assert format_iso8601(since) == actual

0 comments on commit 0863dca

Please sign in to comment.