def test_request_transport_integration(): handler = ReadOnlySchedulerHandler() processor = ReadOnlyScheduler.Processor(handler) pfactory = TJSONProtocol.TJSONProtocolFactory() server = THttpServer.THttpServer(processor, ('localhost', 0), pfactory) server_thread = Thread(target=server.serve) server_thread.start() _, server_port = server.httpd.socket.getsockname() response = None try: transport = TRequestsTransport('http://localhost:%d' % server_port) protocol = TJSONProtocol.TJSONProtocol(transport) client = ReadOnlyScheduler.Client(protocol) response = client.getRoleSummary() finally: server.httpd.shutdown() assert response is not None assert response.responseCode == ResponseCode.OK assert response.serverInfo.clusterName == 'west' assert response.serverInfo.thriftAPIVersion == 3 transport.close()
def __init__(self, host, port, static_path, fetch_handler, search_handler, search_proxy_handler): self.host = host self.port = port SearchServer.FETCH_HANDLER = fetch_handler SearchServer.STATIC_PATH = static_path fetch_processor = FetchCommunicationService.Processor(fetch_handler) fetch_pfactory = TJSONProtocol.TJSONProtocolFactory() SearchServer.FETCH_TSERVER = TServer.TServer(fetch_processor, None, None, None, fetch_pfactory, fetch_pfactory) search_processor = SearchService.Processor(search_handler) search_pfactory = TJSONProtocol.TJSONProtocolFactory() SearchServer.SEARCH_TSERVER = TServer.TServer(search_processor, None, None, None, search_pfactory, search_pfactory) search_proxy_processor = SearchProxyService.Processor( search_proxy_handler) search_proxy_pfactory = TJSONProtocol.TJSONProtocolFactory() SearchServer.SEARCH_PROXY_TSERVER = TServer.TServer( search_proxy_processor, None, None, None, search_proxy_pfactory, search_proxy_pfactory)
def service_core_json(request): # Catch CORS preflight requests if request.method == 'OPTIONS': resp = HttpResponse('') resp['Content-Type'] = 'text/plain charset=UTF-8' resp['Content-Length'] = 0 resp.status_code = 204 else: try: iprot = TJSONProtocol.TJSONProtocol(TMemoryBuffer(request.body)) oprot = TJSONProtocol.TJSONProtocol(TMemoryBuffer()) core_processor.process(iprot, oprot) resp = HttpResponse(oprot.trans.getvalue(), content_type="application/json") except Exception as e: resp = HttpResponse(json.dumps({'error': str(e)}), content_type="application/json") try: resp['Access-Control-Allow-Origin'] = request.META['HTTP_ORIGIN'] except: pass resp['Access-Control-Allow-Methods'] = "POST, PUT, GET, DELETE, OPTIONS" resp['Access-Control-Allow-Credentials'] = "true" resp['Access-Control-Allow-Headers'] = ("Authorization, Cache-Control, " "If-Modified-Since, Content-Type") return resp
def __init__(self, proto, host, port, product, uri, auto_handle_connection=True, session_token=None): # Import only if necessary; some tests may not add this to PYTHONPATH. from libcodechecker import session_manager from ProductManagement_v6 import codeCheckerProductService full_uri = '/v' + VERSION + uri if product: full_uri = '/' + product + full_uri url = util.create_product_url(proto, host, port, full_uri) transport = THttpClient.THttpClient(url) protocol = TJSONProtocol.TJSONProtocol(transport) client = codeCheckerProductService.Client(protocol) if session_token: headers = { 'Cookie': session_manager.SESSION_COOKIE_NAME + "=" + session_token } transport.setCustomHeaders(headers) super(CCProductHelper, self).__init__(transport, client, auto_handle_connection)
def JSONToThrift(base, buf, protocol_factory=TJSONProtocol.TJSONProtocolFactory()): transport = TTransport.TMemoryBuffer(buf) protocol = protocol_factory.getProtocol(transport) base.read(protocol) return base
def thrift_to_json(tobj, remove_timestamps=False, remove_uuids=False): """Get a "pretty-printed" JSON string representation for a Thrift object Args: - `tobj`: A Thrift object - `remove_uuids`: Boolean flag indicating if Concrete UUIDs should be removed Returns: - A string containing a "pretty-printed" JSON representation of the Thrift object """ thrift_json_string = TSerialization.serialize( tobj, TJSONProtocol.TSimpleJSONProtocolFactory()) thrift_json = json.loads(thrift_json_string) if remove_timestamps: thrift_json = get_json_object_without_timestamps(thrift_json) if remove_uuids: thrift_json = get_json_object_without_uuids(thrift_json) return json.dumps(thrift_json, indent=2, separators=(',', ': '), ensure_ascii=False, sort_keys=True)
def __init__(self, protocol, host, port, product, endpoint, auto_handle_connection=True, session_token=None): # Import only if necessary; some tests may not add this to PYTHONPATH. from codeCheckerDBAccess_v6 import codeCheckerDBAccess from codeCheckerDBAccess_v6.constants import MAX_QUERY_SIZE from libcodechecker.libclient.credential_manager \ import SESSION_COOKIE_NAME self.max_query_size = MAX_QUERY_SIZE url = util.create_product_url( protocol, host, port, '/' + product + '/v' + VERSION + endpoint) print("Setup viewer client: " + url) transport = THttpClient.THttpClient(url) protocol = TJSONProtocol.TJSONProtocol(transport) client = codeCheckerDBAccess.Client(protocol) if session_token: headers = {'Cookie': SESSION_COOKIE_NAME + '=' + session_token} transport.setCustomHeaders(headers) super(CCViewerHelper, self).__init__(transport, client, auto_handle_connection)
def to_str(msg): if hasattr(msg, 'thrift_spec'): global g_WriteTMemoryBuffer, g_WriteTBinaryProtocol g_WriteTMemoryBuffer.cstringio_buf.truncate() g_WriteTMemoryBuffer.cstringio_buf.seek(0) if g_protocol == 1: proto = TJSONProtocol.TJSONProtocol(g_WriteTMemoryBuffer) proto.writeMessageBegin('ff', 0, 0) msg.write(proto) proto.writeMessageEnd() else: msg.write(g_WriteTBinaryProtocol) return g_WriteTMemoryBuffer.getvalue() #mb = TTransport.TMemoryBuffer() #bp = TBinaryProtocol.TBinaryProtocol(mb) #bp = TCompactProtocol.TCompactProtocol(mb) #msg.write(bp) #return mb.getvalue() elif hasattr(msg, 'SerializeToString'): return msg.SerializeToString() elif isinstance(msg, unicode): return msg.encode('utf-8') elif isinstance(msg, str): return msg elif msg.__class__ == int or msg.__class__ == long or msg.__class__ == float: return str(msg) else: return json.dumps(msg, ensure_ascii=False)
def __init__(self, protocol, host, port, uri, session_token=None, get_new_token=None): """ @param get_new_token: a function which can generate a new token. """ self.__host = host self.__port = port url = create_product_url(protocol, host, port, uri) self.transport = None try: self.transport = THttpClient.THttpClient(url) except ValueError: # Initalizing THttpClient may raise an exception if proxy settings # are used but the port number is not a valid integer. pass # Thrift do not handle the use case when invalid proxy format is # used (e.g.: no schema is specified). For this reason we need to # verify the proxy format in our side. self._validate_proxy_format() self.protocol = TJSONProtocol.TJSONProtocol(self.transport) self.client = None self.get_new_token = get_new_token self._set_token(session_token)
def thrift_to_value(val_): if g_protocol == 1: dest = protocol_type_() tmp_ReadTMemoryBuffer = TTransport.TMemoryBuffer() tmp_ReadTJsonProtocol = TJSONProtocol.TJSONProtocol( tmp_ReadTMemoryBuffer) tmp_ReadTMemoryBuffer.cstringio_buf.truncate() tmp_ReadTMemoryBuffer.cstringio_buf.seek(0) tmp_ReadTMemoryBuffer.cstringio_buf.write(val_) tmp_ReadTMemoryBuffer.cstringio_buf.seek(0) tmp_ReadTJsonProtocol.readMessageBegin() dest.read(tmp_ReadTJsonProtocol) tmp_ReadTJsonProtocol.readMessageEnd() #mb2 = TTransport.TMemoryBuffer(val_) #bp2 = TBinaryProtocol.TBinaryProtocol(mb2) #dest.read(bp2); return dest else: dest = protocol_type_() global g_ReadTMemoryBuffer, g_ReadTBinaryProtocol g_ReadTMemoryBuffer.cstringio_buf.truncate() g_ReadTMemoryBuffer.cstringio_buf.seek(0) g_ReadTMemoryBuffer.cstringio_buf.write(val_) g_ReadTMemoryBuffer.cstringio_buf.seek(0) dest.read(g_ReadTBinaryProtocol) #mb2 = TTransport.TMemoryBuffer(val_) #bp2 = TBinaryProtocol.TBinaryProtocol(mb2) #dest.read(bp2); return dest
def to_str(msg): if hasattr(msg, 'thrift_spec'): global g_WriteTMemoryBuffer, g_WriteTBinaryProtocol g_WriteTMemoryBuffer.cstringio_buf.truncate() g_WriteTMemoryBuffer.cstringio_buf.seek(0) ret = '' if g_protocol == 1: tmp_WriteTMemoryBuffer = TTransport.TMemoryBuffer() proto = TJSONProtocol.TJSONProtocol(tmp_WriteTMemoryBuffer) proto.writeMessageBegin(msg.__class__.__name__, 0, 0) msg.write(proto) proto.writeMessageEnd() ret = tmp_WriteTMemoryBuffer.getvalue() else: msg.write(g_WriteTBinaryProtocol) ret = g_WriteTMemoryBuffer.getvalue() #print('tostr',len(ret), ret) return ret elif hasattr(msg, 'SerializeToString'): return msg.SerializeToString() elif isinstance(msg, unicode): return msg.encode('utf-8') elif isinstance(msg, str): return msg elif msg.__class__ == int or msg.__class__ == long or msg.__class__ == float: return str(msg) else: return json.dumps(msg, ensure_ascii=False)
def _get_client_by_transport(self, options, transport, socket=None): # Create the protocol and client if options.json: protocol = TJSONProtocol.TJSONProtocol(transport) elif options.compact: protocol = TCompactProtocol.TCompactProtocol(transport) # No explicit option about protocol is specified. Try to infer. elif options.framed or options.unframed: protocol = TBinaryProtocol.TBinaryProtocolAccelerated(transport) elif socket is not None: # If json, compact, framed, and unframed are not specified, # THeaderProtocol is the default. Create a protocol using either # fuzzy or non-fuzzy transport depending on if options.fuzz is set. if options.fuzz is not None: transport = TFuzzyHeaderTransport(socket, fuzz_fields=options.fuzz, verbose=True) else: transport = THeaderTransport(socket) protocol = THeaderProtocol.THeaderProtocol(transport) else: self._exit(error_message=('No valid protocol ' 'specified for %s' % (type(self))), status=os.EX_USAGE) transport.open() self._transport = transport client = self.service_class.Client(protocol) return client
def main(): parser = argparse.ArgumentParser( description= "Encode a Communication archive as a CSV file, where each row contains a " "TJSONProtocol encoded Communication", formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument( 'comms_archive', help="A directory, TGZ file or Zip file of Communications") parser.add_argument( 'csv_file', help="Output CSV file with TJSONProtocol encoded Communications") parser.add_argument('--column-name', default='comm', help="Name to use for CSV column header") args = parser.parse_args() csv_fh = open(args.csv_file, 'wb') fieldnames = [args.column_name] writer = unicodecsv.DictWriter(csv_fh, fieldnames, lineterminator='\n', quoting=unicodecsv.QUOTE_ALL) writer.writeheader() for (comm, filename) in CommunicationReader(args.comms_archive): json_communication = TSerialization.serialize( comm, TJSONProtocol.TJSONProtocolFactory()).decode('utf-8') writer.writerow({args.column_name: json_communication})
def thrift_to_json(tobj, remove_timestamps=False, remove_uuids=False): """Get a "pretty-printed" JSON string representation for a Thrift object Args: tobj: A Thrift object remove_timestamps (bool): Flag for removing timestamps from JSON output remove_uuids (bool): Flag for removing :class:`.UUID` info from JSON output Returns: str: A "pretty-printed" JSON representation of the Thrift object """ thrift_json_string = TSerialization.serialize( tobj, TJSONProtocol.TSimpleJSONProtocolFactory()).decode('utf-8') thrift_json = json.loads(thrift_json_string) if remove_timestamps: thrift_json = get_json_object_without_timestamps(thrift_json) if remove_uuids: thrift_json = get_json_object_without_uuids(thrift_json) return json.dumps(thrift_json, indent=2, separators=(',', ': '), ensure_ascii=False, sort_keys=True)
def __init__(self, user=None, password=None, host=None, port=9091, dbname=None, protocol="http"): if protocol == "http": if not host.startswith(protocol): # the THttpClient expects http[s]://localhost host = protocol + '://' + host transport = THttpClient.THttpClient(host) proto = TJSONProtocol.TJSONProtocol(transport) socket = None elif protocol == "binary": socket = TSocket.TSocket(host, port) transport = TTransport.TBufferedTransport(socket) proto = TBinaryProtocol.TBinaryProtocol(transport) else: raise TypeError("`protocol` should be one of ['http', 'binary'], ", "got {} instead".format(protocol)) self._user = user self._password = password self._host = host self._port = port self._dbname = dbname self._transport = transport self._protocol = protocol self._socket = socket self._transport.open() self._client = MapD.Client(proto) self._session = self._client.connect(user, password, dbname)
def deserialize_TJSONProtocol(serialize_val): tmembuf = TTransport.TMemoryBuffer(serialize_val) tbinprot = TJSONProtocol.TJSONProtocol(tmembuf) request = GetCityWeatherRequest() request.read(tbinprot) print request, request.city, request.user_id
def render_quota(self, write_json, quota_resp): def get_quota_json(quota): result = {} result['cpu'] = quota.numCpus result['ram'] = float(quota.ramMb) / 1024 result['disk'] = float(quota.diskMb) / 1024 return result def get_quota_str(quota): result = [] result.append(' CPU: %s' % quota.numCpus) result.append(' RAM: %f GB' % (float(quota.ramMb) / 1024)) result.append(' Disk: %f GB' % (float(quota.diskMb) / 1024)) return result if write_json: return serialize( quota_resp.result.getQuotaResult, protocol_factory=TJSONProtocol.TSimpleJSONProtocolFactory()) else: quota_result = quota_resp.result.getQuotaResult result = ['Allocated:'] result += get_quota_str(quota_result.quota) if quota_result.prodConsumption: result.append('Production resources consumed:') result += get_quota_str(quota_result.prodConsumption) if quota_result.nonProdConsumption: result.append('Non-production resources consumed:') result += get_quota_str(quota_result.nonProdConsumption) return '\n'.join(result)
def __init__(self, uri=None, # type: Optional[str] user=None, # type: Optional[str] password=None, # type: Optional[str] host=None, # type: Optional[str] port=9091, # type: Optional[int] dbname=None, # type: Optional[str] protocol='binary', # type: Optional[str] ): # type: (...) -> None if uri is not None: if not all([user is None, password is None, host is None, port == 9091, dbname is None, protocol == 'binary']): raise TypeError("Cannot specify both URI and other arguments") user, password, host, port, dbname, protocol = _parse_uri(uri) if host is None: raise TypeError("`host` parameter is required.") if protocol in ("http", "https"): if not host.startswith(protocol): # the THttpClient expects http[s]://localhost host = protocol + '://' + host transport = THttpClient.THttpClient("{}:{}".format(host, port)) proto = TJSONProtocol.TJSONProtocol(transport) socket = None elif protocol == "binary": socket = TSocket.TSocket(host, port) transport = TTransport.TBufferedTransport(socket) proto = TBinaryProtocol.TBinaryProtocolAccelerated(transport) else: raise ValueError("`protocol` should be one of", " ['http', 'https', 'binary'],", " got {} instead".format(protocol)) self._user = user self._password = password self._host = host self._port = port self._dbname = dbname self._transport = transport self._protocol = protocol self._socket = socket self._closed = 0 self._tdf = None try: self._transport.open() except TTransportException as e: if e.NOT_OPEN: err = OperationalError("Could not connect to database") six.raise_from(err, e) else: raise self._client = Client(proto) try: self._session = self._client.connect(user, password, dbname) except TMapDException as e: six.raise_from(_translate_exception(e), e)
def __init__(self, host, port, static_path, fetch_handler, store_handler): self.host = host self.port = port AccessHTTPServer.STATIC_PATH = static_path AccessHTTPServer.FETCH_HANDLER = fetch_handler fetch_processor = FetchCommunicationService.Processor(fetch_handler) fetch_pfactory = TJSONProtocol.TJSONProtocolFactory() AccessHTTPServer.FETCH_TSERVER = TServer.TServer( fetch_processor, None, None, None, fetch_pfactory, fetch_pfactory) AccessHTTPServer.STORE_HANDLER = store_handler store_processor = StoreCommunicationService.Processor(store_handler) store_pfactory = TJSONProtocol.TJSONProtocolFactory() AccessHTTPServer.STORE_TSERVER = TServer.TServer( store_processor, None, None, None, store_pfactory, store_pfactory)
def service_core_json(request): try: iprot = TJSONProtocol.TJSONProtocol(TMemoryBuffer(request.body)) oprot = TJSONProtocol.TJSONProtocol(TMemoryBuffer()) core_processor.process(iprot, oprot) resp = HttpResponse(oprot.trans.getvalue(), content_type="application/json") try: resp['Access-Control-Allow-Origin'] = request.META['HTTP_ORIGIN'] except: pass return resp except Exception, e: return HttpResponse(json.dumps({'error': str(e)}), content_type="application/json")
def do_test(): import TWebsocketClient transport = TWebsocketClient.TWebsocketClient("ws://localhost:8888/thrift") protocol = TJSONProtocol(transport) client = Hello.Client(protocol) transport.open() print client.sayHello() transport.close()
def __ThriftToJson(thriftdata): thrift_json_string = TSerialization.serialize( thriftdata, TJSONProtocol.TSimpleJSONProtocolFactory()).decode('utf-8') json_value = json.loads(thrift_json_string) json_value['password'] = thriftdata.password.decode() json_value['code'] = thriftdata.code.decode() return json_value
def taxon_service(): handler = TaxonService(services=get_services_dict()) processor = thrift_service.Processor(handler) pfactory = TJSONProtocol.TJSONProtocolFactory() server_address = ('127.0.0.1', 9090) server = THttpServer.THttpServer(processor, server_address, pfactory) return server
def __enter__(self): ''' Create and open connection ''' self.transport = THttpClient.THttpClient(uri_or_host=self.uri) protocol = TJSONProtocol.TJSONProtocol(self.transport) cli = self.concrete_service_class.Client(protocol) return cli
class JSONProtocolTest(AbstractTest, unittest.TestCase): protocol_factory = TJSONProtocol.TJSONProtocolFactory() def testNestedStructs(self): self._deserialize(NestedStructs, '{"1":{"rec":{}}"2":{"rec":{}}}') self._deserialize(NestedStructs, '{"1":{"rec":{}},"2":{"rec":{}}}') nested = NestedStructs(bonk=Bonk(), bools=Bools()) protocol_factory = TJSONProtocol.TJSONProtocolFactory(validJSON=True) json.loads(Serializer.serialize(protocol_factory, nested))
def do_test(): transport = THttpClient.THttpClient('http://localhost:9090') transport = TTransport.TBufferedTransport(transport) protocol = TJSONProtocol.TJSONProtocol(transport) client = Hello.Client(protocol) transport.open() print "server response:", client.sayHello() transport.close()
def run(): # Setup Connection transport = THttpClient("http://localhost:8080/thrift/hawk/json") protocol = TJSONProtocol.TJSONProtocol(transport) client = Hawk.Client(protocol) # Make API Call result = client.listBackends() for backend in result: print(backend)
def create_client(service, service_name): '''This function initializes the Thrift client and returns the client objects to the API.''' path = '/' + workspace + '/' + service_name transport = THttpClient.THttpClient(host, port, path) protocol = TJSONProtocol.TJSONProtocol(transport) return service.Client(protocol)
def serialize_TJSONProtocol(): request = GetCityWeatherRequest() request.city = "wuhan" #request.user_id="001" tmembuf = TTransport.TMemoryBuffer() tbinprot = TJSONProtocol.TJSONProtocol(tmembuf) request.write(tbinprot) print tmembuf.getvalue() return tmembuf.getvalue()
def web_sync_service(): processor = Web_Sync.Processor(handler) protocol = TJSONProtocol.TJSONProtocolFactory() server = TServer.TServer(processor, None, None, None, protocol, protocol) itrans = TTransport.TMemoryBuffer(request.data) otrans = TTransport.TMemoryBuffer() iprot = server.inputProtocolFactory.getProtocol(itrans) oprot = server.outputProtocolFactory.getProtocol(otrans) server.processor.process(iprot, oprot) return make_response(otrans.getvalue())