def change_container(container_name): #TODO: check schema validity since somebody else could store a rouge class definition in the object store (via direct interfacing with the object store) try: container_definition = request.json.get("container") container_name = container_definition.get("name") except AttributeError: raise HttpError("malformed request", 400) if not container_name: raise HttpError("container name is missing", 400) containers = swift.get_container_list()[1] if container_name not in [ container.get("name") for container in containers ]: raise HttpError("container does not exist", 404) container_metadata = {} try: class_name = container_definition.get("objectClass") class_definition = internal_data.get_data("object classes", class_name) if class_name: if class_definition is None: raise HttpError("class does not exist", 404) container_metadata = {"x-container-meta-object-class": class_name} except AttributeError: pass # ignore empty or missing class definition swift.create_container(container_name, container_metadata) return "", 201
def create_objectclass(): try: class_definition = request.json.get("objectClass") class_name = class_definition.get("name") class_schema = class_definition.get("schema") except AttributeError: raise HttpError("malformed request", 400) if not class_name or not class_schema: raise HttpError("class name or class schema definition missing", 400) class_names = internal_data.get_keys("object classes") if class_name in class_names: raise HttpError("class already exists", 422) try: Draft4Validator( CLASS_SCHEMA, format_checker=FormatChecker()).validate(class_definition) except ValidationError as e: raise HttpError("invalid class definition: {}".format(e), 400) internal_data.store_data("object classes", class_name, json.dumps(class_definition)) return "", 201
def create_object(container_name): # returns werkzeug.datastructures.FileStorage i.e. file-like # Underlying stream is either BytesIO for small files or _TemporaryFileWrapper for large files file = request.files["objectName"] object_name = secure_filename(file.filename) # check whether an object with the same name already exists in the same container all_objects = swift.get_object_list(container_name)[1] if object_name in [obj.get("name") for obj in all_objects]: raise HttpError( "object with this name already exists in this container", 422) headers = {} retentime = request.form["RetentionPeriod"] if retentime: try: converted_retentime = datetime.strptime(retentime, "%Y-%m-%d") reten_timestamp = int(time.mktime(converted_retentime.timetuple())) headers["X-Object-Meta-RetentionTime"] = reten_timestamp except Exception as e: log.debug( "invalid date format for form parameter RetentionPeriod: {}, for request: {}. Expected format: yyyy-mm-dd" .format(retentime)) raise HttpError( "invalid date format for form parameter RetentionPeriod: {}. Expected format: yyyy-mm-dd" .format(retentime), 400) class_metadata_json = request.form["metadata"] if class_metadata_json: class_metadata = json.loads(class_metadata_json) class_name = swift.get_container_metadata(container_name).get( "x-container-meta-object-class") if class_name: class_definition = json.loads( internal_data.get_data("object classes", class_name)) Draft4Validator( class_definition, format_checker=FormatChecker()).validate(class_metadata) for field in class_metadata.keys(): val = class_metadata[field] if val is not None: field_header = xform_header_names(field) xformed_class_name = xform_header_names(class_name) headers["X-Object-Meta-" + xformed_class_name + "-Class-" + field_header] = class_metadata[field] swift.streaming_object_upload(object_name, container_name, file, headers) return "", 201
def auth_osu_refresh(): tkn = request.json.get('refresh_token') if tkn is None: raise HttpError('Error: No `refresh_token` received!') try: new_tokens = osu_auth_token( grant_type='refresh_token', refresh_token=tkn ) except requests.HTTPError as e: raise HttpError(str(e), status_code=e.response.status_code) except Exception as e: logging.exception(e) raise HttpError(repr(e), status_code=500) else: return jsonify(new_tokens)
def index(path=""): if path[:5] != "swift": # return render_template('index.html') return send_file("angular/index.html") else: # this function is only called, when no other route matches raise HttpError("the requested endpoint does not exist", 404)
def get_objects_in_container(container_name): optional_params = {} limit = request.args.get("limit") if limit is not None: if limit.isdigit() and int(limit) > 0: optional_params["limit"] = int(limit) else: log.debug( "invalid query parameter limit: {}, for request: {}".format( limit, request.url)) raise HttpError( "specified query parameter limit: {}, must be a positive integer" .format(limit), 400) marker = request.args.get("marker") if marker is not None: optional_params["marker"] = marker prefix = request.args.get("prefix") if prefix is not None: optional_params["prefix"] = prefix cts = swift.get_object_list(container_name, **optional_params) resp = {} resp["metadata"] = { "objectClass": cts[0].get("x-container-meta-object-class"), "objectCount": cts[0].get("x-container-object-count") } resp["objects"] = cts[1] return Response(json.dumps(resp, sort_keys=True), mimetype="application/json")
def get_objectclass(class_name): class_def = internal_data.get_data("object classes", class_name) if not class_def: raise HttpError("class does not exist", 404) return Response(class_def, mimetype="application/json")
async def post(self, url, params=None, headers=None, cookies=None, data=None, rate_limited: bool = True): if rate_limited == True: session = self.rl_session else: session = self.session err = None for _ in range(3): # fire request try: response = await session.post(url, params=params, headers=headers, cookies=cookies, data=data, ssl=False) response.raise_for_status() r_json = await response.json(content_type=None) return r_json except aiohttp.ClientError as exc: err = exc cprint(f'aiohttp ClientError: {exc}', color='red') if err: raise HttpError(f'Request failed: {err} @url {url!r}')
def delete_objectclass(class_name): class_def = internal_data.get_data("object classes", class_name) if not class_def: raise HttpError("class does not exist", 404) internal_data.remove_data("object classes", class_name) return "", 204
def delete_object(container_name, object_name): metadata = swift.get_object_metadata(container_name, object_name) retentimestamp = metadata.get("x-object-meta-retentiontime") if retentimestamp and not isRetentionPeriodExpired(retentimestamp): error_msg = "Deletion failed due to retention enforcement, file cannot be deleted till {}!".format( time.strftime("%a, %d. %B %Y", time.localtime(int(retentimestamp)))) log.debug(error_msg) raise HttpError(error_msg, 412) swift.delete_object(container_name, object_name) return "", 204
def add_promotions(): promotions_file = request.files.get('promotions') if not promotions_file or not promotions_file.filename: raise HttpError('Missing field', 400, { 'errors': { 'discounts': 'this field is required', }, }) promotions_parsed = parse(promotions_file) # for production, create_products should probably be 'false'. I set it # as a facility to automatically create missing products promotions = Promotion.from_file(promotions_parsed, request.user, create_products=True) promotions_dict = list(map(serialize_promotion, promotions)) response = jsonify(promotions_dict) response.status_code = 201 return response
def change_object_metadata(container_name, object_name): raise HttpError("funcion not implemented yet")