Beispiel #1
0
    def send_confirmation_to_main_server(key, filename):
        logger.warning(f"Notifying main server about new upload at {main_server_address}")

        if main_server_use_ssl:
            channel = grpc.secure_channel(main_server_address, grpc.ssl_channel_credentials())
        else:
            logger.warning("Connecting to main server insecurely!")
            channel = grpc.insecure_channel(main_server_address)

        media_stub = media_pb2_grpc.MediaStub(channel)
        req = media_pb2.UploadConfirmationReq(
            key=key,
            filename=filename,
        )
        media_stub.UploadConfirmation(req, metadata=(("authorization", f"Bearer {media_server_bearer_token}"),))
Beispiel #2
0
def media_session(bearer_token):
    """
    Create a fresh Media API for testing, uses the bearer token for media auth
    """
    media_auth_interceptor = get_media_auth_interceptor(bearer_token)

    with futures.ThreadPoolExecutor(1) as executor:
        server = grpc.server(executor, interceptors=[media_auth_interceptor])
        port = server.add_secure_port("localhost:0", grpc.local_server_credentials())
        servicer = Media()
        media_pb2_grpc.add_MediaServicer_to_server(servicer, server)
        server.start()

        call_creds = grpc.access_token_call_credentials(bearer_token)
        comp_creds = grpc.composite_channel_credentials(grpc.local_channel_credentials(), call_creds)

        try:
            with grpc.secure_channel(f"localhost:{port}", comp_creds) as channel:
                yield media_pb2_grpc.MediaStub(channel)
        finally:
            server.stop(None).wait()