def setUp(self): self.video_granulate_service = Restfulie.at("http://localhost:8885/").auth('test', 'test').as_('application/json') self.sam = Restfulie.at("http://localhost:8888/").auth('test', 'test').as_('application/json') self.uid_list = [] input_video = open(join(FOLDER_PATH,'input','working_google.flv')).read() self.b64_encoded_video = b64encode(input_video)
def testAuthentication(self): """ Test if the server is authenticating correctly """ sam_with_non_existing_user = Restfulie.at("http://localhost:8888/").as_("application/json").auth('dont', 'exists') result = sam_with_non_existing_user.post(value='test') self.assertEquals(result.code, "401") sam_with_non_existing_user = Restfulie.at("http://localhost:8888/").as_("application/json").auth('test', 'wrongpassword') result = sam_with_non_existing_user.post(value='test') self.assertEquals(result.code, "401")
def setUp(self): self.video_convert_service = Restfulie.at("http://localhost:8884/").auth('test', 'test').as_('application/json') self.sam = Restfulie.at("http://localhost:8888/").auth('test', 'test').as_('application/json') self.uid_list = [] input_video = open(join(FOLDER_PATH,'input','rubik.flv')).read() self.b64_encoded_video = b64encode(input_video) response = self.video_convert_service.post(video=self.b64_encoded_video, filename='video1.flv', callback='http://localhost:8887/').resource() self.video_key = response.key self.uid_list.append(self.video_key)
def test_extraction_with_document_stored_in_SAM(self): pdf = open(join(FOLDER_PATH, 'teste.pdf')).read() pdf64 = b64encode(pdf) sam = Restfulie.at('http://0.0.0.0:8888/').auth('test', 'test').as_('application/json') resource = sam.put(value={'file':pdf64, 'filename':'teste.pdf'}).resource() doc_key = resource.key service = Restfulie.at("http://localhost:8887/").auth('test', 'test').as_('application/json') response = service.post(doc_key=doc_key, filename='teste.pdf') sleep(5) response = service.get(key=doc_key, metadata=True).resource() metadata_key = response.metadata_key metadata_key |should_not| equal_to(None)
def it_should_allow_posting_as_xml(self): uri = 'http://coolresource/post-here' content = {'item': {'name': 'something'}} encoded_content = Dummy() with Stub() as urlencode: from urllib import urlencode urlencode({'content': "<item><name>something</name></item>"}) >> encoded_content with Mock() as urlopen: from urllib2 import urlopen urlopen(uri, encoded_content) Restfulie.at(uri).as_('application/xml').post(content) urlopen.validate()
def test_callback_extraction_service(self): pdf = open(join(FOLDER_PATH, 'teste.pdf')).read() pdf64 = b64encode(pdf) pdf64 = "dasdasih" sam = Restfulie.at('http://0.0.0.0:8888/').auth('test', 'test').as_('application/json') resource = sam.put(value={'file':pdf64, 'filename':'teste.pdf'}).resource() doc_key = resource.key service = Restfulie.at("http://localhost:8887/").auth('test', 'test').as_('application/json') response = service.post(doc_key=doc_key, filename='teste.pdf', callback_url='http://localhost:8886/', verb='POST') sleep(3) callback_log = open('/home/joao/git/metadataservice_buildout/twistd.log').read() (doc_key in callback_log) |should| equal_to(True)
def it_should_allow_posting_as_json(self): uri = 'http://coolresource/post-here' content = {"just": "testing"} encoded_content = Dummy() with Mock() as dumps: from json import dumps dumps(content) >> encoded_content with Mock() as urlopen: from urllib2 import urlopen urlopen(uri, encoded_content) Restfulie.at(uri).as_('application/json').post(content) urlopen.validate() dumps.validate()
def it_should_allow_posting_as_xml(self): uri = 'http://coolresource/post-here' content = 'some content' encoded_content = Dummy() with Mock() as urlencode: from urllib import urlencode urlencode({'content': content}) >> encoded_content with Mock() as urlopen: from urllib2 import urlopen urlopen(uri, encoded_content) Restfulie.at(uri).as_('application/xml').post(content) urlencode.validate() urlopen.validate()
def test_restfulie_at(): """ Running Restfulie.at("www.caelum.com.br") should return a Request object with this URI """ assert Restfulie.at("www.caelum.com.br")
def run(self, url, verb, video_uid): try: print "Sending callback to %s" % url restfulie = Restfulie.at(url).as_('application/json') response = getattr(restfulie, verb)(video_key=video_uid, done=False) except Exception, e: FailCallback.retry(exc=e, countdown=10)
def it_should_allow_posting_as_xml(self): uri = "http://coolresource/post-here" content = {"item": {"name": "something"}} encoded_content = Dummy() with Stub() as urlencode: from urllib import urlencode urlencode({"content": "<item><name>something</name></item>"}) >> encoded_content with Stub() as response: response.code >> 200 with Mock() as urlopen: from urllib2 import urlopen urlopen(uri, encoded_content) >> response Restfulie.at(uri).as_("application/xml").post(content) urlopen.validate()
def test_extraction_with_parameter_metadata(self): pdf = open(join(FOLDER_PATH, 'teste.pdf')).read() pdf64 = b64encode(pdf) service = Restfulie.at("http://localhost:8887/").auth('test', 'test').as_('application/json') response = service.post(file=pdf64, filename='test.pdf') resource = response.resource() resource.doc_key |should_not| equal_to(None) sleep(5) response = service.get(key=resource.doc_key, metadata=True).resource() metadata_key = response.metadata_key metadata_key |should_not| equal_to(None) sam = Restfulie.at('http://0.0.0.0:8888/').auth('test', 'test').as_('application/json') resource = sam.get(key=metadata_key).resource() resource.data.autor |should| equal_to('Jose')
def search(what): description = Restfulie.at( "http://localhost:3000/products/opensearch.xml").accepts( 'application/opensearchdescription+xml').get().resource() items = description.use("application/xml").search(searchTerms=what, startPage=1) return items
def run(self, uid, callback_url, video_link, sam_settings): self.callback_url = callback_url self.sam = Restfulie.at(sam_settings['url']).auth(*sam_settings['auth']).as_('application/json') self.destination_uid = uid self.tmp_path = "/tmp/original-%s" % uuid4() video_is_converted = False if video_link: self._download_video(video_link) else: response = self._get_from_sam(uid) self._original_video = response.data.video if not hasattr(response, 'converted'): video_is_converted = False else: video_is_converted = response.data.converted if not video_is_converted: print "Conversion started." self._process_video() print "Conversion finished." if not self.callback_url == None: print "Callback task sent." send_task('nsivideoconvert.tasks.Callback', args=(callback_url, self.destination_uid), queue='convert', routing_key='convert') else: print "No callback." return self.destination_uid else: raise VideoException("Video already converted.")
def __init__(self, *args, **kwargs): cyclone.web.RequestHandler.__init__(self, *args, **kwargs) self._load_sam_config() self._load_videoconvert_config() self._task_queue = self.settings.task_queue self.sam = Restfulie.at(self.sam_settings['url']).auth( *self.sam_settings['auth']).as_('application/json')
def it_retrieves_content_as_json(self): content = '''{"item": {"name": "product", "price": 2}}''' self.set_server_content(content, 'application/json') resource = Restfulie.at(self.content_uri).get() resource.item.name |should| equal_to('product') resource.item.price |should| be(2)
def run(self, url, verb, doc_uid, **kwargs): try: print "Sending fail callback to %s" % url restfulie = Restfulie.at(url).as_('application/json') response = getattr(restfulie, verb.lower())(doc_key=doc_uid, done=False, error=True) except Exception, e: FailCallback.retry(exc=e, countdown=10)
def run(self, task_queue, uid, document_type, callback_url, callback_verb, expire, cloudooo_settings, sam_settings): print 'Metadata extraction started...' self._sam = Restfulie.at(sam_settings['url']).auth(*sam_settings['auth']).as_('application/json') cloudooo = Server("%s/RPC2" % cloudooo_settings['url']) response = loads(self._sam.get(key=uid).body) doc = response['data']['file'] filename = response['data']['filename'] if not filename.endswith('pdf'): print "Converting document to pdf..." doc = cloudooo.convertFile(doc, filename[-3:], 'pdf') temp_doc = TemporaryFile(suffix=filename[:-3] + "pdf", delete=False) print temp_doc.name, filename temp_doc.write(decodestring(doc)) temp_doc.close() extractor = {'tcc': TccExtractor, 'event': EventExtractor} print "Extracting the metadata..." metadata = extractor[document_type](temp_doc.name).all_metadata() metadata_key = self._sam.post(value=metadata, expire=expire).resource().key response['data']['metadata_key'] = metadata_key self._sam.put(key=uid, value=response['data']).resource() print "Metadata extraction complete. Sending callback task..." send_task('nsicloudooomanager.tasks.MetadataCallback', args=(callback_url, callback_verb, uid, metadata_key), queue=task_queue, routing_key=task_queue) print "Metadata callback task sent."
def it_gets_raw_data_from_an_entry_point(self): content = "<item><name>Rich Rock Sunshine</name></item>" self.set_server_content(content) resource = Restfulie.at(self.content_uri).raw().get() resource.response.code |should| be(200) resource.response.body |should| equal_to(content)
def run(self, url, verb, video_uid, grains_keys): try: print "Sending callback to %s" % url restfulie = Restfulie.at(url).as_('application/json') response = getattr(restfulie, verb)(video_key=video_uid, grains_keys=grains_keys, done=True) except Exception, e: print "Erro no callback." Callback.retry(exc=e, countdown=10)
def __init__(self, *args, **kwargs): FunkLoadTestCase.__init__(self, *args, **kwargs) """Setting up the benchmark cycle.""" self.server_url = self.conf_get('main', 'url') self.sam = Restfulie.at('http://localhost:8888/').auth('test', 'test').as_('application/json') self.lipsum = Lipsum() self.uid_list = [] self.video_file = b64encode(open(join(FOLDER_PATH, 'input', 'rubik.flv')).read())
def __init__(self, *args, **kwargs): FunkLoadTestCase.__init__(self, *args, **kwargs) """Setting up the benchmark cycle.""" self.server_url = self.conf_get("main", "url") self.sam = Restfulie.at("http://localhost:8888/").auth("test", "test").as_("application/json") self.uid_list = [] size_multiplier = int(self.conf_get("main", "request_size")) self.data = b64encode("a" * 40 * 1024 * size_multiplier)
def run(self, url, verb, doc_uid, grains_keys, thumbnail_key, **kwargs): try: print "Sending callback to %s" % url restfulie = Restfulie.at(url).as_('application/json') response = getattr(restfulie, verb.lower())(doc_key=doc_uid, grains_keys=grains_keys, thumbnail_key=thumbnail_key, done=True) except Exception, e: Callback.retry(exc=e, countdown=10)
def clean_files(path, sam_host, sam_port, sam_user, sam_password): print 'Cleaning the files...' sam = Restfulie.at("http://%s:%s/" % (sam_host, sam_port)) sam = sam.auth(sam_user, sam_password).as_('application/json') for entry in listdir(path): if sam.get(key=entry).code == '404': remove(join(path, entry)) print 'Done.'
def run(self, url, verb, doc_uid, metadata_key): try: print "Sending metadata callback to %s" % url restfulie = Restfulie.at(url).as_('application/json') response = getattr(restfulie, verb.lower())(doc_key=doc_uid, metadata_key=metadata_key, done=True) except Exception, e: Callback.retry(exc=e, countdown=10)
def it_follows_201_response_location(self): self.set_server_post_response(code=201, location=self.content_uri) self.set_server_content( '<items><item><name>product</name><price>2</price></item></items>', 'application/xml') response = Restfulie.at(self.post_uri).as_('application/xml').\ post({'anything': 'goes here'}) response.items[0].name |should| equal_to('product') response.items[0].price |should| equal_to(2)
def _download_doc(self, doc_link): try: print "Downloading document from %s" % doc_link document = Restfulie.at(doc_link).get().body except Exception: raise DocumentDownloadException("Could not download the document from %s" % doc_link) else: print "Document downloaded." self._original_doc = b64encode(document)
def _download_video(self, video_link): try: print "Downloading video from %s" % video_link video = Restfulie.at(video_link).get().body except Exception: raise VideoDownloadException("Could not download the video from %s" % video_link) else: print "Video downloaded." return b64encode(video)
def __init__(self, *args, **kwargs): FunkLoadTestCase.__init__(self, *args, **kwargs) """Setting up the benchmark cycle.""" self.server_url = self.conf_get('main', 'url') self.user = self.conf_get('main', 'user') self.password = self.conf_get('main', 'password') self.cloudooo = Restfulie.at(self.server_url).auth(self.user, self.password).as_('application/json') self.lipsum = Lipsum() self.uid_list = [] self.document = b64encode(open(join(FOLDER_PATH, 'input', '26images-1table.odt')).read())
def test_extraction(self): pdf = open(join(FOLDER_PATH, 'teste.pdf')).read() pdf64 = b64encode(pdf) service = Restfulie.at("http://localhost:8887/").auth('test', 'test').as_('application/json') response = service.post(file=pdf64, filename='test.pdf') resource = response.resource() resource.doc_key |should_not| equal_to(None) sleep(5) response = service.get(key=resource.doc_key).resource() response.done |should| equal_to(True)
def __fetch_url(self, url=None): logger.info("Fetching url at %s", url) response = Restfulie.at(url).accepts("application/json").get() if response.code != "200" : raise Exception("Resposta do Serviço Ruim =(") data = response.resource() return data
def run(self, task_queue, video_uid, filename, callback_url, sam_settings, video_link, verb='POST'): sleep(3) self.filename = filename self.video_uid = video_uid self.callback_url = callback_url self.callback_verb = verb.lower() self.video_link = video_link self.task_queue = task_queue print video_link self.sam = Restfulie.at(sam_settings['url']).as_('application/json').auth(*sam_settings['auth']) # try: self._granulate_video()
from restfulie import Restfulie found = False visited = {} steps = 0 def solve(current): global found global visited if not found and not current.link('exit'): directions = ["start", "east", "west", "south", "north"] for direction in directions: link = current.link(direction) if not found and link and not visited.get(link.href): visited[link.href] = True solve(link.follow().get()) else: print "FOUND!" found = True current = Restfulie.at('http://amundsen.com/examples/mazes/2d/five-by-five/' ).accepts("application/xml").get() solve(current)
def should_return_a_dsl_object(self): assert type(Restfulie.at("www.caelum.com.br")) == Dsl