def post_records(gist_slug, tracking_slugs, version=None): """Associated tracked records with a commit""" url = _u('/data/' + gist_slug + '/commit' + _v(version)) res = post(url, json=tracking_slugs, headers=_h()) if res.status_code == 200: return res.json()['data'] else: raise ApiError('Data logging failed: ' + pretty(res))
def post_slack_message(data, safe=False): """Push data to Slack, if slack is integrated with jovian account""" url = _u('/slack/notify') res = post(url, json=data, headers=_h()) if res.status_code == 200: return res.json() elif safe: return {'data': {'messageSent': False}} else: raise ApiError('Slack trigger failed: ' + pretty(res))
def upload_file(gist_slug, file, folder=None, version=None, artifact=False, version_title=None): """Upload an additional file to a gist""" data = {'artifact': 'true'} if artifact else {} if folder: data['folder'] = folder if version_title: data['version_title'] = version_title res = post(url=_u('/gist/' + gist_slug + '/upload' + _v(version)), files={'files': file}, data=data, headers=_h()) if res.status_code == 200: return res.json()['data'] raise ApiError('File upload failed: ' + pretty(res))
def submit(assignment=None, notebook_url=None, **kwargs): """ Performs jovian.commit and makes a assignment submission with the uploaded notebook. """ if not assignment: log("Please provide assignment name", error=True) return filename = _parse_filename(kwargs.get('filename')) if filename == '__notebook_source__.ipynb': log("""jovian.submit does not support kaggle notebooks directly. Please make a commit first, copy the notebook URL and pass it to jovian.submit. eg. jovian.submit(assignment="zero-to-pandas-a1", notebook_url="https://jovian.ai/PrajwalPrashanth/assignment")""", error=True) return post_url = POST_API.format(assignment) nb_url = notebook_url if notebook_url else commit(**kwargs) if nb_url: data = { 'assignment_url': nb_url } auth_headers = _h() log('Submitting assignment..') res = post(url=_u(post_url), json=data, headers=auth_headers) if res.status_code == 200: data = res.json()['data'] course_slug = data.get('course_slug') assignment_slug = data.get('section_slug') assignment_page_url = ASSIGNMENT_PAGE_URL.format(course_slug, assignment_slug) log('Verify your submission at {}'.format(urljoin(read_webapp_url(), assignment_page_url))) else: log('Jovian submit failed. {}'.format(pretty(res)), error=True)
def create_gist_simple(filename=None, gist_slug=None, privacy='auto', title=None, version_title=None): """Upload the current notebook to create/update a gist""" auth_headers = _h() with open(filename, 'rb') as f: nb_file = (filename, f) log('Uploading notebook..') if gist_slug: return upload_file(gist_slug=gist_slug, file=nb_file, version_title=version_title) else: data = {'visibility': privacy} # For compatibility with old version of API endpoint if privacy == 'auto': data['public'] = True elif privacy == 'secret' or privacy == 'private': data['public'] = False if title: data['title'] = title if version_title: data['version_title'] = version_title res = post(url=_u('/gist/create'), data=data, files={'files': nb_file}, headers=auth_headers) if res.status_code == 200: data, warning = parse_success_response(res) if warning: log(warning, error=True) return data raise ApiError('File upload failed: ' + pretty(res))
def perform_colab_commit(project, privacy): if '/' not in project: project = get_current_user()['username'] + '/' + project data = { 'project': project, 'file_id': get_colab_file_id(), 'visibility': privacy } if privacy == 'auto': data['public'] = True elif privacy == 'secret' or privacy == 'private': data['public'] = False auth_headers = _h() log("Uploading colab notebook to Jovian...") res = post(url=_u('/gist/colab-commit'), data=data, headers=auth_headers) if res.status_code == 200: return res.json()['data'] raise ApiError('Colab commit failed: ' + pretty(res))
def upload_file(gist_slug, file, folder=None, version=None, artifact=False, version_title=None): """Upload an additional file to a gist""" data = {'artifact': 'true'} if artifact else {} if folder: data['folder'] = folder if version_title: data['version_title'] = version_title res = post(url=_u('/gist/' + gist_slug + '/upload' + _v(version)), files={'files': file}, data=data, headers=_h()) if res.status_code == 200: data, warning = parse_success_response(res) if warning: log(warning, error=True) return data raise ApiError('File upload failed: ' + pretty(res))
def test_post_called_twice(self, mock_post): post('http://someurl.com/noauth', headers={'key': 'value'}) assert mock_post.call_count == 2
def test_post_called_once(self, mock_post): post('http://someurl.com/create', headers={'key': 'value'}) assert mock_post.call_count == 1