Skip to content

Commit

Permalink
refactore: names
Browse files Browse the repository at this point in the history
  • Loading branch information
YounesOMK committed Dec 21, 2023
1 parent cc6b9a6 commit 858423c
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 32 deletions.
4 changes: 2 additions & 2 deletions metricheq/connectors/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def apply(self, request):
pass


class ApiTokenAuthenticator(Authenticator):
class BearerTokenAuthenticator(Authenticator):
def __init__(self, token) -> None:
self.token = token

Expand All @@ -17,7 +17,7 @@ def apply(self, request):
return request


class BasicAuthenticator(Authenticator):
class UserPasswordBasicAuthenticator(Authenticator):
def __init__(self, username, password) -> None:
self.username = username
self.password = password
Expand Down
8 changes: 4 additions & 4 deletions metricheq/connectors/git_providers/github.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
from pydantic import BaseModel
import requests

from metricheq.connectors.base import ApiTokenAuthenticator, Client, Connector
from metricheq.connectors.base import BearerTokenAuthenticator, Client, Connector


class GitHubConnectorConfig(BaseModel):
class GitHubConfig(BaseModel):
api_key: str
base_url: str = "https://api.github.com"


class GitHubClient(Client):
def __init__(self, config: GitHubConnectorConfig):
def __init__(self, config: GitHubConfig):
super().__init__(config)
self.authenticator = ApiTokenAuthenticator(config.api_key)
self.authenticator = BearerTokenAuthenticator(config.api_key)
self.base_url = config.base_url

def make_request(self, endpoint: str, method: str = "GET", **kwargs):
Expand Down
20 changes: 11 additions & 9 deletions metricheq/connectors/sonar.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
from typing import Optional
import requests
from metricheq.connectors.base import (
ApiTokenAuthenticator,
BasicAuthenticator,
BearerTokenAuthenticator,
UserPasswordBasicAuthenticator,
Client,
Connector,
)
from pydantic import BaseModel


class SonarConnectorBaseConfig(BaseModel):
class SonarBaseConfig(BaseModel):
host_url: str
proxy: Optional[str] = None


class SonarConnectorApiTokenConfig(SonarConnectorBaseConfig):
class SonarTokenConfig(SonarBaseConfig):
user_token: str


class SonarConnectorBasicConfig(SonarConnectorBaseConfig):
class SonarUserPasswordConfig(SonarBaseConfig):
username: str
password: str

Expand All @@ -28,10 +28,12 @@ def __init__(self, config):
self.base_url = config.host_url
self.proxy = config.proxy

if isinstance(config, SonarConnectorApiTokenConfig):
self.authenticator = ApiTokenAuthenticator(config.user_token)
if isinstance(config, SonarConnectorBasicConfig):
self.authenticator = BasicAuthenticator(config.username, config.password)
if isinstance(config, SonarTokenConfig):
self.authenticator = BearerTokenAuthenticator(config.user_token)
if isinstance(config, SonarUserPasswordConfig):
self.authenticator = UserPasswordBasicAuthenticator(
config.username, config.password
)
self.base_url = config.host_url
else:
return TypeError("Unsupported configuration type.")
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions metricheq/fetchers/base.py → metricheq/extractors/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from metricheq.connectors.base import Connector


class Fetcher(ABC):
class Extractor(ABC):
def __init__(self, connector: Connector, params: dict):
self.connector = connector
self.client = connector.client
Expand All @@ -25,7 +25,7 @@ def process_data(self, data):
def finalize(self, processed_data):
pass

def execute(self):
def perform(self):
self.connector.ensure_connectivity()
data = self.fetch_data()
processed_data = self.process_data(data)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pydantic import BaseModel
from metricheq.fetchers.base import Fetcher
from metricheq.fetchers.utils import DurationFormat
from metricheq.connectors.git_providers.github import GitHubConnector
from metricheq.extractors.base import Extractor
from metricheq.extractors.utils import DurationFormat
from metricheq.connectors.base import Connector


Expand All @@ -20,8 +21,11 @@ class GitProviderLastCommitFreshnessParams(BaseModel):
format: DurationFormat = DurationFormat.SECONDS


