1
0
mirror of synced 2026-01-05 21:02:13 -05:00
Files
airbyte/airbyte-integrations/connectors/source-pokeapi/source_pokeapi/source.py
Abhi Vaidyanatha e2d5bfa3ea Add primary_key noop to docs and PokeAPI connector. (#3245)
* Add primary_key noop to docs and PokeAPI connector.

* Small grammar fix in FAQ.

* Change empty string to None for noop.

Co-authored-by: Abhi Vaidyanatha <abhivaidyanatha@Abhis-MacBook-Pro.local>
2021-05-06 10:54:42 -07:00

85 lines
3.2 KiB
Python

# MIT License
#
# Copyright (c) 2020 Airbyte
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from typing import Any, Iterable, List, Mapping, MutableMapping, Optional, Tuple
import requests
from base_python import AbstractSource, HttpStream, Stream
from . import pokemon_list
class PokeapiStream(HttpStream):
url_base = "https://pokeapi.co/api/v2/"
def __init__(self, pokemon_name: str, **kwargs):
super().__init__(**kwargs)
self.pokemon_name = pokemon_name
def request_params(
self,
stream_state: Mapping[str, Any],
stream_slice: Mapping[str, Any] = None,
next_page_token: Mapping[str, Any] = None,
) -> MutableMapping[str, Any]:
# The api requires that we include the Pokemon name as a query param so we do that in this method
return {"pokemon_name": self.pokemon_name}
def parse_response(
self,
response: requests.Response,
stream_state: Mapping[str, Any],
stream_slice: Mapping[str, Any] = None,
next_page_token: Mapping[str, Any] = None,
) -> Iterable[Mapping]:
# The response is a simple JSON whose schema matches our stream's schema exactly,
# so we just return a list containing the response
return [response.json()]
def next_page_token(self, response: requests.Response) -> Optional[Mapping[str, Any]]:
# The API does not offer pagination,
# so we return None to indicate there are no more pages in the response
return None
class Pokemon(PokeapiStream):
# Set this as a noop.
primary_key = None
def path(self, **kwargs) -> str:
pokemon_name = self.pokemon_name
return f"pokemon/{pokemon_name}"
# Source
class SourcePokeapi(AbstractSource):
def check_connection(self, logger, config) -> Tuple[bool, any]:
input_pokemon = config["pokemon_name"]
if input_pokemon not in pokemon_list.POKEMON_LIST:
return False, f"Input Pokemon {input_pokemon} is invalid. Please check your spelling our input a valid Pokemon."
else:
return True, None
def streams(self, config: Mapping[str, Any]) -> List[Stream]:
return [Pokemon(pokemon_name=config["pokemon_name"])]