|
1 | | -# TODO |
| 1 | +# Abstract API Python SDK |
| 2 | + |
| 3 | +This is a simple and intuitive Python SDK for using [AbstractAPI's](https://www.abstractapi.com/) services. |
| 4 | + |
| 5 | +Current supported services: |
| 6 | +=========================== |
| 7 | +All AbstractAPI services are supported by this SDK (16th. January 2023). |
| 8 | +- Email Validation |
| 9 | +- Phone Validation |
| 10 | +- VAT Validation/Calculation/Categories |
| 11 | +- IBAN Validation |
| 12 | +- IP Geolocation |
| 13 | +- Holidays Lookup |
| 14 | +- Exchange Rates Live/Convert/Historical |
| 15 | +- Company Enrichment |
| 16 | +- Timezone Current/Conversion |
| 17 | +- Avatars Generation |
| 18 | +- Website Screenshot |
| 19 | +- Website Scrape |
| 20 | +- Image Processing |
| 21 | + |
| 22 | +Installation: |
| 23 | +============= |
| 24 | +Install using `pip`: |
| 25 | + |
| 26 | + pip install abstract-api |
| 27 | + |
| 28 | +Getting Started: |
| 29 | +================ |
| 30 | +To use any service, you must have your API key for that service.\ |
| 31 | +To do that, you have two options: |
| 32 | +1. Export your API key as an environment variable:\ |
| 33 | + To export an API key for a service, you should follow this scheme: |
| 34 | + ``` |
| 35 | + ABSTRACTAPI_{SERVICE_NAME}_API_KEY |
| 36 | + ``` |
| 37 | + Note: `SERVICE_NAME` is all uppercase and underscore separated.\ |
| 38 | + For example, to export your Email Validation service API key, use the following environment variable name: |
| 39 | + ``` |
| 40 | + ABSTRACTAPI_EMAIL_VALIDATION_API_KEY |
| 41 | + ``` |
| 42 | + Example in terminal: |
| 43 | + ```shell |
| 44 | + export ABSTRACTAPI_AVATARS_API_KEY=612345e4a63044b47a1234567a53cc81 |
| 45 | + ``` |
| 46 | + In initialization, you don't have to pass an API key: |
| 47 | + ```python |
| 48 | + from abstract_api import EmailValidation |
| 49 | + |
| 50 | + service = EmailValidation() |
| 51 | + ``` |
| 52 | +2. Pass your API key during service class instantiation:\ |
| 53 | + Example: |
| 54 | + ```python |
| 55 | + from abstract_api import EmailValidation |
| 56 | + |
| 57 | + service = EmailValidation(api_key="612345e4a63044b47a1234567a53cc81") |
| 58 | + ``` |
| 59 | + |
| 60 | +**Note**: |
| 61 | +If both options were used simultaneously, the API key that was passed to constructor is used. |
| 62 | + |
| 63 | +Examples: |
| 64 | +========= |
| 65 | +**Notes**: |
| 66 | +- Each service response is represented as a response class to provide an intuitive\ |
| 67 | +Pythonic way for handling responses. |
| 68 | +- All public interfaces of all services classes are modeled after AbstractAPI endpoints interfaces and responses.\ |
| 69 | + Example: Email Validation service endpoint expects the following parameters: |
| 70 | + - `api_key` |
| 71 | + - `email` |
| 72 | + - `auto_correct`\ |
| 73 | + The `EmailValidation` class's `check()` method expects the same parameters `email` and `auto_correct`.\ |
| 74 | + (No need to pass `api_key`. It is already passed during service instantiation.) |
| 75 | + |
| 76 | +**Recommended**:\ |
| 77 | +- Check service class and service class response documentations. |
| 78 | +- Response fields used in examples are not only the ones. Check documentation to see\ |
| 79 | + all the capabilities. |
| 80 | + |
| 81 | +- Email Validation |
| 82 | + ```python |
| 83 | + from abstract_api import EmailValidation |
| 84 | + service = EmailValidation(api_key="612345e4a63044b47a1234567a53cc81") |
| 85 | + response = service.check("example@example.com") |
| 86 | + if response.is_valid_format: |
| 87 | + print("Email is valid!") |
| 88 | + if response.is_disposable_email: |
| 89 | + print("Email is disposable, not this time :( ") |
| 90 | + ``` |
| 91 | + `EmailValidation` documentation can be found [here](TODO)\ |
| 92 | + `EmailValidationResponse` documentation can be found [here](TODO) |
| 93 | + |
| 94 | +- Phone Validation |
| 95 | + ```python |
| 96 | + from abstract_api import PhoneValidation |
| 97 | + service = PhoneValidation(api_key="612345e4a63044b47a1234567a53cc81") |
| 98 | + response = service.check("20123456789") |
| 99 | + if response.valid: |
| 100 | + print("Phone number is valid!") |
| 101 | + ``` |
| 102 | + `PhoneValidation` documentation can be found [here](TODO)\ |
| 103 | + `PhoneValidationResponse` documentation can be found [here](TODO) |
| 104 | + |
| 105 | +- VAT Validation/Calculation/Inquiry |
| 106 | + ```python |
| 107 | + from abstract_api import VAT |
| 108 | + service = VAT(api_key="612345e4a63044b47a1234567a53cc81") |
| 109 | + validation_response = service.check("SE556656688001") |
| 110 | + calculation_response = service.calculate(amount=100, country_code="EG") |
| 111 | + categories_response = service.categories("EG") |
| 112 | + ``` |
| 113 | + `VAT` documentation can be found [here](TODO)\ |
| 114 | + `VATValidationResponse` documentation can be found [here](TODO)\ |
| 115 | + `VATCalculationResponse` documentation can be found [here](TODO)\ |
| 116 | + `VATCategoriesResponse` documentation can be found [here](TODO) |
| 117 | + |
| 118 | +- IBAN Validation |
| 119 | + ```python |
| 120 | + from abstract_api import IBANValidation |
| 121 | + service = IBANValidation(api_key="612345e4a63044b47a1234567a53cc81") |
| 122 | + response = service.check("BE71096123456769") |
| 123 | + if response.is_valid: |
| 124 | + print("IBAN is valid!") |
| 125 | + ``` |
| 126 | + `IBANValidation` documentation can be found [here](TODO)\ |
| 127 | + `IBANValidationResponse` documentation can be found [here](TODO) |
| 128 | + |
| 129 | +- IP Geolocation |
| 130 | + ```python |
| 131 | + from abstract_api import IPGeolocation |
| 132 | + service = IPGeolocation(api_key="612345e4a63044b47a1234567a53cc81") |
| 133 | + response = service.check("156.215.70.7", fields=["city"]) |
| 134 | + print("IP is in: ", response.city) |
| 135 | + ``` |
| 136 | + `IPGeolocation` documentation can be found [here](TODO)\ |
| 137 | + `IPGeolocationResponse` documentation can be found [here](TODO) |
| 138 | + |
| 139 | +- Holidays Lookup |
| 140 | + ```python |
| 141 | + from abstract_api import Holidays |
| 142 | + service = Holidays(api_key="612345e4a63044b47a1234567a53cc81") |
| 143 | + response = service.check("EG") |
| 144 | + print(response.holidays) |
| 145 | + ``` |
| 146 | + `Holidays` documentation can be found [here](TODO)\ |
| 147 | + `HolidaysResponse` documentation can be found [here](TODO) |
| 148 | + |
| 149 | +- Exchange Rates Live/Convert/Historical |
| 150 | + ```python |
| 151 | + from abstract_api import ExchangeRates |
| 152 | + service = ExchangeRates(api_key="612345e4a63044b47a1234567a53cc81") |
| 153 | + live_response = service.live("USD", "EGP") |
| 154 | + conversion_response = service.convert("USD", "EGP", "2023-01-17", 150) |
| 155 | + historical_response = service.historical("USD", "2023-01-17", 150) |
| 156 | + ``` |
| 157 | + `ExchangeRates` documentation can be found [here](TODO)\ |
| 158 | + `LiveExchangeRatesResponse` documentation can be found [here](TODO)\ |
| 159 | + `HistoricalExchangeRatesResponse` documentation can be found [here](TODO)\ |
| 160 | + `ExchangeRatesConversionResponse` documentation can be found [here](TODO) |
| 161 | + |
| 162 | +- Company Enrichment |
| 163 | + ```python |
| 164 | + from abstract_api import CompanyEnrichment |
| 165 | + service = CompanyEnrichment(api_key="612345e4a63044b47a1234567a53cc81") |
| 166 | + response = service.check("EG") |
| 167 | + print(response.holidays) |
| 168 | + ``` |
| 169 | + `CompanyEnrichment` documentation can be found [here](TODO)\ |
| 170 | + `CompanyEnrichmentResponse` documentation can be found [here](TODO) |
| 171 | + |
| 172 | +- Timezone Current/Conversion |
| 173 | + ```python |
| 174 | + from abstract_api import Timezone |
| 175 | + service = Timezone(api_key="612345e4a63044b47a1234567a53cc81") |
| 176 | + current_response = service.current("Cairo, Egypt", "82.111.111.111") |
| 177 | + conversion_response = service.convert((30.0594627, 31.1758899), "Cairo, Egypt") |
| 178 | + ``` |
| 179 | + `Timezone` documentation can be found [here](TODO)\ |
| 180 | + `CurrentTimezoneResponse` documentation can be found [here](TODO)\ |
| 181 | + `TimezoneConversionResponse` documentation can be found [here](TODO) |
| 182 | + |
| 183 | +- Avatars Generation |
| 184 | + ```python |
| 185 | + from abstract_api import Avatars |
| 186 | + service = Avatars(api_key="612345e4a63044b47a1234567a53cc81") |
| 187 | + response = service.create("John Doe", 200) |
| 188 | + file = open("logo.png", "wb+") |
| 189 | + file.write(response.content) |
| 190 | + ``` |
| 191 | + `Avatars` documentation can be found [here](TODO)\ |
| 192 | + `AvatarsResponse` documentation can be found [here](TODO) |
| 193 | + |
| 194 | +- Website Screenshot |
| 195 | + ```python |
| 196 | + from abstract_api import WebsiteScreenshot |
| 197 | + service = WebsiteScreenshot(api_key="612345e4a63044b47a1234567a53cc81") |
| 198 | + response = service.capture("https://www.github.com", capture_full_page=False) |
| 199 | + file = open("github-home-screenshot.png", "wb+") |
| 200 | + file.write(response.content) |
| 201 | + ``` |
| 202 | + `WebsiteScreenshot` documentation can be found [here](TODO)\ |
| 203 | + `WebsiteScreenshotResponse` documentation can be found [here](TODO) |
| 204 | + |
| 205 | +- Website Scrape |
| 206 | + ```python |
| 207 | + from abstract_api import WebScraping |
| 208 | + service = WebScraping(api_key="612345e4a63044b47a1234567a53cc81") |
| 209 | + response = service.scrape("https://www.github.com", proxy_country="EG") |
| 210 | + file = open("github-home-screenshot.png", "wb+") |
| 211 | + file.write(response.content) |
| 212 | + ``` |
| 213 | + `WebScraping` documentation can be found [here](TODO)\ |
| 214 | + `WebScrapingResponse` documentation can be found [here](TODO) |
| 215 | + |
| 216 | +- Image Processing |
| 217 | + ```python |
| 218 | + from abstract_api import ImageProcessing |
| 219 | + from abstract_api.image_processing.strategies import Crop, Exact |
| 220 | + resize = Exact(height=200, width=200) |
| 221 | + service = ImageProcessing(api_key="612345e4a63044b47a1234567a53cc81") |
| 222 | + image = open('example.png', 'rb') |
| 223 | + response = service.upload(image, lossy=False, resize=resize) |
| 224 | + print(response.url) |
| 225 | + response = service.url("https://example.com/image.jpeg", lossy=False, resize=resize) |
| 226 | + print(response.url) |
| 227 | + ``` |
| 228 | + `ImageProcessing` documentation can be found [here](TODO)\ |
| 229 | + `ImageProcessingResponse` documentation can be found [here](TODO) |
| 230 | + |
| 231 | +### Handling Errors |
| 232 | +1. If something wrong happened on client side: |
| 233 | + ```python |
| 234 | + from abstract_api import ImageProcessing |
| 235 | + from abstract_api.core.exceptions import ClientRequestError |
| 236 | + service = ImageProcessing(api_key="612345e4a63044b47a1234567a53cc81") |
| 237 | + try: |
| 238 | + service.url("https://example.com/image.jpeg", quality=150) |
| 239 | + except ClientRequestError as e: |
| 240 | + print("Some error happended from client's side") |
| 241 | + print(str(e)) |
| 242 | + 'quality must be in range from 1 to 100 (inclusive)' |
| 243 | + ``` |
| 244 | +2. If the service endpoint returns a status code that is not 200 or 204.\ |
| 245 | + (200 and 204 are -currently- the only accepted status codes.) |
| 246 | + ```python |
| 247 | + from abstract_api import ImageProcessing |
| 248 | + from abstract_api.core.exceptions import APIRequestError |
| 249 | + service = ImageProcessing(api_key="612345e4a63044b47a1234567a53cc81") |
| 250 | + try: |
| 251 | + service.url("https://example.com/image.jpeg", quality=150) |
| 252 | + except APIRequestError as e: |
| 253 | + if e.status_code == 500: |
| 254 | + print("AbstractAPI service is currently having a problem") |
| 255 | + print(str(e)) |
| 256 | + ``` |
0 commit comments