我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用twisted.internet.defer.returnValue()。
def inlineCallbacks(f,*args, **kwargs): # ... try: gen = f(*args, **kwargs) except defer._DefGen_Return: raise TypeError( "inlineCallbacks requires %r to produce a generator; instead" "caught returnValue being used in a non-generator" % (f,)) if not isinstance(gen, types.GeneratorType): raise TypeError( "inlineCallbacks requires %r to produce a generator; " "instead got %r" % (f, gen)) return defer._inlineCallbacks(None, gen, defer.Deferred()) # ... # ... # ...
def as_view(path): def decorator(func): # .. path_name, klass_name = (path.split(':')) # ... @inlineCallbacks def wrapper(router, request, *args, **kwargs): # ... module = importlib.import_module(path_name) Klass = getattr(module,klass_name) klass = Klass(router, request,*args, **kwargs) # .. result = yield defer.maybeDeferred(klass) defer.returnValue(result) # .. # _conspect_name(wrapper, klass_name) _conspect_name(wrapper, func.__name__) _conspect_param(wrapper, func) _conspect_param_defaults(wrapper, func) return wrapper return decorator
def response(self,result): # ... response = { 'id': b'1', 'jsonrpc': self.DEFAULT_JSONRPC } # .. response.update(result) response = yield json.dumps( response, cls=utils.JSONRPCEncoder ) # ... defer.returnValue(response) # ... # ... # ...
def call(self, request): # ... self._init_request_resource(request) self._init_request_method(request) # ... rdata = yield self._init_json_data(request) # ... params = self._get_params(rdata) method = self._get_method(rdata) # .. result = yield defer.maybeDeferred(method, request, **params) result = self._make_result(result) # ... defer.returnValue(result) # ..init # ==================================
def read_content(queue): frame = yield queue.get() header = frame.payload children = [] for i in range(header.weight): content = yield read_content(queue) children.append(content) size = header.size read = 0 buf = six.StringIO() while read < size: body = yield queue.get() content = body.payload.content # if this is the first instance of real binary content, convert the string buffer to BytesIO # Not a nice fix but it preserves the original behaviour if six.PY3 and isinstance(content, bytes) and isinstance(buf, six.StringIO): buf = six.BytesIO() buf.write(content) read += len(content) defer.returnValue(Content(buf.getvalue(), children, header.properties.copy()))
def prepareClient(client, username, password): yield client.authenticate(username, password) channel = yield client.channel(1) yield channel.channel_open() yield channel.exchange_declare(exchange=servicesExchange, type="direct") yield channel.exchange_declare(exchange=responsesExchange, type="direct") pfactory = TBinaryProtocol.TBinaryProtocolFactory() # To trigger an unroutable message error (caught in the above # gotTransportError errback), change the routing key (i.e., # calculatorKey) in the following to be something invalid, like # calculatorKey + 'xxx'. thriftClient = yield client.createThriftClient(responsesExchange, servicesExchange, calculatorKey, tutorial.Calculator.Client, iprot_factory=pfactory, oprot_factory=pfactory) defer.returnValue(thriftClient)
def start(cls, net, factory, bitcoind, peer_ports, merged_urls): self = cls() self.n = node.Node(factory, bitcoind, [], [], net) yield self.n.start() self.n.p2p_node = node.P2PNode(self.n, port=0, max_incoming_conns=1000000, addr_store={}, connect_addrs=[('127.0.0.1', peer_port) for peer_port in peer_ports]) self.n.p2p_node.start() wb = work.WorkerBridge(node=self.n, my_pubkey_hash=random.randrange(2**160), donation_percentage=random.uniform(0, 10), merged_urls=merged_urls, worker_fee=3, args=math.Object(donation_percentage=random.uniform(0, 10), address='foo', worker_fee=3, timeaddresses=1000), pubkeys=main.keypool(), bitcoind=bitcoind) self.wb = wb web_root = resource.Resource() worker_interface.WorkerInterface(wb).attach_to(web_root) self.web_port = reactor.listenTCP(0, server.Site(web_root)) defer.returnValue(self)
def get_height_rel_highest_func(bitcoind, factory, best_block_func, net): if '\ngetblock ' in (yield deferral.retry()(bitcoind.rpc_help)()): @deferral.DeferredCacher @defer.inlineCallbacks def height_cacher(block_hash): try: x = yield bitcoind.rpc_getblock('%x' % (block_hash,)) except jsonrpc.Error_for_code(-5): # Block not found if not p2pool.DEBUG: raise deferral.RetrySilentlyException() else: raise defer.returnValue(x['blockcount'] if 'blockcount' in x else x['height']) best_height_cached = variable.Variable((yield deferral.retry()(height_cacher)(best_block_func()))) def get_height_rel_highest(block_hash): this_height = height_cacher.call_now(block_hash, 0) best_height = height_cacher.call_now(best_block_func(), 0) best_height_cached.set(max(best_height_cached.value, this_height, best_height)) return this_height - best_height_cached.value else: get_height_rel_highest = HeightTracker(best_block_func, factory, 5*net.SHARE_PERIOD*net.CHAIN_LENGTH/net.PARENT.BLOCK_PERIOD).get_height_rel_highest defer.returnValue(get_height_rel_highest)
def unmountLocal(self, mountPoint): print("Unmounting...") try: # errorCode = self.libc.umount(mountPoint, None) errorCode = call(["umount", "-l", mountPoint]) result = "Unmount " + mountPoint + " result: " + str(errorCode) except Exception as msg: result = "Unmounting error:", msg print(result) yield returnValue(result)
def unmountRemote(self, mountPoint): print("Unmounting...") try: # errorCode = self.libc.umount(mountPoint, None) errorCode = call(["umount", "-l", mountPoint]) result = "Unmount " + mountPoint + " result: " + str(errorCode) except Exception as msg: result = "Unmounting error:", msg print(result) yield returnValue(result)
def build_mixnet_nodes(pki, params, rand_reader): """ i am a helper function used to build a testing mix network. given the sphinx params and a node_factory i will return a dictionary of NodeDescriptors, a dictionary of node protocols and a dictionary of addr -> node protocol """ mix_size = 5 nodes = {} addr_to_nodes = {} for i in range(mix_size): addr = i public_key, private_key = generate_node_keypair(rand_reader) replay_cache = PacketReplayCacheDict() key_state = MixKeyState(public_key, private_key) params = SphinxParams(5, 1024) # 5 hops max and payload 1024 bytes transport = DummyTransport(i) node_id = generate_node_id(rand_reader) threshold_count = 100 mix = ThresholdMixNode(threshold_count, node_id, replay_cache, key_state, params, pki, transport) yield mix.start() nodes[node_id] = mix addr_to_nodes[addr] = mix defer.returnValue((nodes, addr_to_nodes))
def do_build_transport(self): if len(self.tor_control_unix_socket) == 0: assert len(self.onion_tcp_interface_ip) != 0 tor_controller_endpoint_desc = "tcp:%s:%s" % (self.tor_control_tcp_host, self.tor_control_tcp_port) else: tor_controller_endpoint_desc = "unix:%s" % self.tor_control_unix_socket tor_controller_endpoint = endpoints.clientFromString(self.reactor, tor_controller_endpoint_desc) tor = yield txtorcon.connect(self.reactor, control_endpoint=tor_controller_endpoint) onion_tcp_port = 0 if len(self.onion_unix_socket) == 0: onion_tcp_port = yield txtorcon.util.available_tcp_port(self.reactor) hs = txtorcon.EphemeralHiddenService(["%s %s:%s" % (self.onion_service_port, self.onion_tcp_interface_ip, onion_tcp_port)]) else: hs = txtorcon.EphemeralHiddenService(["%s unix:%s" % (self.onion_service_port, self.onion_unix_socket)]) yield hs.add_to_tor(tor.protocol) transport = OnionTransport(self.reactor, tor, onion_host=hs.hostname.encode('utf-8'), onion_port=self.onion_service_port, onion_key=hs.private_key.encode('utf-8'), onion_tcp_interface_ip=self.onion_tcp_interface_ip, onion_tcp_port=onion_tcp_port) yield hs.remove_from_tor(tor.protocol) defer.returnValue(transport)
def _get_or_create_mailbox(self, mailbox_name): """ Avoid creating variations of the case. If there's already a 'Sent' folder, do not create 'SENT', just return that. """ name = yield self._get_case_insensitive_mbox(mailbox_name) if name is None: name = mailbox_name yield self.account.add_mailbox(name) mailbox = yield self.account.get_collection_by_mailbox( name) # Pixelated expects the mailbox wrapper; # it should limit itself to the Mail API instead. # This is also a smell that the collection-mailbox-wrapper # distinction is not clearly cut. defer.returnValue(mailbox.mbox_wrapper)
def do_msg_status(self, userid, mbox, msgid): account = self._get_account(userid) msg = yield account.get_message_by_msgid(mbox, msgid) if msg is None: raise Exception("Not found message id: " + msgid) headers = msg.get_headers() encryption = headers.get(IncomingMail.LEAP_ENCRYPTION_HEADER, '') signature = headers.get(IncomingMail.LEAP_SIGNATURE_HEADER, '') status = {} pubkey_re = re.compile(' pubkey="([0-9A-F]*)"') fingerprint = first(pubkey_re.findall(signature)) status['signature'] = signature.split(';')[0] status['sign_fp'] = fingerprint status['encryption'] = encryption if ((IncomingMail.LEAP_ENCRYPTION_DECRYPTED == encryption) and (IncomingMail.LEAP_SIGNATURE_VALID == status['signature'])): status['secured'] = True else: status['secured'] = False defer.returnValue(status)
def authenticate(self): uri = self._api.get_handshake_uri() met = self._api.get_handshake_method() self.log.debug('%s to %s' % (met, uri)) params = self._srp_auth.get_handshake_params() handshake = yield self._request(self._agent, uri, values=params, method=met) self._srp_auth.process_handshake(handshake) uri = self._api.get_authenticate_uri(login=self.username) met = self._api.get_authenticate_method() self.log.debug('%s to %s' % (met, uri)) params = self._srp_auth.get_authentication_params() auth = yield self._request(self._agent, uri, values=params, method=met) uuid, token = self._srp_auth.process_authentication(auth) self._srp_auth.verify_authentication() self._uuid = uuid self._token = token defer.returnValue(OK)
def _stop_vpn(self, restart=False): """ Stops the openvpn subprocess. Attempts to send a SIGTERM first, and after a timeout it sends a SIGKILL. :param restart: whether this stop is part of a hard restart. :type restart: bool """ # TODO how to return False if this fails # XXX maybe return a deferred if self._vpnproc is None: self.log.debug('Tried to stop VPN but no process found') defer.returnValue(False) self._vpnproc.restarting = restart self.__stop_pre_down(self._vpnproc) stopped = yield self._vpnproc.terminate_or_kill() defer.returnValue(stopped)
def do_get_cert(self, username): try: _, provider = username.split('@') except ValueError: if not username: raise ValueError('Need an username. are you logged in?') raise ValueError(username + ' is not a valid username, it should' ' contain an @') # fetch vpn cert and store bonafide = self.parent.getServiceNamed("bonafide") _, cert_str = yield bonafide.do_get_vpn_cert(username) cert_path = get_vpn_cert_path(provider) cert_dir = os.path.dirname(cert_path) if not os.path.exists(cert_dir): os.makedirs(cert_dir, mode=0700) with open(cert_path, 'w') as outf: outf.write(cert_str) check_and_fix_urw_only(cert_path) defer.returnValue({'get_cert': 'ok'})
def do_list(self): bonafide = self.parent.getServiceNamed("bonafide") _providers = yield bonafide.do_provider_list() providers = [p['domain'] for p in _providers] provider_dict = {} for provider in providers: try: config = yield bonafide.do_provider_read(provider, 'eip') except ValueError: continue gateways = GatewaySelector( config.gateways, config.locations, preferred={'cc': self._cco, 'loc': self._loc} ) provider_dict[provider] = gateways.get_sorted_gateways() defer.returnValue(provider_dict)
def _get_inactive_private_keys(self): """ Return all inactive private keys bound to address, that are stored locally. This can be used to attempt decryption from multiple keys. :return: A Deferred which fires the list of inactive keys sorted according to their expiry dates. :rtype: Deferred """ all_keys = yield self.get_all_keys(private=True) inactive_keys = filter(lambda _key: not _key.is_active(), all_keys) inactive_keys = \ sorted(inactive_keys, key=lambda _key: _key.expiry_date) defer.returnValue(inactive_keys)
def start(cls, net, factory, bitcoind, peer_ports, merged_urls): self = cls() self.n = node.Node(factory, bitcoind, [], [], net) yield self.n.start() self.n.p2p_node = node.P2PNode(self.n, port=0, max_incoming_conns=1000000, addr_store={}, connect_addrs=[('127.0.0.1', peer_port) for peer_port in peer_ports]) self.n.p2p_node.start() wb = work.WorkerBridge(node=self.n, my_pubkey_hash=random.randrange(2**160), donation_percentage=random.uniform(0, 10), merged_urls=merged_urls, worker_fee=3) self.wb = wb web_root = resource.Resource() worker_interface.WorkerInterface(wb).attach_to(web_root) self.web_port = reactor.listenTCP(0, server.Site(web_root)) defer.returnValue(self)
def parse_clients_list(file_path): trusted_clients = None # @TODO create a service to read trusted clients from DB try: trusted_clients = open(file_path, 'r').readlines() trusted_clients = map(lambda c: c.replace('\n', ''), trusted_clients) except IOError: _clients = [] log.warn("Trusted clinets list not found.") clients_list = {} if trusted_clients: for row in csv.reader(trusted_clients, dialect='pipes', quotechar='!'): _host, _user, _pass = row if ip_re.match(_host): _ip = _host else: _host = prepare_url(_host) parsed_url = urlparse(_host) _ip = yield client.getHostByName(parsed_url.netloc) clients_list[_ip] = {'host': _host, 'user': _user, 'pass': _pass} defer.returnValue(clients_list)
def connect_service_account(self): """ Make a new connection to the LDAP backend server using the credentials of the service account :return: A Deferred that fires a `LDAPClient` instance """ client = yield connectToLDAPEndpoint(reactor, self.proxied_endpoint_string, LDAPClient) if self.use_tls: client = yield client.startTLS() try: yield client.bind(self.service_account_dn, self.service_account_password) except ldaperrors.LDAPException, e: # Call unbind() here if an exception occurs: Otherwise, Twisted will keep the file open # and slowly run out of open files. yield client.unbind() raise e defer.returnValue(client)
def get_item_by_id(self, item_id): """ Returns a handler object that can be used to send messages to an item. :param item_id: globally unique id of the item :return: a proxy object for the item """ if not self._reactor.running: raise Exception("You must call parlay.utils.setup() at the beginning of a script!") def find(): g = self._find_item_info(self.discovery, item_id, "ID") item_disc = next(g) return self._proxy_item(item_disc) try: defer.returnValue(find()) except StopIteration: # discover and try again try: yield self.discover(force=False) defer.returnValue(find()) except StopIteration: raise KeyError("Couldn't find item with id " + str(item_id))
def get_item_by_name(self, item_name): """ Returns a handler object that can be used to send messages to an item. :param item_name: globally unique name of the item :return: a proxy object for the item """ if not self._reactor.running: raise Exception("You must call parlay.utils.setup() at the beginning of a script!") def find(): g = self._find_item_info(self.discovery, item_name, "NAME") item_disc = next(g) return self._proxy_item(item_disc) try: defer.returnValue(find()) except StopIteration: # discover and try again try: yield self.discover(force=False) defer.returnValue(find()) except StopIteration: raise KeyError("Couldn't find item with name " + str(item_name))
def get_all_items_with_name(self, item_name): """ Returns a handler object that can be used to send messages to an item. :param item_name: globally unique name of the item :return: a proxy object for the item """ if not self._reactor.running: raise Exception("You must call parlay.utils.setup() at the beginning of a script!") result = [self._proxy_item(x) for x in self._find_item_info(self.discovery, item_name, "NAME")] if len(result) == 0: # retry after discover if it fails yield self.discover(force=False) result = [self._proxy_item(x) for x in self._find_item_info(self.discovery, item_name, "NAME")] defer.returnValue(result)
def get_property_name(self, to, requested_property_id): """ Sends a message down the serial line requesting the command name of a given command ID, used in discovery protocol :param to: destination item ID :param requested_property_id: property ID that we want to know the name of :return: name of the property from Embedded Core """ try: response = yield self.send_command(to, command_id=GET_PROPERTY_NAME, params=["property_id"], data=[requested_property_id]) except Exception as e: logger.error("[PCOM] Unable to find property name for property {0} because of exception: {1}".format( requested_property_id, e)) defer.returnValue(None) # The data in the response message will be a list, # the property name should be in the 0th position # and strip the NULL byte. try: defer.returnValue(response.data[0]) except IndexError: logger.error("Response from embedded board during discovery sequence did not return data in " "expect format. Expected at least one data field, received: {0}".format(response.data)) defer.returnValue(None)
def get_property_desc(self, to, requested_property_id): """ Sends a message to the embedded board requesting the property description for a specified property ID :param to: item ID to send the message to :param requested_property_id: property ID to get the description of :return: """ try: response = yield self.send_command(to, command_id=GET_PROPERTY_DESC, params=["property_id"], data=[requested_property_id]) except Exception as e: logger.error("[PCOM] Unable to find property description for property {0} in item {1} because of exception:" "{2}".format(requested_property_id, to, e)) defer.returnValue(None) try: defer.returnValue(response.data[0]) except IndexError: logger.error("Response from embedded board during discovery sequence did not return data in expect format." " Expected at least one data field, received: {0}".format(response.data)) defer.returnValue(None)
def get_command_name(self, to, requested_command_id): """ Sends a messge down the serial line requesting the property name of a given property ID, used in discovery protocol :param to: destination ID :param requested_command_id: command ID that we want to know the name of :return: name from Embedded Core """ try: response = yield self.send_command(to, command_id=GET_COMMAND_NAME, params=["command_id"], data=[requested_command_id]) except Exception as e: logger.error("[PCOM] Unable to find command name for command {0} in item {1} because of exception:" "{2}".format(requested_command_id, to, e)) defer.returnValue(None) # The data in the response message will be a list, # the command name should be in the 0th position try: defer.returnValue(response.data[0]) except IndexError: logger.error("Response from embedded board during discovery sequence did not return data in expect format." " Expected at least one data field, received: {0}".format(response.data)) defer.returnValue(None)
def get_command_input_param_format(self, to, requested_command_id): """ Given a command ID and item ID, sends a message to the item ID requesting the format of its input parameters. This functions should return a string that describes each parameter. NOTE: variable arrays are indicated with a *. Eg. A list of ints would be "*i". See format string details for character->byte translation. :param to: destination item ID :param requested_command_id: command ID that we want the parameter format of :return: format string describing input parameters """ try: response = yield self.send_command(to, command_id=GET_COMMAND_INPUT_PARAM_FORMAT, params=["command_id"], data=[requested_command_id]) except Exception as e: logger.error("[PCOM] Unable to find command input format for command {0} in item {1} because of exception:" "{2}".format(requested_command_id, to, e)) defer.returnValue(None) r_val = '' if len(response.data) == 0 else response.data[0] defer.returnValue(r_val)
def get_command_input_param_names(self, to, requested_command_id): """ Given an item ID and a command ID, requests the parameter names of the command from the item. Returns a list of names (comma delimited) that represent the parameter names. TODO: change return value to string? Eg. "frequency,duty cycle" :param to: destination item ID :param requested_command_id: command id to find the parameter names of :return: a list of parameter names """ try: response = yield self.send_command(to, command_id=GET_COMMAND_INPUT_PARAM_NAMES, params=["command_id"], data=[requested_command_id]) except Exception as e: logger.error("[PCOM] Unable to find command input parameter names for command {0} in item {1} because of " "exception: {2}".format(requested_command_id, to, e)) defer.returnValue(None) param_names = [] if len(response.data) == 0 else [x.strip() for x in response.data[0].split(',')] defer.returnValue(param_names)
def get_property_type(self, to, requested_property_id): """ Given a property ID, requests the property's type from the item ID. Gets back a format string. :param to: destination item ID :param requested_property_id: property ID that we want the type of :return: format string describing the type """ try: response = yield self.send_command(to, command_id=GET_PROPERTY_TYPE, params=["property_id"], data=[requested_property_id]) except Exception as e: logger.error("[PCOM] Unable to find property type for property {0} in item {1} because of exception: " "{2}".format(requested_property_id, to, e)) defer.returnValue(None) r_val = '' if len(response.data) == 0 else response.data[0] defer.returnValue(r_val)
def deferred_render_PUT(self, request): """ Update values of a set of tags (values and tags are given in a JSON payload) on objects that match a query (query is given in the URI). @param request: The incoming C{twisted.web.server.Request} request. @return: A L{Deferred} which will fire with C{None} when the request has completed. The deferred may errback for a variety of reasons, for example an invalid query, the mention of a non-existent tag or a tag that the caller does not have CREATE permission for. """ usage = registry.findUsage(httpValueCategoryName, 'PUT', ValuesResource) requestObject = ValuesQuerySchema.createFromRequest(request, usage) yield self.facadeClient.updateValuesForQueries( self.session, requestObject) request.setResponseCode(usage.successCode) defer.returnValue(None)
def deferred_render_DELETE(self, request): """ Handle a DELETE request for /values with a query and a list of wanted tags. @param request: The incoming C{twisted.web.server.Request} request. @return: A C{Deferred} which will fire when the request has completed. The deferred may errback for a variety of reasons, for example an invalid query, the mention of a non-existent tag or a tag that the caller does not have DELETE permission for. """ usage = registry.findUsage(httpValueCategoryName, 'DELETE', ValuesResource) registry.checkRequest(usage, request) query = request.args[queryArg][0] tags = request.args[tagArg] if tags == ['*']: tags = None yield self.facadeClient.deleteValuesForQuery(self.session, query, tags) request.setResponseCode(usage.successCode) defer.returnValue(None) # ------------------------------ Values GET -----------------------------
def deferred_render_GET(self, request): usage = registry.findUsage(httpObjectCategoryName, 'GET', ObjectsResource) registry.checkRequest(usage, request) responseType = usage.getResponsePayloadTypeFromAcceptHeader(request) query = request.args['query'][0] results = yield self.facadeClient.resolveQuery(self.session, query) responseDict = {'ids': list(results)} registry.checkResponse(responseType, responseDict, usage, request) body = payloads.buildPayload(responseType, responseDict) request.setHeader('Content-length', str(len(body))) request.setHeader('Content-type', responseType) request.setResponseCode(usage.successCode) defer.returnValue(body) # ------------------------------ Objects POST -----------------------------
def deferred_render_DELETE(self, request): """ Delete a tag from an object. Return a Deferred that fires with None once the facade has done the deletion. The following code, apart from the yield self._setObjectId(), is taken verbatim from deferred_render_DELETE in objects.py. So if you change this code, you'll likely need to change that, and vice versa. @param request: The HTTP request. @return: A C{Deferred} that fires with C{None} once the request has completed. """ usage = registry.findUsage(httpAboutCategoryName, 'DELETE', AboutTagInstanceResource) registry.checkRequest(usage, request) yield self._setObjectId() yield self.facadeClient.deleteTagInstance( self.session, self.path, self.objectId) request.setResponseCode(usage.successCode) defer.returnValue(None)
def requestAvatarId(self, credentials): """ Return the avatar id of the avatar which can be accessed using the given credentials. credentials will be an object with username and password tags. We need to raise an error to indicate failure or return a username to indicate success. requestAvatar will then be called with the avatar id we returned. """ try: session = yield self.facadeClient.authenticateUserWithPassword( credentials.username, credentials.password) except (TPasswordIncorrect, TNoSuchUser): unauthorizedLogin = error.UnauthorizedLogin('Invalid credentials') log.msg('Bad credentials: %r:%r' % (credentials.username, '<sanitized>')) raise unauthorizedLogin except Exception, e: log.msg('requestAvatarId exception authenticating %r/%r.' % (credentials.username, '<sanitized>')) log.err(e) raise else: defer.returnValue(session)
def requestAvatarId(self, credentials): """ Return the avatar id of the avatar which can be accessed using the given OAuth credentials. @param credentials: A L{IOAuthCredentials} that contains OAuth credentials. @raise UnauthorizedLogin: if the OAuth credentials don't match the L{User}'s. """ try: session = yield self.facadeClient.authenticateUserWithOAuth( credentials) except TPasswordIncorrect: logging.info('Bad OAuth credentials: %r:%r' % (credentials.consumerKey, '<sanitized>')) raise error.UnauthorizedLogin('Invalid credentials') except Exception: logging.info('requestAvatarId exception authenticating %r/%r.' % (credentials.consumerKey, '<sanitized>')) raise else: defer.returnValue(session)
def requestAvatarId(self, credentials): """ Return the avatar ID of the avatar which can be accessed using the given OAuth credentials. @param credentials: A L{IOAuth2Credentials} that contains OAuth credentials. @raise UnauthorizedLogin: Raised if the OAuth credentials don't match the L{User}'s. """ try: session = yield self.facadeClient.authenticateUserWithOAuth2( credentials) except TPasswordIncorrect: logging.info('Bad OAuth credentials: %r:%r' % (credentials.consumerKey, '<sanitized>')) raise error.UnauthorizedLogin('Invalid credentials') except Exception: logging.info('requestAvatarId exception authenticating %r/%r.' % (credentials.consumerKey, '<sanitized>')) raise else: defer.returnValue(session)
def getPermissions(self, path, action, requesterUsername=None, requesterPassword=None): headers = { 'accept': 'application/json', } self.addBasicAuthHeader(headers, requesterUsername, requesterPassword) d = http.getPage( '%s/%s/%s?action=%s' % (self.endpoint, defaults.httpPermissionCategoryName, urllib.quote(path.encode('utf-8')), urllib.quote_plus(action)), headers=headers, method='GET') d.addCallback(self.checkStatus, txHttp.OK) d.addCallback(self.checkPayloadHas, dict.fromkeys(['policy', 'exceptions'])) result = yield d payload = result[2] dictionary = json.loads(payload) defer.returnValue((dictionary['policy'], dictionary['exceptions']))
def getPolicy(self, username, category, action, requesterUsername=None, requesterPassword=None): headers = { 'accept': 'application/json', } self.addBasicAuthHeader(headers, requesterUsername, requesterPassword) path = '%s/%s/%s/%s/%s' % ( self.endpoint, defaults.httpPolicyCategoryName, urllib.quote(username.encode('utf-8')), urllib.quote(category.encode('utf-8')), urllib.quote(action.encode('utf-8'))) d = http.getPage(path, headers=headers, method='GET') d.addCallback(self.checkStatus, txHttp.OK) d.addCallback(self.checkPayloadHas, dict.fromkeys(['policy', 'exceptions'])) result = yield d payload = result[2] dictionary = json.loads(payload) defer.returnValue((dictionary['policy'], dictionary['exceptions']))
def query(self, query, requesterUsername=None, requesterPassword=None): headers = { 'accept': 'application/json', } self.addBasicAuthHeader(headers, requesterUsername, requesterPassword) d = http.getPage( '%s/%s?query=%s' % (self.endpoint, defaults.httpObjectCategoryName, urllib.quote(query.encode('utf-8'))), headers=headers, method='GET') d.addCallback(self.checkStatus, txHttp.OK) d.addCallback(self.checkPayloadHas, dict.fromkeys(['ids'])) result = yield d payload = result[2] dictionary = json.loads(payload) defer.returnValue(dictionary['ids'])