def upload_photo(form_data, user_id, user_token, lang, create=True): api_url = join(settings.WS_BASE_URL, PRIVATE_API_SCHEMA_URL) chunk_size = config.PHOTO_UPLOAD_CHUNK_SIZE # request headers authorization = {'Authorization': 'Token {}'.format(user_token)} headers = dict(authorization) headers.update({ 'Accept-Language': lang, 'content_type': MULTIPART_CONTENT }) transports = HTTPTransport(credentials=authorization, headers=headers) client = Client(transports=[transports]) # get api schema schema_cache_key = "{}_{}".format(CACHE_SCHEMA_PREFIX_KEY, user_id) schema = cache.get(schema_cache_key) if not schema: schema = client.get(api_url) cache_set(schema_cache_key, schema) # get image to upload upload_id = form_data.pop('upload_id') image = MyChunkedUpload.objects.get(upload_id=upload_id) image_file = image.file filename = image.filename # request parameters data = {'filename': filename} offset, chunks = 0, image_file.chunks(chunk_size) request_path = ['photos', 'upload', 'update'] img_id = 0 for chunk_file in chunks: data.update(**{'file': ContentFile(chunk_file)}) client.transports[0].headers._data[ 'Content-Range'] = 'bytes {}-{}/{}'.format( offset, offset + len(chunk_file), image_file.size) response = client.action(schema, request_path, params=data) offset = response['offset'] img_id = response['id'] data.update({'id': img_id}) request_path = ['photos', 'upload', 'chunk', 'update'] request_path = ['photos', 'upload', 'chunk', 'create'] data.update({'md5': _checksum_file(image_file, chunk_size)}) client.action(schema, request_path, params=data) # Request is not multipart, so we remove the header, otherwise uwsgi doesn't works client.transports[0].headers._data.pop('content_type', None) # upload photo information form_data['image'] = img_id form_data['original_file_name'] = filename if create: form_data['owner'] = user_id client.action(schema, ['photos', 'create'], params=form_data) else: client.action(schema, ['photos', 'partial_update'], params=form_data)
class DAMPublicWebService(object): """ For api requests without token """ def __init__(self, request): super(DAMPublicWebService, self).__init__() api_url = join(settings.WS_BASE_URL, PUBLIC_API_SCHEMA_URL) self.request = request self.client = Client() self.schema = self.client.get(api_url) def password_reset(self, params): path_list = ['auth', 'password', 'reset', 'create'] return self.client.action(self.schema, path_list, params=params) def password_reset_confirm(self, params): path_list = ['auth', 'password', 'reset', 'confirm', 'create'] return self.client.action(self.schema, path_list, params=params)
def _action(self, fiware_service, fiware_service_path, keys, params=None, validate=True): headers = { 'Fiware-Service': fiware_service, 'Fiware-ServicePath': fiware_service_path } transport = HTTPTransport(headers=headers) client = Client(transports=[transport]) document = client.get(self.schema_endpoint) return client.action(document, keys, params, validate)
import requests from coreapi import Client from coreapi.auth import TokenAuthentication # """ http post http://localhost:9000/timeside/api-token-auth/ username=admin password=admin """ url = 'http://localhost:9000/timeside/api-token-auth/' auth={'username':'******', 'password':'******'} r = requests.post(url, data=auth) token=r.json()['token'] #coreapi client with the right token auth = TokenAuthentication( scheme='Token', token=token ) client = Client(auth=auth) #testing several request to the TimeSide core API schema = client.get('http://localhost:9000/timeside/api/schema/') keys = ['api', 'items', 'create'] params = {'title':'fooTest'} client.action(schema,keys,params) keys = ['api', 'items', 'list'] data = client.action(schema,keys) for item in data: print(item['title'] + ' ' + item['uuid'])
for event in d: event['scroll'] = scroll_id print(event) try: client.action(schema, ['events', 'create'], params=event) except Exception as e: print(e) finally: pass # create(scroll, title, text, datetime, image, [mediatype], # [source_url], [source_date], [content_url]) # print('var oldRadioNews =', json.dumps(d, sort_keys=True, indent=4), ";\n") if __name__ == "__main__": auth = BasicAuthentication( username='******', password='******' ) client = Client(auth=auth) print(client) schema = client.get('http://127.0.0.1:8000/schema') print(schema) new_scroll = client.action(schema, ['api', '0', 'scrolls', 'create'], params={"title": "WWII Radio News"}) scroll_d = dict(new_scroll) scroll_url = "http://127.0.0.1:8000/scrolls/{}/".format(scroll_d['id']) print(scroll_url) main(client, scroll_url)
#coreapi client with the right token auth = TokenAuthentication(scheme='Token', token=token) client = Client(auth=auth) #schema of the API schema = client.get('http://localhost:9000/timeside/api/schema/') #Item keys = ['api', 'items', 'create'] params = { 'title': 'Billie Jean', 'description': 'Music from Michael Jackson', 'external_uri': 'http://www.youtube.com/watch?v=Zi_XLOBDo_Y', 'provider': '/timeside/api/providers/042d0121-456a-4b7d-a993-f8b040f6fc9c/' } item = client.action(schema, keys, params) #Ajout dans la selection WASABI keys = ['api', 'selections', 'partial_update'] params = { 'uuid': '3c58f084-a16d-470a-afc3-4a2341c46e40', 'items': ['/timeside/api/items/' + item['uuid'] + '/'] } selec = client.action(schema, keys, params) #Experience keys = ['api', 'experiences', 'create'] params = { 'title': 'spectrogramme_test', 'presets': ['/timeside/api/presets/eaccfb5b-e1cb-4b66-a49f-831564e4c789/'] }
#Connecting to JASPAR client = Client() document = client.get('http://jaspar.genereg.net/api/v1/docs/') except ConnectionError as e: print e print "Database is not responding. Try again later. " print "Profile Inference Search Started....." for record in SeqIO.parse(inputfile, "fasta"): recordseq = record.seq action = ["infer", "read"] params = { "sequence": '%s' % recordseq, } result = client.action(document, action, params=params) data1 = record.id + "#" + str(result) with open(outputfilename, 'a') as the_file: the_file.write(str(data1) + '\n') except ValueError: if not inputfile: raise ValueError('No sequence file') else: raise ValueError('check your sequence format') finally: print "Profile Inference Search Finished" resultfinle = open(outputfilename, 'r')
def test_keys_should_access_a_link(doc): client = Client() with pytest.raises(LinkLookupError): client.action(doc, 'dict')
def test_keys_should_be_valid_indexes(doc): client = Client() with pytest.raises(LinkLookupError): client.action(doc, 'dummy')
def test_keys_should_be_a_list_of_strings_or_ints(doc): client = Client() with pytest.raises(TypeError): client.action(doc, ['nested', {}])
def test_keys_should_be_a_list_or_string(doc): client = Client() with pytest.raises(TypeError): client.action(doc, True)
class API(object): def __init__(self, host, port, user, token, params_callback=None, message_callback=None): self.base_url = self.get_service_url(host, port) self.client = Client( decoders=[ codecs.CoreJSONCodec(), # application/vnd.coreapi+json codecs.JSONCodec() # application/json ], transports=[ transports.CommandHTTPSTransport( auth=auth.CommandClientTokenAuthentication(user=user, token=token, scheme='Token', domain='*'), params_callback=params_callback, message_callback=message_callback) # https only ]) self.schema = self.client.get(self.base_url) def get_service_url(self, host, port): return "https://{}:{}/".format(host, port) def _normalize_data(self, data): if isinstance(data, dict): for key, value in data.items(): data[key] = self._normalize_data(value) elif isinstance(data, list): for index, value in enumerate(data): data[index] = self._normalize_data(value) else: # Scalar value if isinstance(data, str) and len(data) == 0: data = None return data def _format_params(self, params): params = self._normalize_data(params) for key, value in params.items(): if isinstance(value, dict): params[key] = json.dumps(value) elif isinstance(value, (list, tuple)): params[key] = json.dumps(list(value)) return params def execute(self, action, params=None): if not params: params = {} try: action = action.split(' ') if isinstance(action, str) else action params = self._format_params(params) return self.client.action(self.schema, action, params=params) except exceptions.ErrorMessage as error: raise CommandError("API request error: {}\n".format(error))
class DAMWebService(object): """ For api requests with token """ def __init__(self, request): super(DAMWebService, self).__init__() api_url = join(settings.WS_BASE_URL, PRIVATE_API_SCHEMA_URL) self.request = request self.user_id = request.user.id # initialize api client with user token user_params = cache.get("{}_{}".format(CACHE_USER_PROFILE_PREFIX_KEY, self.user_id)) authorization = {} if user_params and user_params.get('token'): authorization = { 'Authorization': 'Token {}'.format(user_params.get('token')) } headers = dict(authorization) headers.update({ 'Accept-Language': request.META.get('HTTP_ACCEPT_LANGUAGE', request.LANGUAGE_CODE) }) transports = HTTPTransport( credentials=authorization, headers=headers, response_callback=self._callback_client_transport) self.client = Client(transports=[transports]) # get api schema schema_cache_key = "{}_{}".format(CACHE_SCHEMA_PREFIX_KEY, self.user_id) self.schema = cache.get(schema_cache_key) if not self.schema: self.schema = self.get_or_logout(api_url) cache.set(schema_cache_key, self.schema) def get_client_schema(self): return self.schema def _callback_client_transport(self, response): self.transport_status_code = response.status_code def get_or_logout(self, url): """ Method to get the schema of the api according the client, which depends of the user who requests. Raise a ServiceClientException on error customizing the error code. """ try: response = self.client.get(url) return response except ParameterError as e: raise ServiceClientException(HTTP_BAD_REQUEST, e) except CoreAPIException as e: raise ServiceClientException(self.transport_status_code, e) def action_or_logout(self, path_list, params, use_cache=False, clear_cache=False): """ to execute the action """ # get response from cache if use cache is if use_cache: cache_suffix_key = "_".join([ "{}_{}".format(key, params[key]) for key in sorted(params.keys()) ]) cache_key = "{}_{}_{}".format("_".join(path_list), self.user_id, cache_suffix_key) response = cache.get(cache_key) if response: return response # do request through api client try: response = self.client.action(self.schema, path_list, params=params) # clear all request related entries in cache if clear_cache: cache_delete_startswith(path_list[0]) return response # if response successful status code if use_cache: cache_set(cache_key, response) return response except ParameterError as e: raise ServiceClientException(HTTP_BAD_REQUEST, e) except CoreAPIException as e: raise ServiceClientException(self.transport_status_code, e) # auth def password_change(self, params): return self.action_or_logout(['auth', 'password', 'change', 'create'], params=params) # users def get_user_info(self): return self.action_or_logout(['whoami', 'list'], {}) def get_user(self, user_id): return self.action_or_logout(['users', 'read'], params={'id': user_id}) def get_users_list(self, **kwargs): return self.action_or_logout(['users', 'list'], params=kwargs) def user_create(self, params): return self.action_or_logout(['users', 'create'], params=params) def user_edit(self, params): return self.action_or_logout(['users', 'partial_update'], params=params) def delete_user(self, user_id): return self.action_or_logout(['users', 'delete'], params={'id': user_id}) # groups def get_groups_list(self, **kwargs): return self.action_or_logout(['groups', 'list'], params=kwargs) # albums def get_albums_list(self, **kwargs): return self.action_or_logout(['albums', 'list'], params=kwargs) def get_album(self, album_id): return self.action_or_logout(['albums', 'read'], params={'id': album_id}) def create_album(self, params): return self.action_or_logout(['albums', 'create'], params) def update_album(self, params): return self.action_or_logout(['albums', 'partial_update'], params) def delete_album(self, album_id): return self.action_or_logout(['albums', 'delete'], params={'id': album_id}) # photos def search_photos_list(self, **kwargs): return self.action_or_logout(['search', 'list'], params=kwargs) def get_photos_list(self, **kwargs): return self.action_or_logout(['photos', 'list'], params=kwargs) def get_photo(self, photo_id): return self.action_or_logout(['photos', 'read'], params={'id': photo_id}) def create_photo(self, params): return self.action_or_logout(['photos', 'create'], params=params) def update_photo(self, params): return self.action_or_logout(['photos', 'partial_update'], params) def update_photo_multiple(self, params): return self.action_or_logout(['photos', 'addition', 'partial_update'], params) def delete_photo(self, photo_id): return self.action_or_logout(['photos', 'delete'], params={'id': photo_id}) # galleries def get_galleries_list(self, **kwargs): return self.action_or_logout(['galleries', 'list'], params=kwargs) def get_gallery(self, gallery_id): return self.action_or_logout(['galleries', 'read'], params={'id': gallery_id}) def create_gallery(self, params): return self.action_or_logout(['galleries', 'create'], params=params) def update_gallery(self, params): return self.action_or_logout(['galleries', 'partial_update'], params) def delete_gallery(self, gallery_id): return self.action_or_logout(['galleries', 'delete'], params={'id': gallery_id}) # categories def get_categories_simple_list(self, **kwargs): """ Categories list with only 'id' and 'title' information """ return self.action_or_logout(['categories', 'flat', 'list'], params=kwargs, use_cache=True) def get_categories_list(self, **kwargs): return self.action_or_logout(['categories', 'list'], params=kwargs, use_cache=True) def get_category(self, category_id): return self.action_or_logout(['categories', 'read'], params={'id': category_id}, use_cache=True) def create_category(self, params): return self.action_or_logout(['categories', 'create'], params=params, clear_cache=True) def update_category(self, params): return self.action_or_logout(['categories', 'update'], params, clear_cache=True) def delete_category(self, category_id): return self.action_or_logout(['categories', 'delete'], params={'id': category_id}, clear_cache=True) # metadata def get_names_list(self, **kwargs): """ Tag-Name list filtered by a part of its content """ return self.action_or_logout(['names', 'list'], params=kwargs) def get_keywords_list(self, **kwargs): """ Tag-Name list filtered by a part of its content """ return self.action_or_logout(['keywords', 'list'], params=kwargs) # copyrights def get_authors_list(self, **kwargs): """ Photo author list filtered by a part of its name """ return self.action_or_logout(['authors', 'list'], params=kwargs) def get_copyrights_list(self, **kwargs): """ Copyright list filtered by a part of its name """ return self.action_or_logout(['copyrights', 'list'], params=kwargs) def get_restrictions_list(self, **kwargs): """ Restriction list filtered by a part of its title """ return self.action_or_logout(['restrictions', 'list'], params=kwargs) # link def create_link(self, params): return self.action_or_logout(['link', 'create'], params=params) def delete_link(self, params): return self.action_or_logout(['link', 'delete'], params=params) # logger def logger_view(self, params): params['action'] = ACTION_VIEW_PHOTO return self.logger_action(params) def logger_download(self, params): params['action'] = ACTION_DOWNLOAD_PHOTO return self.logger_action(params) def logger_action(self, params): return self.action_or_logout(['logger', 'create'], params=params) def get_log_list(self, **kwargs): return self.action_or_logout(['logger', 'list'], params=kwargs) def get_photo_upload_log_list(self, **kwargs): return self.action_or_logout(['photos', 'upload', 'list'], params=kwargs) # exports def export_logger(self, kwargs): return self.action_or_logout(['exports', 'logger', 'list'], params=kwargs) # flickr def flickr_import(self, params): return self.action_or_logout(['photos', 'import', 'album', 'create'], params=params) # photo type def get_photo_type_list(self, **params): return self.action_or_logout(['types', 'list'], params=params)
# """ http post http://localhost:9000/timeside/api-token-auth/ username=admin password=admin """ url = 'http://localhost:9000/timeside/api-token-auth/' auth = {'username': '******', 'password': '******'} r = requests.post(url, data=auth) token = r.json()['token'] #coreapi client with the right token auth = TokenAuthentication(scheme='Token', token=token) client = Client(auth=auth) #schema of the API schema = client.get('http://localhost:9000/timeside/api/schema/') #Getting uuid of Provider and WASABI selection keys = ['api', 'selections', 'list'] for selec in client.action(schema, keys): if selec['title'] == 'WASABI': WASABI_selection = selec selection_uuid = WASABI_selection['uuid'] keys = ['api', 'providers', 'list'] for prv in client.action(schema, keys): if prv['pid'] == 'youtube': youtube_provider = prv provider_uri = '/timeside/api/providers/' + youtube_provider['uuid'] + '/' #creation of an Item for Michael Jackson's Thriller track using YouTube's URL keys = ['api', 'items', 'create'] external_youtube_uri = 'https://www.youtube.com/watch?v=sOnqjkJTMaA'