Skip to content

Instantly share code, notes, and snippets.

@gilv
Created May 5, 2015 08:06
Show Gist options
  • Save gilv/7e70ba055f24bcc472b6 to your computer and use it in GitHub Desktop.
Save gilv/7e70ba055f24bcc472b6 to your computer and use it in GitHub Desktop.
Swift stopper middleware
# Copyright IBM Corp. 2015, 2015 All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from swift.common.utils import get_logger, split_path
from swift.common.swob import wsgify, Request, HTTPCreated, HTTPOk, HTTPNoContent
class SwiftStopperMiddleware(object):
def __init__(self, app, conf):
self.app = app
self.logger = get_logger(conf, log_route='swift_stopper')
@wsgify
def __call__(self, req):
try:
(version, account, container, objname) = split_path( req.path_info, 1, 4, True)
except ValueError:
return self.app
if (req.method == 'PUT' or req.method =='POST'):
rh = {'etag': '123'}
return HTTPCreated(body='created', headers = rh)
elif (req.method == 'DELETE'):
return HTTPNoContent('deleted')
elif (req.method == 'GET'):
return HTTPOk(body='get response')
elif (req.method == 'HEAD'):
return HTTPNoContent(body='no content for HEAD')
return self.app
def filter_factory(global_conf, **local_conf):
conf = global_conf.copy()
conf.update(local_conf)
def swift_stopper(app):
return SwiftStopperMiddleware(app, conf)
return swift_stopper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment