Created
February 16, 2021 12:11
-
-
Save xoelop/cbd2ba772138f399620515c7594e0f23 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import urllib.parse | |
import json | |
def jprint(data: str): | |
"""Prints JSON-like string (data) nicely""" | |
print(json.dumps(data, indent=4, ensure_ascii=False)) | |
def ingest_data(datasource: str, | |
token: str, | |
csv_url: str = None, | |
csv_file: str = None, | |
mode='append'): | |
print(f'Appending {csv_url or csv_file} to the Tinybird Data Source {datasource}') | |
headers = {'Authorization': f'Bearer {token}'} | |
base_url = 'https://api.tinybird.co/v0/datasources' | |
url = f'{base_url}?name={datasource}&mode={mode}' | |
if csv_file: | |
with open(csv_file, 'rb') as f: | |
files = {'csv': f} # the key has to be *exactly* 'csv' or it'll fail silently (0 rows will be imported) | |
response = requests.post(url, headers=headers, files=files) | |
elif csv_url: | |
url += f'&url={urllib.parse.quote(csv_url)}' | |
response = requests.post(url, headers=headers) | |
return jprint(response.json()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment