def get_cached_query_results(self, query_id=None, query_api_key=None): """ Get the results from a cached query result and get back the CSV http response object back with the CSV string in result.content `Args:` query_id: str or int The query id of the query query_api_key: str If you did not supply a user_api_key on the Redash object, then you can supply a query_api_key to get cached results back anonymously. `Returns:` Table Class """ query_id = check('REDASH_QUERY_ID', query_id) query_api_key = check('REDASH_QUERY_API_KEY', query_api_key, optional=True) params = {} if not self.user_api_key and query_api_key: params['api_key'] = query_api_key response = self.session.get( f'{self.base_url}/api/queries/{query_id}/results.csv', params=params, verify=self.verify) if response.status_code != 200: raise RedashQueryFailed( f'Failed getting results for query {query_id}. {response.text}' ) return Table.from_csv_string(response.text)
def __init__(self, api_user=None, api_password=None, api_version=None): """ Args: api_user: str The Newmode api user. Not required if ``NEWMODE_API_USER`` env variable is passed. api_password: str The Newmode api password. Not required if ``NEWMODE_API_PASSWORD`` env variable is passed. api_version: str The Newmode api version. Defaults to "v1.0" or the value of ``NEWMODE_API_VERSION`` env variable. Returns: Newmode class """ self.api_user = check_env.check('NEWMODE_API_USER', api_user) self.api_password = check_env.check('NEWMODE_API_PASSWORD', api_password) if api_version is None: api_version = "v1.0" self.api_version = check_env.check('NEWMODE_API_VERSION', api_version) self.client = Client(api_user, api_password, api_version)
def __init__(self, client_id, client_secret): self.uri = HUSTLE_URI self.client_id = check_env.check('HUSTLE_CLIENT_ID', client_id) self.client_secret = check_env.check('HUSTLE_CLIENT_SECRET', client_secret) self.token_expiration = None self._get_auth_token(client_id, client_secret)
def __init__(self, hostname=None, user_token=None): self.hostname = check_env.check('QUICKBASE_HOSTNAME', hostname) self.user_token = check_env.check('QUICKBASE_USER_TOKEN', user_token) self.api_hostname = 'https://api.quickbase.com/v1' self.client = APIConnector(self.api_hostname, headers={'QB-Realm-Hostname': self.hostname, 'AUTHORIZATION': f'QB-USER-TOKEN {self.user_token}'})
def __init__(self, username=None, password=None, api_token=None, qa_url=False): """A class to access the PDI API. `Args:` username: str The username for a PDI account. Can be passed as arguement or can be set as `PDI_USERNAME` environment variable. password: str The password for a PDI account. Can be passed as arguement or can be set as `PDI_PASSWORD` environment variable. api_token: str The api_token for a PDI account. Can be passed as arguement or can be set as `PDI_API_TOKEN` environment variable. qa_url: bool Defaults to False. If True, requests will be made to a sandbox account. NOTE: This requires separate qa credentials and api token. """ if qa_url: self.base_url = "https://apiqa.bluevote.com" else: self.base_url = "https://api.bluevote.com" self.username = check_env.check('PDI_USERNAME', username) self.password = check_env.check('PDI_PASSWORD', password) self.api_token = check_env.check('PDI_API_TOKEN', api_token) super().__init__() self._get_session_token()
def __init__(self, partner_id=None, partner_api_key=None): self.partner_id = check_env.check('RTV_PARTNER_ID', partner_id) self.partner_api_key = check_env.check('RTV_PARTNER_API_KEY', partner_api_key) self.client = APIConnector('https://vr.rockthevote.com/api/v4', headers=REQUEST_HEADERS)
def __init__(self, api_key=None, client_id=None, client_secret=None): self.api_key = check_env.check('BLOOMERANG_API_KEY', api_key, optional=True) self.client_id = check_env.check('BLOOMERANG_CLIENT_ID', client_id, optional=True) self.client_secret = check_env.check('BLOOMERANG_CLIENT_SECRET', client_secret, optional=True) self.uri = URI self.uri_auth = URI_AUTH self.conn = self._conn()
def __init__(self, api_token=None, api_url=None): self.api_token = check_env.check('AN_API_TOKEN', api_token) self.headers = { "Content-Type": "application/json", "OSDI-API-Token": self.api_token } self.api_url = check_env.check('AN_API_URL', api_url) self.api = APIConnector(self.api_url, headers=self.headers)
def __init__(self, host=None, username=None, password=None, db=None, port=3306): super().__init__() self.username = check_env.check('MYSQL_USERNAME', username) self.password = check_env.check('MYSQL_PASSWORD', password) self.host = check_env.check('MYSQL_HOST', host) self.db = check_env.check('MYSQL_DB', db) self.port = port or os.environ.get('MYSQL_PORT')
def __init__(self, sftp_username=None, sftp_password=None): self.sftp_host = TS_STFP_HOST self.sftp_port = TS_SFTP_PORT self.sftp_dir = TS_SFTP_DIR self.sftp_username = check_env.check('TS_SFTP_USERNAME', sftp_username) self.sftp_password = check_env.check('TS_SFTP_PASSWORD', sftp_password) self.sftp = SFTP(self.sftp_host, self.sftp_username, self.sftp_password, self.sftp_port)
def __init__(self, partner_id=None, partner_api_key=None, testing=False): self.partner_id = check_env.check('RTV_PARTNER_ID', partner_id) self.partner_api_key = check_env.check('RTV_PARTNER_API_KEY', partner_api_key) if testing: self.client = APIConnector(TESTING_URI, headers=REQUEST_HEADERS) else: self.client = APIConnector(PRODUCTION_URI, headers=REQUEST_HEADERS)
def __init__(self, subdomain=None, password=None, api_key=None, api_version=None): self.subdomain = check_env.check('SHOPIFY_SUBDOMAIN', subdomain) self.password = check_env.check('SHOPIFY_PASSWORD', password) self.api_key = check_env.check('SHOPIFY_API_KEY', api_key) self.api_version = check_env.check('SHOPIFY_API_VERSION', api_version) self.client = APIConnector('https://%s.myshopify.com/admin/api/%s/' % ( self.subdomain, self.api_version ), auth=(self.api_key, self.password))
def __init__(self, api_token=None, api_token_secret=None, api_version='v5'): self.api_token = check_env.check('SURVEYGIZMO_API_TOKEN', api_token) self.api_token_secret = check_env.check('SURVEYGIZMO_API_TOKEN_SECRET', api_token_secret) self.api_version = check_env.check('SURVEYGIZMO_API_VERSION', api_version) self._client = surveygizmo.SurveyGizmo( api_version=self.api_version, api_token=self.api_token, api_token_secret=self.api_token_secret )
def __init__(self, user=None, password=None): self.user = check_env.check('BLUELINK_WEBHOOK_USER', user) self.password = check_env.check('BLUELINK_WEBHOOK_PASSWORD', password) self.headers = { "Content-Type": "application/json", } self.api_url = API_URL self.api = APIConnector(self.api_url, auth=(self.user, self.password), headers=self.headers)
def __init__(self, hostname=None, client_id=None, client_secret=None): self.hostname = check_env.check('CONTROLSHIFT_HOSTNAME', hostname) token_url = f'{self.hostname}/oauth/token' self.client = OAuth2APIConnector( self.hostname, client_id=check_env.check('CONTROLSHIFT_CLIENT_ID', client_id), client_secret=check_env.check('CONTROLSHIFT_CLIENT_SECRET', client_secret), token_url=token_url, auto_refresh_url=token_url)
def __init__(self, actblue_client_uuid=None, actblue_client_secret=None, actblue_uri=None): self.actblue_client_uuid = check_env.check('ACTBLUE_CLIENT_UUID', actblue_client_uuid) self.actblue_client_secret = check_env.check('ACTBLUE_CLIENT_SECRET', actblue_client_secret) self.uri = check_env.check( 'ACTBLUE_URI', actblue_uri, optional=True ) or ACTBLUE_API_ENDPOINT self.headers = { "accept": "application/json", } self.client = APIConnector(self.uri, auth=(self.actblue_client_uuid, self.actblue_client_secret), headers=self.headers)
def setup_google_application_credentials(app_creds): # Detect if the app_creds string is a path or json and if it is a # json string, then convert it to a temporary file. Then set the # environmental variable. if app_creds: try: json.loads(app_creds) creds_path = files.string_to_temp_file(app_creds, suffix='.json') except ValueError: creds_path = app_creds os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = creds_path check_env.check('GOOGLE_APPLICATION_CREDENTIALS', app_creds)
def get_fresh_query_results(self, query_id=None, params=None): """ Make a fresh query result and get back the CSV http response object back with the CSV string in result.content `Args:` query_id: str or int The query id of the query params: dict If there are values for the redash query parameters (described https://redash.io/help/user-guide/querying/query-parameters e.g. "{{datelimit}}" in the query), then this is a dict that will pass the parameters in the POST. We add the "p_" prefix for parameters, so if your query had ?p_datelimit=.... in the url, you should just set 'datelimit' in params here. If you set this with REDASH_QUERY_PARAMS environment variable instead of passing the values, then you must include the "p_" prefixes and it should be a single url-encoded string as you would see it in the URL bar. `Returns:` Table Class """ query_id = check('REDASH_QUERY_ID', query_id, optional=True) params_from_env = check('REDASH_QUERY_PARAMS', '', optional=True) redash_params = ( {'p_%s' % k: str(v).replace("'", "''") for k, v in params.items()} if params else {}) response = self.session.post( f'{self.base_url}/api/queries/{query_id}/refresh?{params_from_env}', params=redash_params, verify=self.verify) if response.status_code != 200: raise RedashQueryFailed( f'Refresh failed for query {query_id}. {response.text}') job = response.json()['job'] result_id = self._poll_job(self.session, job, query_id) if result_id: response = self.session.get( f'{self.base_url}/api/queries/{query_id}/results/{result_id}.csv', verify=self.verify) if response.status_code != 200: raise RedashQueryFailed( f'Failed getting results for query {query_id}. {response.text}' ) else: raise RedashQueryFailed( f'Failed getting result {query_id}. {response.text}') return Table.from_csv_string(response.text)
def __init__(self, app_id=None, app_key=None): # check first for CapitolCanary branded app key and ID cc_app_id = check_env.check('CAPITOLCANARY_APP_ID', None, optional=True) cc_app_key = check_env.check('CAPITOLCANARY_APP_KEY', None, optional=True) self.app_id = cc_app_id or check_env.check('PHONE2ACTION_APP_ID', app_id) self.app_key = cc_app_key or check_env.check('PHONE2ACTION_APP_KEY', app_key) self.auth = HTTPBasicAuth(self.app_id, self.app_key) self.client = APIConnector(CAPITOL_CANARY_URI, auth=self.auth)
def __init__(self, account_name=None, credential=None, account_domain='blob.core.windows.net', account_url=None): self.account_url = os.getenv('AZURE_ACCOUNT_URL', account_url) self.credential = check_env.check('AZURE_CREDENTIAL', credential) if not self.account_url: self.account_name = check_env.check('AZURE_ACCOUNT_NAME', account_name) self.account_domain = check_env.check('AZURE_ACCOUNT_DOMAIN', account_domain) self.account_url = f'https://{self.account_name}.{self.account_domain}/' else: if not self.account_url.startswith('http'): self.account_url = f'https://{self.account_url}' # Update the account name and domain if a URL is supplied parsed_url = urlparse(self.account_url) self.account_name = parsed_url.netloc.split(".")[0] self.account_domain = ".".join(parsed_url.netloc.split(".")[1:]) self.client = BlobServiceClient(account_url=self.account_url, credential=self.credential)
def __init__(self, api_key=None, auth_name='default', db=None): self.api_key = check_env.check('VAN_API_KEY', api_key) if db == 'MyVoters': self.db_code = 0 elif db in ['MyMembers', 'MyCampaign', 'EveryAction']: self.db_code = 1 else: raise KeyError('Invalid database type specified. Pick one of:' ' MyVoters, MyCampaign, MyMembers, EveryAction.') self.uri = URI self.db = db self.auth_name = auth_name self.pagination_key = 'nextPageLink' self.auth = (self.auth_name, self.api_key + '|' + str(self.db_code)) self.api = APIConnector(self.uri, auth=self.auth, data_key='items', pagination_key=self.pagination_key) # We will not create the SOAP client unless we need to as this triggers checking for # valid credentials. As not all API keys are provisioned for SOAP, this keeps it from # raising a permission exception when creating the class. self._soap_client = None
def __init__(self, api_token=None, api_token_secret=None, api_version='v5'): sg_compatibility() self.api_token = check_env.check('ALCHEMER_API_TOKEN', api_token) self.api_token_secret = check_env.check('ALCHEMER_API_TOKEN_SECRET', api_token_secret) self.api_version = check_env.check('ALCHEMER_API_VERSION', api_version) self._client = surveygizmo.SurveyGizmo( api_version=self.api_version, api_token=self.api_token, api_token_secret=self.api_token_secret)
def __init__(self, host=None, port=None, username=None, password=None, tls=None, close_manually=False): self.host = check('SMTP_HOST', host) self.port = check('SMTP_PORT', port, optional=True) or 587 self.username = check('SMTP_USER', username) self.password = check('SMTP_PASSWORD', password) self.tls = not (check('SMTP_TLS', tls, optional=True) in ('false', 'False', '0', False)) self.close_manually = close_manually self.conn = None
def __init__(self, username=None, password=None, security_token=None, test_environment=False): self.username = check_env.check('SALESFORCE_USERNAME', username) self.password = check_env.check('SALESFORCE_PASSWORD', password) self.security_token = check_env.check('SALESFORCE_SECURITY_TOKEN', security_token) if test_environment: self.domain = check_env.check('SALESFORCE_DOMAIN', 'test') else: self.domain = None self._client = None
def __init__(self, client_id=None, client_secret=None, domain=None): self.base_url = f"https://{check_env.check('AUTH0_DOMAIN', domain)}" access_token = requests.post( f'{self.base_url}/oauth/token', data={ 'grant_type': 'client_credentials', # OAuth 2.0 flow to use 'client_id': check_env.check('AUTH0_CLIENT_ID', client_id), 'client_secret': check_env.check('AUTH0_CLIENT_SECRET', client_secret), 'audience': f'{self.base_url}/api/v2/' }).json().get('access_token') self.headers = { 'Authorization': f'Bearer {access_token}', 'Content-Type': 'application/json' }
def __init__(self, username=None, password=None, access_token=None): self.username = check_env.check('GITHUB_USERNAME', username, optional=True) self.password = check_env.check('GITHUB_PASSWORD', password, optional=True) self.access_token = check_env.check('GITHUB_ACCESS_TOKEN', access_token, optional=True) if self.username and self.password: self.client = PyGithub(self.username, self.password) elif self.access_token: self.client = PyGithub(self.access_token) else: self.client = PyGithub()
def bulk_upload_table(self, table, import_page, autocreate_user_fields=0, no_overwrite_on_empty=False, set_only_columns=None): """ Bulk upload a table of new users or user updates. See `ActionKit User Upload Documentation <https://roboticdogs.actionkit.com/docs/manual/api/rest/uploads.html>`_ Be careful that blank values in columns will overwrite existing data. Tables with only an identifying column (user_id/email) and user_ user fields will be fast-processed -- this is useful for setting/updating user fields. .. note:: If you get a 500 error, try sending a much smaller file (say, one row), which is more likely to return the proper 400 with a useful error message `Args:` import_page: str The page to post the action. The page short name. table: Table Class A Table of user data to bulk upload A user_id or email column is required. autocreate_user_fields: bool When True columns starting with "user_" will be uploaded as user fields. `ActionKit <https://actionkit.com/>`_. See the autocreate_user_fields `documentation <https://roboticdogs.actionkit.com/docs/manual/api/rest/uploads.html#create-a-multipart-post-request>`_. no_overwrite_on_empty: bool When uploading user data, ActionKit will, by default, take a blank value and overwrite existing data for that user. This can be undesirable, if the goal is to only send updates. Setting this to True will divide up the table into multiple upload batches, changing the columns uploaded based on permutations of empty columns. set_only_columns: list This is similar to no_overwrite_on_empty but restricts to a specific set of columns which, if blank, should not be overwritten. `Returns`: dict success: bool -- whether upload was successful (individual rows may not have been) results: [dict] -- This is a list of the full results. progress_url and res for any results """ # noqa: E501,E261 import_page = check_env.check('ACTION_KIT_IMPORTPAGE', import_page) upload_tables = self._split_tables_no_empties( table, no_overwrite_on_empty, set_only_columns) results = [] for tbl in upload_tables: user_fields_only = int(not any([ h for h in tbl.columns if h != 'email' and not h.startswith('user_')])) results.append(self.bulk_upload_csv(tbl.to_csv(), import_page, autocreate_user_fields=autocreate_user_fields, user_fields_only=user_fields_only)) return { 'success': all([r['success'] for r in results]), 'results': results }
def __init__( self, base_url=None, user_api_key=None, pause_time=3, timeout=0, # never timeout verify=True): self.base_url = check('REDASH_BASE_URL', base_url) self.user_api_key = check('REDASH_USER_API_KEY', user_api_key, optional=True) self.pause = int(check('REDASH_PAUSE_TIME', pause_time, optional=True)) self.timeout = int(check('REDASH_TIMEOUT', timeout, optional=True)) self.verify = verify # for https requests self.session = requests.Session() if user_api_key: self.session.headers.update( {'Authorization': f'Key {user_api_key}'})
def setup_google_application_credentials( app_creds, env_var_name='GOOGLE_APPLICATION_CREDENTIALS'): # Detect if app_creds is a dict, path string or json string, and if it is a # json string, then convert it to a temporary file. Then set the # environmental variable. credentials = check_env.check(env_var_name, app_creds) try: if (type(credentials) is dict): credentials = json.dumps(credentials) creds_path = files.string_to_temp_file(credentials, suffix='.json') except ValueError: creds_path = credentials os.environ[env_var_name] = creds_path
def __init__(self, api_key=None, auth_name='default', db=None): self.api_key = check_env.check('VAN_API_KEY', api_key) if db == 'MyVoters': self.db_code = 0 elif db in ['MyMembers', 'MyCampaign', 'EveryAction']: self.db_code = 1 else: raise KeyError('Invalid database type specified. Pick one of:' ' MyVoters, MyCampaign, MyMembers, EveryAction.') self.uri = URI self.db = db self.auth_name = auth_name self.auth = (self.auth_name, self.api_key + '|' + str(self.db_code)) self.api = APIConnector(self.uri, auth=self.auth, data_key='items')