Skip to content

Commit

Permalink
feat(go-sdk): allow specifying port and url
Browse files Browse the repository at this point in the history
  • Loading branch information
rhamzeh authored and booniepepper committed Dec 6, 2023
1 parent 730bd07 commit 1ff7a8c
Show file tree
Hide file tree
Showing 14 changed files with 275 additions and 198 deletions.
6 changes: 3 additions & 3 deletions config/clients/dotnet/template/README_initializing.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Example {
try {
var configuration = new ClientConfiguration() {
ApiScheme = Environment.GetEnvironmentVariable("FGA_API_SCHEME"), // optional, defaults to "https"
ApiHost = Environment.GetEnvironmentVariable("FGA_API_HOST"), // required, define without the scheme (e.g. api.fga.example instead of https://api.fga.example)
ApiHost = Environment.GetEnvironmentVariable("FGA_API_HOST"), // required, define without the scheme (e.g. api.{{sampleApiDomain}} instead of https://api.{{sampleApiDomain}})
StoreId = Environment.GetEnvironmentVariable("FGA_STORE_ID"), // not needed when calling `CreateStore` or `ListStores`
AuthorizationModelId = Environment.GetEnvironmentVariable("FGA_AUTHORIZATION_MODEL_ID"), // Optional, can be overridden per request
};
Expand All @@ -42,7 +42,7 @@ namespace Example {
try {
var configuration = new ClientConfiguration() {
ApiScheme = Environment.GetEnvironmentVariable("FGA_API_SCHEME"), // optional, defaults to "https"
ApiHost = Environment.GetEnvironmentVariable("FGA_API_HOST"), // required, define without the scheme (e.g. api.fga.example instead of https://api.fga.example)
ApiHost = Environment.GetEnvironmentVariable("FGA_API_HOST"), // required, define without the scheme (e.g. api.{{sampleApiDomain}} instead of https://api.{{sampleApiDomain}})
StoreId = Environment.GetEnvironmentVariable("FGA_STORE_ID"), // not needed when calling `CreateStore` or `ListStores`
AuthorizationModelId = Environment.GetEnvironmentVariable("FGA_AUTHORIZATION_MODEL_ID"), // Optional, can be overridden per request
Credentials = new Credentials() {
Expand Down Expand Up @@ -75,7 +75,7 @@ namespace Example {
try {
var configuration = new ClientConfiguration() {
ApiScheme = Environment.GetEnvironmentVariable("FGA_API_SCHEME"), // optional, defaults to "https"
ApiHost = Environment.GetEnvironmentVariable("FGA_API_HOST"), // required, define without the scheme (e.g. api.fga.example instead of https://api.fga.example)
ApiHost = Environment.GetEnvironmentVariable("FGA_API_HOST"), // required, define without the scheme (e.g. api.{{sampleApiDomain}} instead of https://api.{{sampleApiDomain}})
StoreId = Environment.GetEnvironmentVariable("FGA_STORE_ID"), // not needed when calling `CreateStore` or `ListStores`
AuthorizationModelId = Environment.GetEnvironmentVariable("FGA_AUTHORIZATION_MODEL_ID"), // Optional, can be overridden per request
Credentials = new Credentials() {
Expand Down
17 changes: 7 additions & 10 deletions config/clients/go/template/README_initializing.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ import (

func main() {
fgaClient, err := NewSdkClient(&ClientConfiguration{
ApiScheme: os.Getenv("FGA_API_SCHEME"), // optional, defaults to "https"
ApiHost: os.Getenv("FGA_API_HOST"), // required, define without the scheme (e.g. api.fga.example instead of https://api.fga.example)
StoreId: os.Getenv("FGA_STORE_ID"), // not needed when calling `CreateStore` or `ListStores`
ApiUrl: os.Getenv("FGA_API_URL"), // required, e.g. https://api.{{sampleApiDomain}}
StoreId: os.Getenv("FGA_STORE_ID"), // not needed when calling `CreateStore` or `ListStores`
})

if err != nil {
Expand All @@ -34,9 +33,8 @@ import (

func main() {
fgaClient, err := NewSdkClient(&ClientConfiguration{
ApiScheme: os.Getenv("FGA_API_SCHEME"), // optional, defaults to "https"
ApiHost: os.Getenv("FGA_API_HOST"), // required, define without the scheme (e.g. api.fga.example instead of https://api.fga.example)
StoreId: os.Getenv("FGA_STORE_ID"), // not needed when calling `CreateStore` or `ListStores`
ApiUrl: os.Getenv("FGA_API_URL"), // required, e.g. https://api.{{sampleApiDomain}}
StoreId: os.Getenv("FGA_STORE_ID"), // not needed when calling `CreateStore` or `ListStores`
Credentials: &credentials.Credentials{
Method: credentials.CredentialsMethodApiToken,
Config: &credentials.Config{
Expand All @@ -63,10 +61,9 @@ import (

func main() {
fgaClient, err := NewSdkClient(&ClientConfiguration{
ApiScheme: os.Getenv("FGA_API_SCHEME"), // optional, defaults to "https"
ApiHost: os.Getenv("FGA_API_HOST"), // required, define without the scheme (e.g. api.fga.example instead of https://api.fga.example)
StoreId: os.Getenv("FGA_STORE_ID"), // not needed when calling `CreateStore` or `ListStores`
AuthorizationModelId: {{packageName}}.PtrString("{{appUpperCaseName}}_AUTHORIZATION_MODEL_ID"),
ApiUrl: os.Getenv("FGA_API_URL"), // required, e.g. https://api.{{sampleApiDomain}}
StoreId: os.Getenv("FGA_STORE_ID"), // not needed when calling `CreateStore` or `ListStores`
AuthorizationModelId: {{packageName}}.PtrString("{{appUpperCaseName}}_AUTHORIZATION_MODEL_ID"),
Credentials: &credentials.Credentials{
Method: credentials.CredentialsMethodClientCredentials,
Config: &credentials.Config{
Expand Down
20 changes: 5 additions & 15 deletions config/clients/go/template/api_client.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -242,37 +242,27 @@ func (c *APIClient) prepareRequest(
}

// Setup path and query parameters
url, err := url.Parse(path)
uri, err := url.Parse(c.cfg.ApiUrl + path)
if err != nil {
return nil, err
}

// Override request host, if applicable
if c.cfg.ApiHost != "" {
url.Host = c.cfg.ApiHost
}

// Override request scheme, if applicable
if c.cfg.ApiScheme != "" {
url.Scheme = c.cfg.ApiScheme
}

// Adding Query Param
query := url.Query()
query := uri.Query()
for k, v := range queryParams {
for _, iv := range v {
query.Add(k, iv)
}
}

// Encode the parameters.
url.RawQuery = query.Encode()
uri.RawQuery = query.Encode()

// Generate a new request
if body != nil {
localVarRequest, err = http.NewRequest(method, url.String(), body)
localVarRequest, err = http.NewRequest(method, uri.String(), body)
} else {
localVarRequest, err = http.NewRequest(method, url.String(), nil)
localVarRequest, err = http.NewRequest(method, uri.String(), nil)
}
if err != nil {
return nil, err
Expand Down
3 changes: 1 addition & 2 deletions config/clients/go/template/api_doc.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ func main() {
{{/allParams}}

configuration, err := {{packageName}}.NewConfiguration({{packageName}}.Configuration{
ApiScheme: os.Getenv("{{appUpperCaseName}}_API_SCHEME"), // optional, defaults to "https"
ApiHost: os.Getenv("{{appUpperCaseName}}_API_HOST"), // required, define without the scheme (e.g. api.{{sampleApiDomain}} instead of https://api.{{sampleApiDomain}})
ApiUrl: os.Getenv("FGA_API_URL"), // required, e.g. https://api.{{sampleApiDomain}}
StoreId: os.Getenv("{{appUpperCaseName}}_STORE_ID"), // not needed when calling `CreateStore` or `ListStores`
})

Expand Down
42 changes: 21 additions & 21 deletions config/clients/go/template/api_test.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func Test{{appShortName}}ApiConfiguration(t *testing.T) {

httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder("GET", fmt.Sprintf("%s://%s/stores/%s/authorization-models", configuration.ApiScheme, configuration.ApiHost, configuration.StoreId),
httpmock.RegisterResponder("GET", fmt.Sprintf("%s/stores/%s/authorization-models", configuration.ApiUrl, configuration.StoreId),
func(req *http.Request) (*http.Response, error) {
resp, err := httpmock.NewJsonResponse(200, ReadAuthorizationModelsResponse{AuthorizationModels: []AuthorizationModel{
{
Expand Down Expand Up @@ -236,7 +236,7 @@ func Test{{appShortName}}ApiConfiguration(t *testing.T) {

httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder("GET", fmt.Sprintf("%s://%s/stores/%s/authorization-models", configuration.ApiScheme, configuration.ApiHost, configuration.StoreId),
httpmock.RegisterResponder("GET", fmt.Sprintf("%s/stores/%s/authorization-models", configuration.ApiUrl, configuration.StoreId),
func(req *http.Request) (*http.Response, error) {
resp, err := httpmock.NewJsonResponse(200, ReadAuthorizationModelsResponse{AuthorizationModels: []AuthorizationModel{
{
Expand Down Expand Up @@ -276,7 +276,7 @@ func Test{{appShortName}}ApiConfiguration(t *testing.T) {
if numCalls != 1 {
t.Fatalf("Expected call to get access token to be made exactly once, saw: %d", numCalls)
}
numCalls = info[fmt.Sprintf("GET %s://%s/stores/%s/authorization-models", configuration.ApiScheme, configuration.ApiHost, configuration.StoreId)]
numCalls = info[fmt.Sprintf("GET %s/stores/%s/authorization-models", configuration.ApiUrl, configuration.StoreId)]
if numCalls != 1 {
t.Fatalf("Expected call to get authorization models to be made exactly once, saw: %d", numCalls)
}
Expand All @@ -300,7 +300,7 @@ func Test{{appShortName}}ApiConfiguration(t *testing.T) {

httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder("GET", fmt.Sprintf("%s://%s/stores/%s/authorization-models", configuration.ApiScheme, configuration.ApiHost, configuration.StoreId),
httpmock.RegisterResponder("GET", fmt.Sprintf("%s/stores/%s/authorization-models", configuration.ApiUrl, configuration.StoreId),
func(req *http.Request) (*http.Response, error) {
resp, err := httpmock.NewJsonResponse(200, ReadAuthorizationModelsResponse{AuthorizationModels: []AuthorizationModel{
{
Expand All @@ -327,7 +327,7 @@ func Test{{appShortName}}ApiConfiguration(t *testing.T) {
if numCalls != 0 {
t.Fatalf("Unexpected call to get access token made. Expected 0, saw: %d", numCalls)
}
numCalls = info[fmt.Sprintf("GET %s://%s/stores/%s/authorization-models", configuration.ApiScheme, configuration.ApiHost, configuration.StoreId)]
numCalls = info[fmt.Sprintf("GET %s/stores/%s/authorization-models", configuration.ApiUrl, configuration.StoreId)]
if numCalls != 1 {
t.Fatalf("Expected call to get authorization models to be made exactly once, saw: %d", numCalls)
}
Expand Down Expand Up @@ -417,7 +417,7 @@ func Test{{appShortName}}Api(t *testing.T) {

httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder(test.Method, fmt.Sprintf("%s://%s/stores/%s/%s", configuration.ApiScheme, configuration.ApiHost, configuration.StoreId, test.RequestPath),
httpmock.RegisterResponder(test.Method, fmt.Sprintf("%s/stores/%s/%s", configuration.ApiUrl, configuration.StoreId, test.RequestPath),
func(req *http.Request) (*http.Response, error) {
resp, err := httpmock.NewJsonResponse(test.ResponseStatus, expectedResponse)
if err != nil {
Expand Down Expand Up @@ -482,7 +482,7 @@ func Test{{appShortName}}Api(t *testing.T) {

httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder(test.Method, fmt.Sprintf("%s://%s/stores/%s/%s", configuration.ApiScheme, configuration.ApiHost, configuration.StoreId, test.RequestPath),
httpmock.RegisterResponder(test.Method, fmt.Sprintf("%s/stores/%s/%s", configuration.ApiUrl, configuration.StoreId, test.RequestPath),
func(req *http.Request) (*http.Response, error) {
resp, err := httpmock.NewJsonResponse(test.ResponseStatus, expectedResponse)
if err != nil {
Expand Down Expand Up @@ -523,7 +523,7 @@ func Test{{appShortName}}Api(t *testing.T) {

httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder(test.Method, fmt.Sprintf("%s://%s/stores/%s/%s/%s", configuration.ApiScheme, configuration.ApiHost, configuration.StoreId, test.RequestPath, modelId),
httpmock.RegisterResponder(test.Method, fmt.Sprintf("%s/stores/%s/%s/%s", configuration.ApiUrl, configuration.StoreId, test.RequestPath, modelId),
func(req *http.Request) (*http.Response, error) {
resp, err := httpmock.NewJsonResponse(test.ResponseStatus, expectedResponse)
if err != nil {
Expand Down Expand Up @@ -575,7 +575,7 @@ func Test{{appShortName}}Api(t *testing.T) {

httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder(test.Method, fmt.Sprintf("%s://%s/stores/%s/%s", configuration.ApiScheme, configuration.ApiHost, configuration.StoreId, test.RequestPath),
httpmock.RegisterResponder(test.Method, fmt.Sprintf("%s/stores/%s/%s", configuration.ApiUrl, configuration.StoreId, test.RequestPath),
func(req *http.Request) (*http.Response, error) {
resp, err := httpmock.NewJsonResponse(test.ResponseStatus, expectedResponse)
if err != nil {
Expand Down Expand Up @@ -631,7 +631,7 @@ func Test{{appShortName}}Api(t *testing.T) {

httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder(test.Method, fmt.Sprintf("%s://%s/stores/%s/%s", configuration.ApiScheme, configuration.ApiHost, configuration.StoreId, test.RequestPath),
httpmock.RegisterResponder(test.Method, fmt.Sprintf("%s/stores/%s/%s", configuration.ApiUrl, configuration.StoreId, test.RequestPath),
func(req *http.Request) (*http.Response, error) {
resp, err := httpmock.NewJsonResponse(test.ResponseStatus, expectedResponse)
if err != nil {
Expand Down Expand Up @@ -679,7 +679,7 @@ func Test{{appShortName}}Api(t *testing.T) {

httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder(test.Method, fmt.Sprintf("%s://%s/stores/%s/%s", configuration.ApiScheme, configuration.ApiHost, configuration.StoreId, test.RequestPath),
httpmock.RegisterResponder(test.Method, fmt.Sprintf("%s/stores/%s/%s", configuration.ApiUrl, configuration.StoreId, test.RequestPath),
func(req *http.Request) (*http.Response, error) {
resp, err := httpmock.NewJsonResponse(test.ResponseStatus, expectedResponse)
if err != nil {
Expand Down Expand Up @@ -722,7 +722,7 @@ func Test{{appShortName}}Api(t *testing.T) {

httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder(test.Method, fmt.Sprintf("%s://%s/stores/%s/%s", configuration.ApiScheme, configuration.ApiHost, configuration.StoreId, test.RequestPath),
httpmock.RegisterResponder(test.Method, fmt.Sprintf("%s/stores/%s/%s", configuration.ApiUrl, configuration.StoreId, test.RequestPath),
func(req *http.Request) (*http.Response, error) {
resp, err := httpmock.NewJsonResponse(test.ResponseStatus, expectedResponse)
if err != nil {
Expand Down Expand Up @@ -770,7 +770,7 @@ func Test{{appShortName}}Api(t *testing.T) {

httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder(test.Method, fmt.Sprintf("%s://%s/stores/%s/%s", configuration.ApiScheme, configuration.ApiHost, configuration.StoreId, test.RequestPath),
httpmock.RegisterResponder(test.Method, fmt.Sprintf("%s/stores/%s/%s", configuration.ApiUrl, configuration.StoreId, test.RequestPath),
func(req *http.Request) (*http.Response, error) {
resp, err := httpmock.NewJsonResponse(test.ResponseStatus, expectedResponse)
if err != nil {
Expand Down Expand Up @@ -814,7 +814,7 @@ func Test{{appShortName}}Api(t *testing.T) {

httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder(test.Method, fmt.Sprintf("%s://%s/stores/%s/%s", configuration.ApiScheme, configuration.ApiHost, configuration.StoreId, test.RequestPath),
httpmock.RegisterResponder(test.Method, fmt.Sprintf("%s/stores/%s/%s", configuration.ApiUrl, configuration.StoreId, test.RequestPath),
func(req *http.Request) (*http.Response, error) {
resp, err := httpmock.NewJsonResponse(test.ResponseStatus, expectedResponse)
if err != nil {
Expand Down Expand Up @@ -882,7 +882,7 @@ func Test{{appShortName}}Api(t *testing.T) {

httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder(test.Method, fmt.Sprintf("%s://%s/stores/%s/%s", configuration.ApiScheme, configuration.ApiHost, configuration.StoreId, test.RequestPath),
httpmock.RegisterResponder(test.Method, fmt.Sprintf("%s/stores/%s/%s", configuration.ApiUrl, configuration.StoreId, test.RequestPath),
func(req *http.Request) (*http.Response, error) {
resp, err := httpmock.NewJsonResponse(test.ResponseStatus, expectedResponse)
if err != nil {
Expand Down Expand Up @@ -935,7 +935,7 @@ func Test{{appShortName}}Api(t *testing.T) {

httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder(test.Method, fmt.Sprintf("%s://%s/stores/%s/%s", configuration.ApiScheme, configuration.ApiHost, configuration.StoreId, test.RequestPath),
httpmock.RegisterResponder(test.Method, fmt.Sprintf("%s/stores/%s/%s", configuration.ApiUrl, configuration.StoreId, test.RequestPath),
func(req *http.Request) (*http.Response, error) {
errObj := ErrorResponse{
Code: "validation_error",
Expand Down Expand Up @@ -998,7 +998,7 @@ func Test{{appShortName}}Api(t *testing.T) {

httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder(test.Method, fmt.Sprintf("%s://%s/stores/%s/%s", configuration.ApiScheme, configuration.ApiHost, configuration.StoreId, test.RequestPath),
httpmock.RegisterResponder(test.Method, fmt.Sprintf("%s/stores/%s/%s", configuration.ApiUrl, configuration.StoreId, test.RequestPath),
func(req *http.Request) (*http.Response, error) {
errObj := ErrorResponse{
Code: "auth_failure",
Expand Down Expand Up @@ -1054,7 +1054,7 @@ func Test{{appShortName}}Api(t *testing.T) {

httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder(test.Method, fmt.Sprintf("%s://%s/stores/%s/%s", configuration.ApiScheme, configuration.ApiHost, configuration.StoreId, test.RequestPath),
httpmock.RegisterResponder(test.Method, fmt.Sprintf("%s/stores/%s/%s", configuration.ApiUrl, configuration.StoreId, test.RequestPath),
func(req *http.Request) (*http.Response, error) {
errObj := ErrorResponse{
Code: "undefined_endpoint",
Expand Down Expand Up @@ -1117,7 +1117,7 @@ func Test{{appShortName}}Api(t *testing.T) {

httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder(test.Method, fmt.Sprintf("%s://%s/stores/%s/%s", configuration.ApiScheme, configuration.ApiHost, configuration.StoreId, test.RequestPath),
httpmock.RegisterResponder(test.Method, fmt.Sprintf("%s/stores/%s/%s", configuration.ApiUrl, configuration.StoreId, test.RequestPath),
func(req *http.Request) (*http.Response, error) {
errObj := ErrorResponse{
Code: "rate_limit_exceeded",
Expand Down Expand Up @@ -1190,7 +1190,7 @@ func Test{{appShortName}}Api(t *testing.T) {
defer httpmock.DeactivateAndReset()
firstMock := httpmock.NewStringResponder(429, "")
secondMock, _ := httpmock.NewJsonResponder(200, expectedResponse)
httpmock.RegisterResponder(test.Method, fmt.Sprintf("%s://%s/stores/%s/%s", configuration.ApiScheme, configuration.ApiHost, configuration.StoreId, test.RequestPath),
httpmock.RegisterResponder(test.Method, fmt.Sprintf("%s/stores/%s/%s", configuration.ApiUrl, configuration.StoreId, test.RequestPath),
firstMock.Then(firstMock).Then(firstMock).Then(secondMock),
)
updatedConfiguration, err := NewConfiguration(Configuration{
Expand Down Expand Up @@ -1250,7 +1250,7 @@ func Test{{appShortName}}Api(t *testing.T) {

httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder(test.Method, fmt.Sprintf("%s://%s/stores/%s/%s", configuration.ApiScheme, configuration.ApiHost, configuration.StoreId, test.RequestPath),
httpmock.RegisterResponder(test.Method, fmt.Sprintf("%s/stores/%s/%s", configuration.ApiUrl, configuration.StoreId, test.RequestPath),
func(req *http.Request) (*http.Response, error) {
errObj := ErrorResponse{
Code: "internal_error",
Expand Down
Loading

0 comments on commit 1ff7a8c

Please sign in to comment.