|
| 1 | +import logging |
| 2 | +from typing import Any, Dict, Optional |
| 3 | + |
| 4 | +from asgiref.sync import async_to_sync |
| 5 | +from pydantic import Field |
| 6 | + |
| 7 | +from llmstack.apps.schemas import OutputTemplate |
| 8 | +from llmstack.common.utils.prequests import get |
| 9 | +from llmstack.processors.providers.api_processor_interface import ( |
| 10 | + ApiProcessorInterface, |
| 11 | + ApiProcessorSchema, |
| 12 | +) |
| 13 | +from llmstack.processors.providers.metrics import MetricType |
| 14 | + |
| 15 | +logger = logging.getLogger(__name__) |
| 16 | + |
| 17 | + |
| 18 | +class VerifyProcessorInput(ApiProcessorSchema): |
| 19 | + email: str = Field(description="The email to verify") |
| 20 | + mode: Optional[str] = Field(description="The mode to use for verification", default="regular") |
| 21 | + url: Optional[str] = Field( |
| 22 | + description="A webhook target URL specified to receive verification result event in real-time through an HTTP POST request.", |
| 23 | + default=None, |
| 24 | + ) |
| 25 | + |
| 26 | + |
| 27 | +class VerifyProcessorOutput(ApiProcessorSchema): |
| 28 | + response: str = Field(description="The response from the API call as a string", default="") |
| 29 | + response_json: Optional[Dict[str, Any]] = Field( |
| 30 | + description="The response from the API call as a JSON object", default={} |
| 31 | + ) |
| 32 | + response_objref: Optional[str] = Field(description="The reference to the response object", default=None) |
| 33 | + headers: Optional[Dict[str, str]] = Field(description="The headers from the API call", default={}) |
| 34 | + code: int = Field(description="The status code from the API call", default=200) |
| 35 | + size: int = Field(description="The size of the response from the API call", default=0) |
| 36 | + time: float = Field(description="The time it took to get the response from the API call", default=0.0) |
| 37 | + |
| 38 | + |
| 39 | +class VerifyProcessorConfiguration(ApiProcessorSchema): |
| 40 | + pass |
| 41 | + |
| 42 | + |
| 43 | +class EchoProcessor( |
| 44 | + ApiProcessorInterface[VerifyProcessorInput, VerifyProcessorOutput, VerifyProcessorConfiguration], |
| 45 | +): |
| 46 | + """ |
| 47 | + Single email verification processor |
| 48 | + """ |
| 49 | + |
| 50 | + @staticmethod |
| 51 | + def name() -> str: |
| 52 | + return "Email Verification" |
| 53 | + |
| 54 | + @staticmethod |
| 55 | + def slug() -> str: |
| 56 | + return "verify" |
| 57 | + |
| 58 | + @staticmethod |
| 59 | + def description() -> str: |
| 60 | + return "Submit email for verification." |
| 61 | + |
| 62 | + @staticmethod |
| 63 | + def provider_slug() -> str: |
| 64 | + return "bounceban" |
| 65 | + |
| 66 | + @classmethod |
| 67 | + def get_output_template(cls) -> OutputTemplate | None: |
| 68 | + return OutputTemplate( |
| 69 | + markdown="{{response}}", |
| 70 | + jsonpath="$.response", |
| 71 | + ) |
| 72 | + |
| 73 | + def process(self) -> dict: |
| 74 | + provider_config = self.get_provider_config(provider_slug=self.provider_slug(), processor_slug="*") |
| 75 | + deployment_config = self.get_provider_config(provider_slug=self.provider_slug(), processor_slug="*") |
| 76 | + api_key = deployment_config.api_key |
| 77 | + response = get( |
| 78 | + url="https://api.bounceban.com/v1/verify/single", |
| 79 | + headers={"Authorization": f"{api_key}"}, |
| 80 | + params=self._input.model_dump(), |
| 81 | + ) |
| 82 | + self._usage_data.append( |
| 83 | + ( |
| 84 | + f"{self.provider_slug()}/*/*/*", |
| 85 | + MetricType.API_INVOCATION, |
| 86 | + (provider_config.provider_config_source, 1), |
| 87 | + ) |
| 88 | + ) |
| 89 | + async_to_sync(self._output_stream.write)( |
| 90 | + VerifyProcessorOutput( |
| 91 | + response=response.text, |
| 92 | + response_json=response.json(), |
| 93 | + headers=response.headers, |
| 94 | + code=response.status_code, |
| 95 | + size=len(response.content), |
| 96 | + time=response.elapsed.total_seconds(), |
| 97 | + ) |
| 98 | + ) |
| 99 | + |
| 100 | + output = self._output_stream.finalize() |
| 101 | + return output |
0 commit comments