goccy / bigquery-emulator

BigQuery emulator server implemented in Go
MIT License
846 stars 108 forks source link

C# client #240

Closed ambujs-ps closed 1 year ago

ambujs-ps commented 1 year ago

I'm trying to follow the examples of calling the apiEndpoint in the Docker image. But unfortunately, the official C# client doesn't support providing of an options or apiEndpoint property.

Just wondering if there's a suggested way of achieving this?

Thanks in advance

richardware commented 1 year ago

Hi @ambujs-ps, this has worked for me (using Test Containers):

// Build the container
var container = new ContainerBuilder()
   .WithImage("ghcr.io/goccy/bigquery-emulator:latest")
   .WithPortBinding(9050, true)
   .WithResourceMapping(new FileInfo("data.yaml"), "/srv/testdata/")
   .WithCommand("--project=test", "--data-from-yaml=/srv/testdata/data.yaml")
   .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(9050))
   .Build();

// Start the container.
await container
   .StartAsync()
   .ConfigureAwait(false);

// Build the BigQuery Client
var bigQueryEndpoint = new UriBuilder(Uri.UriSchemeHttp, container.Hostname, container.GetMappedPublicPort(9050)).Uri;
var client = await new BigQueryClientBuilder {
   ProjectId = /* your project Id **/,
   ApiKey = "fake",
   BaseUri = bigQueryEndpoint.ToString()
}.BuildAsync();
ambujs-ps commented 1 year ago
BigQueryClientBuilder

Thanks so much!, I'll give this a try.

richardware commented 1 year ago

FYI a key part is providing a fake ApiKey to Google's BigQueryClientBuilder. Even if the BaseUri is pointing to a containerized emulator instance, you may still have problems - if you provide a null credential then the Client will look for the Application Default Credentials file, if you provide even a fake User credential from JSON or a file then the Client will attempt to refresh the auth token.