Skip to content

Instantly share code, notes, and snippets.

@xoelop
Created February 16, 2021 12:11
Show Gist options
  • Save xoelop/cbd2ba772138f399620515c7594e0f23 to your computer and use it in GitHub Desktop.
Save xoelop/cbd2ba772138f399620515c7594e0f23 to your computer and use it in GitHub Desktop.
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