Skip to content

Commit

Permalink
Make /api not mandatory when setting base url (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
strokyl authored Jul 19, 2024
1 parent cbb1311 commit 62a00d4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 24 deletions.
10 changes: 8 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"os"
"regexp"
"strings"

"github.com/conduktor/ctl/resource"
Expand Down Expand Up @@ -32,6 +33,11 @@ type ApiParameter struct {
Insecure bool
}

func uniformizeBaseUrl(baseUrl string) string {
regex := regexp.MustCompile(`(/api)?/?$`)
return regex.ReplaceAllString(baseUrl, "/api")
}

func Make(apiParameter ApiParameter) (*Client, error) {
//apiKey is set later because it's not mandatory for getting the openapi and parsing different kind
//or to get jwt token
Expand Down Expand Up @@ -64,7 +70,7 @@ func Make(apiParameter ApiParameter) (*Client, error) {

result := &Client{
apiKey: apiParameter.ApiKey,
baseUrl: apiParameter.BaseUrl,
baseUrl: uniformizeBaseUrl(apiParameter.BaseUrl),
client: restyClient,
kinds: nil,
}
Expand Down Expand Up @@ -210,7 +216,7 @@ func (client *Client) Get(kind *schema.Kind, parentPathValue []string) ([]resour
}

func (client *Client) Login(username, password string) (LoginResult, error) {
url := client.baseUrl + "/api/login"
url := client.baseUrl + "/login"
resp, err := client.client.R().SetBody(map[string]string{"username": username, "password": password}).Post(url)
if err != nil {
return LoginResult{}, err
Expand Down
35 changes: 25 additions & 10 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ var aResource = resource.Resource{
Json: []byte(`{"apiVersion":"v1","kind":"Topic","metadata":{"name":"abc.myTopic"},"spec":{"replicationFactor":1}}`),
}

func TestUniformizeBaseUrl(t *testing.T) {
validUrls := []string{
"http://baseUrl/api/",
"http://baseUrl/api",
"http://baseUrl/",
"http://baseUrl",
}
expectedResult := "http://baseUrl/api"
for _, url := range validUrls {
finalUrl := uniformizeBaseUrl(url)
if finalUrl != expectedResult {
t.Errorf("When uniformize %s got %s expected %s", url, finalUrl, expectedResult)
}
}
}
func TestApplyShouldWork(t *testing.T) {
defer httpmock.Reset()
baseUrl := "http://baseUrl"
Expand Down Expand Up @@ -45,7 +60,7 @@ func TestApplyShouldWork(t *testing.T) {

httpmock.RegisterMatcherResponderWithQuery(
"PUT",
"http://baseUrl/public/kafka/v2/cluster/local/topic",
"http://baseUrl/api/public/kafka/v2/cluster/local/topic",
nil,
httpmock.HeaderIs("Authorization", "Bearer "+apiKey).
And(httpmock.HeaderIs("X-CDK-CLIENT", "CLI/unknown")).
Expand Down Expand Up @@ -87,7 +102,7 @@ func TestApplyWithDryModeShouldWork(t *testing.T) {

httpmock.RegisterMatcherResponderWithQuery(
"PUT",
"http://baseUrl/public/self-serve/v1/application",
"http://baseUrl/api/public/self-serve/v1/application",
"dryMode=true",
httpmock.HeaderIs("Authorization", "Bearer "+apiKey).
And(httpmock.BodyContainsBytes(topic.Json)),
Expand Down Expand Up @@ -131,7 +146,7 @@ func TestApplyShouldFailIfNo2xx(t *testing.T) {

httpmock.RegisterMatcherResponderWithQuery(
"PUT",
"http://baseUrl/public/self-serve/v1/application",
"http://baseUrl/api/public/self-serve/v1/application",
nil,
httpmock.HeaderIs("Authorization", "Bearer "+apiKey).
And(httpmock.BodyContainsBytes(topic.Json)),
Expand Down Expand Up @@ -165,7 +180,7 @@ func TestGetShouldWork(t *testing.T) {

httpmock.RegisterMatcherResponderWithQuery(
"GET",
"http://baseUrl/public/self-serve/v1/application",
"http://baseUrl/api/public/self-serve/v1/application",
nil,
httpmock.HeaderIs("Authorization", "Bearer "+apiKey).
And(httpmock.HeaderIs("X-CDK-CLIENT", "CLI/unknown")),
Expand Down Expand Up @@ -203,7 +218,7 @@ func TestGetShouldFailIfN2xx(t *testing.T) {

httpmock.RegisterMatcherResponderWithQuery(
"GET",
"http://baseUrl/public/self-serve/v1/application",
"http://baseUrl/api/public/self-serve/v1/application",
nil,
httpmock.HeaderIs("Authorization", "Bearer "+apiKey),
responder,
Expand Down Expand Up @@ -237,7 +252,7 @@ func TestDescribeShouldWork(t *testing.T) {

httpmock.RegisterMatcherResponderWithQuery(
"GET",
"http://baseUrl/public/self-serve/v1/application/yo",
"http://baseUrl/api/public/self-serve/v1/application/yo",
nil,
httpmock.HeaderIs("Authorization", "Bearer "+apiKey).
And(httpmock.HeaderIs("X-CDK-CLIENT", "CLI/unknown")),
Expand Down Expand Up @@ -276,7 +291,7 @@ func TestDescribeShouldFailIfNo2xx(t *testing.T) {

httpmock.RegisterMatcherResponderWithQuery(
"GET",
"http://baseUrl/public/self-serve/v1/application/yo",
"http://baseUrl/api/public/self-serve/v1/application/yo",
nil,
httpmock.HeaderIs("Authorization", "Bearer "+apiKey),
responder,
Expand Down Expand Up @@ -310,7 +325,7 @@ func TestDeleteShouldWork(t *testing.T) {

httpmock.RegisterMatcherResponderWithQuery(
"DELETE",
"http://baseUrl/public/self-serve/v1/application/yo",
"http://baseUrl/api/public/self-serve/v1/application/yo",
nil,
httpmock.HeaderIs("Authorization", "Bearer "+apiKey).
And(httpmock.HeaderIs("X-CDK-CLIENT", "CLI/unknown")),
Expand Down Expand Up @@ -345,7 +360,7 @@ func TestDeleteResourceShouldWork(t *testing.T) {

httpmock.RegisterMatcherResponderWithQuery(
"DELETE",
"http://baseUrl/public/kafka/v2/cluster/local/topic/toto",
"http://baseUrl/api/public/kafka/v2/cluster/local/topic/toto",
nil,
httpmock.HeaderIs("Authorization", "Bearer "+apiKey).
And(httpmock.HeaderIs("X-CDK-CLIENT", "CLI/unknown")),
Expand Down Expand Up @@ -382,7 +397,7 @@ func TestDeleteShouldFailOnNot2XX(t *testing.T) {

httpmock.RegisterMatcherResponderWithQuery(
"DELETE",
"http://baseUrl/public/self_serve/v1/api/application/yo",
"http://baseUrl/api/public/self_serve/v1/api/application/yo",
nil,
httpmock.HeaderIs("Authorization", "Bearer "+apiKey),
responder,
Expand Down
24 changes: 12 additions & 12 deletions docker/initializer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
"httpRequest": {
"method": "Put",
"path": "/public/kafka/v2/cluster/my-cluster/topic",
"path": "/api/public/kafka/v2/cluster/my-cluster/topic",
"headers": {
"Authorization": "Bearer yo"
}
Expand All @@ -20,7 +20,7 @@
{
"httpRequest": {
"method": "Get",
"path": "/public/kafka/v2/cluster/my-cluster/topic",
"path": "/api/public/kafka/v2/cluster/my-cluster/topic",
"headers": {
"Authorization": "Bearer yo"
}
Expand All @@ -38,7 +38,7 @@
{
"httpRequest": {
"method": "Get",
"path": "/public/kafka/v2/cluster/my-cluster/topic/yolo",
"path": "/api/public/kafka/v2/cluster/my-cluster/topic/yolo",
"headers": {
"Authorization": "Bearer yo"
}
Expand All @@ -56,7 +56,7 @@
{
"httpRequest": {
"method": "Delete",
"path": "/public/kafka/v2/cluster/my-cluster/topic/yolo",
"path": "/api/public/kafka/v2/cluster/my-cluster/topic/yolo",
"headers": {
"Authorization": "Bearer yo"
}
Expand All @@ -74,7 +74,7 @@
{
"httpRequest": {
"method": "Delete",
"path": "/public/kafka/v2/cluster/my-cluster/topic/abcd.topic",
"path": "/api/public/kafka/v2/cluster/my-cluster/topic/abcd.topic",
"headers": {
"Authorization": "Bearer yo"
}
Expand All @@ -92,7 +92,7 @@
{
"httpRequest": {
"method": "Delete",
"path": "/public/kafka/v2/cluster/my-cluster/topic/abcd.myTopicWrong",
"path": "/api/public/kafka/v2/cluster/my-cluster/topic/abcd.myTopicWrong",
"headers": {
"Authorization": "Bearer yo"
}
Expand Down Expand Up @@ -134,7 +134,7 @@
{
"httpRequest": {
"method": "Get",
"path": "/token/v1/admin_tokens",
"path": "/api/token/v1/admin_tokens",
"headers": {
"Authorization": "Bearer yo"
}
Expand All @@ -152,7 +152,7 @@
{
"httpRequest": {
"method": "GET",
"path": "/token/v1/application_instance_tokens/my_app_instance",
"path": "/api/token/v1/application_instance_tokens/my_app_instance",
"headers": {
"Authorization": "Bearer yo"
}
Expand All @@ -170,7 +170,7 @@
{
"httpRequest": {
"method": "Post",
"path": "/token/v1/admin_tokens",
"path": "/api/token/v1/admin_tokens",
"headers": {
"Authorization": "Bearer yo"
},
Expand All @@ -196,7 +196,7 @@
{
"httpRequest": {
"method": "Post",
"path": "/token/v1/application_instance_tokens/my_app_instance",
"path": "/api/token/v1/application_instance_tokens/my_app_instance",
"headers": {
"Authorization": "Bearer yo"
},
Expand All @@ -222,7 +222,7 @@
{
"httpRequest": {
"method": "DELETE",
"path": "/token/v1/0-0-0-0-0",
"path": "/api/token/v1/0-0-0-0-0",
"headers": {
"Authorization": "Bearer yo"
}
Expand All @@ -234,7 +234,7 @@
{
"httpRequest": {
"method": "Get",
"path": "/public/console/v2/kafka-cluster/my_kafka_cluster",
"path": "/api/public/console/v2/kafka-cluster/my_kafka_cluster",
"headers": {
"Authorization": "Bearer yo_from_login"
}
Expand Down

0 comments on commit 62a00d4

Please sign in to comment.