github.com/uber/kraken@v0.1.4/test/python/uploader.py (about) 1 # Copyright (c) 2016-2019 Uber Technologies, Inc. 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 from __future__ import absolute_import 15 16 import requests 17 18 from utils import tls_opts_with_client_certs 19 20 21 class Uploader(object): 22 23 def __init__(self, addr): 24 self.addr = addr 25 26 def _start(self, name): 27 url = 'https://{addr}/namespace/testfs/blobs/sha256:{name}/uploads'.format( 28 addr=self.addr, name=name) 29 res = requests.post(url, **tls_opts_with_client_certs()) 30 res.raise_for_status() 31 return res.headers['Location'] 32 33 def _patch(self, name, uid, start, stop, chunk): 34 url = 'https://{addr}/namespace/testfs/blobs/sha256:{name}/uploads/{uid}'.format( 35 addr=self.addr, name=name, uid=uid) 36 res = requests.patch(url, headers={'Content-Range': '%d-%d' % (start, stop)}, data=chunk, **tls_opts_with_client_certs()) 37 res.raise_for_status() 38 39 def _commit(self, name, uid): 40 url = 'https://{addr}/namespace/testfs/blobs/sha256:{name}/uploads/{uid}'.format( 41 addr=self.addr, name=name, uid=uid) 42 res = requests.put(url, **tls_opts_with_client_certs()) 43 res.raise_for_status() 44 45 def upload(self, name, blob): 46 uid = self._start(name) 47 self._patch(name, uid, 0, len(blob), blob) 48 self._commit(name, uid)