github.com/weaviate/weaviate@v1.24.6/test/helper/client.go (about) 1 // _ _ 2 // __ _____ __ ___ ___ __ _| |_ ___ 3 // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \ 4 // \ V V / __/ (_| |\ V /| | (_| | || __/ 5 // \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___| 6 // 7 // Copyright © 2016 - 2024 Weaviate B.V. All rights reserved. 8 // 9 // CONTACT: hello@weaviate.io 10 // 11 12 package helper 13 14 // This file contains the Client(t *testing.T) function, that can be used to construct a client that talks to 15 // the Weaviate server that is configured using command line arguments (see init.go). 16 // 17 // We pass in the test (*testing.T), to be able to log HTTP traffic to that specific test case. 18 // This allows us to get detailed logs of the performed HTTP requests if a acceptance test fails. 19 20 // The CreateAuth returns a function that attaches the key and token headers to each HTTP call. 21 22 // Example: 23 // func TestSomething(t *testing.T) { 24 // // Use specific key & token 25 // auth := helper.CreateAuth(key, token) 26 // helper.Client(t).SomeScope.SomeOperation(&someParams, auth) 27 // 28 // // Use root key & token 29 // helper.Client(t).SomeScope.SomeOperation(&someParams, helper.RootAuth) 30 // } 31 32 import ( 33 "fmt" 34 "testing" 35 36 "github.com/go-openapi/runtime" 37 httptransport "github.com/go-openapi/runtime/client" 38 "github.com/go-openapi/strfmt" 39 apiclient "github.com/weaviate/weaviate/client" 40 ) 41 42 // Create a client that logs with t.Logf, if a *testing.T is provided. 43 // If there is no test case at hand, pass in nil to disable logging. 44 func Client(t *testing.T) *apiclient.Weaviate { 45 transport := httptransport.New(fmt.Sprintf("%s:%s", ServerHost, ServerPort), "/v1", []string{ServerScheme}) 46 47 // If a test case is provided, and we want to dump HTTP traffic, 48 // create a simple logger that logs HTTP traffic to the test case. 49 if t != nil && DebugHTTP { 50 transport.SetDebug(true) 51 transport.SetLogger(&testLogger{t: t}) 52 } 53 54 client := apiclient.New(transport, strfmt.Default) 55 return client 56 } 57 58 // Create a Weaviate client for the given API key & token. 59 func CreateAuth(apiKey strfmt.UUID, apiToken string) runtime.ClientAuthInfoWriterFunc { 60 // Create an auth writer that both sets the api key & token. 61 authWriter := func(r runtime.ClientRequest, _ strfmt.Registry) error { 62 err := r.SetHeaderParam("X-API-KEY", string(apiKey)) 63 if err != nil { 64 return err 65 } 66 67 return r.SetHeaderParam("X-API-TOKEN", apiToken) 68 } 69 70 return authWriter 71 }