class GitProviderFileExistsFetcher(Fetcher):
class GitProviderFileExistsExtractor(Extractor):
def __init__(self, connector: Connector, params: dict):
if not isinstance(connector, GitHubConnector):
raise TypeError("The provided connector is not a valid github connector")

self.params_model = GitProviderFileExistsParams(**params)
super().__init__(connector, params)

Expand All @@ -30,8 +34,10 @@ def friendly_name(self):
return "existence of file in git repository"


class GitProviderLastWorkFlowDurationFetcher(Fetcher):
class GitProviderLastWorkFlowDurationExtractor(Extractor):
def __init__(self, connector: Connector, params: dict):
if not isinstance(connector, GitHubConnector):
raise TypeError("The provided connector is not a valid github connector")
self.params_model = GitProviderLastWorkflowDurationParams(**params)
super().__init__(connector, params)

Expand All @@ -40,8 +46,10 @@ def friendly_name(self):
return "last Git workflow duration"


class GitProviderLastCommitFreshnessFetcher(Fetcher):
class GitProviderLastCommitFreshnessExtractor(Extractor):
def __init__(self, connector: Connector, params: dict):
if not isinstance(connector, GitHubConnector):
raise TypeError("The provided connector is not a valid github connector")
self.params_model = GitProviderLastCommitFreshnessParams(**params)
super().__init__(connector, params)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from datetime import datetime, timezone
from metricheq.fetchers.git_providers.base import (
GitProviderFileExistsFetcher,
GitProviderLastCommitFreshnessFetcher,
GitProviderLastWorkFlowDurationFetcher,
from metricheq.extractors.git_providers.base import (
GitProviderFileExistsExtractor,
GitProviderLastCommitFreshnessExtractor,
GitProviderLastWorkFlowDurationExtractor,
)
from metricheq.fetchers.utils import DurationFormat, convert_seconds
from metricheq.extractors.utils import convert_seconds


class GitHubFileExistsFetcher(GitProviderFileExistsFetcher):
class GitHubFileExistsExtractor(GitProviderFileExistsExtractor):
def fetch_data(self):
endpoint = f"/repos/{self.params_model.repo_name}/contents/{self.params_model.file_path}"
return self.client.make_request(endpoint)
Expand All @@ -19,7 +19,7 @@ def finalize(self, file_exists) -> bool:
return file_exists


class GitHubLastWorkFlowDurationFetcher(GitProviderLastWorkFlowDurationFetcher):
class GitHubLastWorkFlowDurationExtractor(GitProviderLastWorkFlowDurationExtractor):
def fetch_data(self):
endpoint = f"/repos/{self.params_model.repo_name}/actions/runs"
return self.client.make_request(endpoint)
Expand All @@ -44,7 +44,7 @@ def finalize(self, duration_in_seconds: float):
return convert_seconds(duration_in_seconds, self.params_model.format)


class GitHubLastCommitFreshnessFetcher(GitProviderLastCommitFreshnessFetcher):
class GitHubLastCommitFreshnessExtractor(GitProviderLastCommitFreshnessExtractor):
def fetch_data(self):
endpoint = f"/repos/{self.params_model.repo_name}/commits/{self.params_model.branch_name}"
return self.client.make_request(endpoint)
Expand Down
7 changes: 5 additions & 2 deletions metricheq/fetchers/sonar.py → metricheq/extractors/sonar.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from enum import Enum
from pydantic import BaseModel
from metricheq.connectors.base import Connector
from metricheq.connectors.sonar import SonarConnector

from metricheq.fetchers.base import Fetcher
from metricheq.extractors.base import Extractor


class SonarMetricType(str, Enum):
Expand All @@ -17,8 +18,10 @@ class SonarMeasuresParams(BaseModel):
metric_key: SonarMetricType


class SonarMeasuresFetcher(Fetcher):
class SonarMeasuresFetcher(Extractor):
def __init__(self, connector: Connector, params: dict):
if not isinstance(connector, SonarConnector):
raise TypeError("The provided connector is not a valid sonar connector")
self.params_model = SonarMeasuresParams(**params)
super().__init__(connector, params)

Expand Down
File renamed without changes.

0 comments on commit 858423c

Please sign in to comment.