Skip to content

Commit

Permalink
♻️ refactor: updated api curl model and service #9
Browse files Browse the repository at this point in the history
  • Loading branch information
arisnguyen215 committed Dec 13, 2023
1 parent 8194a29 commit 8b61110
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 21 deletions.
41 changes: 40 additions & 1 deletion apix/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,11 @@ func (e *EndpointConfig) SetTelegram(value telegram.TelegramConfig) *EndpointCon
return e
}

func (e *EndpointConfig) SetTelegramOptions(value EndpointOptionsConfig) *EndpointConfig {
e.TelegramOptions = value
return e
}

func (e *EndpointConfig) Json() string {
return utils.ToJson(e)
}
Expand All @@ -269,10 +274,44 @@ func GetEndpointSample() *EndpointConfig {
AppendBody("email", "tester@gmail.com").
SetAuthentication(*GetAuthenticationSample()).
SetRetry(*GetRetrySample()).
SetTelegram(*telegram.GetTelegramConfigSample())
SetTelegram(*telegram.GetTelegramConfigSample()).
SetTelegramOptions(*NewEndpointOptionsConfig())
return e
}

func NewEndpointOptionsConfig() *EndpointOptionsConfig {
return &EndpointOptionsConfig{}
}

func (e *EndpointOptionsConfig) SetSkipMessageHeader(value bool) *EndpointOptionsConfig {
e.SkipMessageHeader = value
return e
}

func (e *EndpointOptionsConfig) SetSkipMessageRequestBody(value bool) *EndpointOptionsConfig {
e.SkipMessageRequestBody = value
return e
}

func (e *EndpointOptionsConfig) SetSkipMessageResponseBody(value bool) *EndpointOptionsConfig {
e.SkipMessageResponseBody = value
return e
}

func (e *EndpointOptionsConfig) SetSkipMessageQueryParam(value bool) *EndpointOptionsConfig {
e.SkipMessageQueryParam = value
return e
}

func (e *EndpointOptionsConfig) SetSkipMessagePathParam(value bool) *EndpointOptionsConfig {
e.SkipMessagePathParam = value
return e
}

func (e *EndpointOptionsConfig) Json() string {
return utils.ToJson(e)
}

func NewApiRequest() *ApiRequestConfig {
a := &ApiRequestConfig{}
return a
Expand Down
37 changes: 23 additions & 14 deletions apix/api_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,29 @@ type RetryConfig struct {
}

type EndpointConfig struct {
IsEnabled bool `json:"enabled" yaml:"enabled"`
DebugMode bool `json:"debug_mode" yaml:"debug_mode"`
BaseURL string `json:"base_url" yaml:"base_url"`
Timeout time.Duration `json:"timeout" yaml:"timeout"`
Path string `json:"path" yaml:"path"`
Method string `json:"method" yaml:"method"`
Description string `json:"description" yaml:"description"`
QueryParams map[string]string `json:"query_params" yaml:"query_params"`
PathParams map[string]string `json:"path_params" yaml:"path_params"`
Headers map[string]string `json:"headers" yaml:"headers"`
Body map[string]interface{} `json:"body" yaml:"body"`
Retry RetryConfig `json:"retry" yaml:"retry"`
Authentication AuthenticationConfig `json:"authentication" yaml:"authentication"`
Telegram telegram.TelegramConfig `json:"telegram" yaml:"telegram"`
IsEnabled bool `json:"enabled" yaml:"enabled"`
DebugMode bool `json:"debug_mode" yaml:"debug_mode"`
BaseURL string `json:"base_url" yaml:"base_url"`
Timeout time.Duration `json:"timeout" yaml:"timeout"`
Path string `json:"path" yaml:"path"`
Method string `json:"method" yaml:"method"`
Description string `json:"description" yaml:"description"`
QueryParams map[string]string `json:"query_params" yaml:"query_params"`
PathParams map[string]string `json:"path_params" yaml:"path_params"`
Headers map[string]string `json:"headers" yaml:"headers"`
Body map[string]interface{} `json:"body" yaml:"body"`
Retry RetryConfig `json:"retry" yaml:"retry"`
Authentication AuthenticationConfig `json:"authentication" yaml:"authentication"`
Telegram telegram.TelegramConfig `json:"telegram" yaml:"telegram"`
TelegramOptions EndpointOptionsConfig `json:"telegram_options" yaml:"telegram_options"`
}

type EndpointOptionsConfig struct {
SkipMessageHeader bool `json:"skip_message_header" yaml:"skip_message_header"`
SkipMessageRequestBody bool `json:"skip_message_request_body" yaml:"skip_message_request_body"`
SkipMessageResponseBody bool `json:"skip_message_response_body" yaml:"skip_message_response_body"`
SkipMessageQueryParam bool `json:"skip_message_query_param" yaml:"skip_message_query_param"`
SkipMessagePathParam bool `json:"skip_message_path_param" yaml:"skip_message_path_param"`
}

type ApiRequestConfig struct {
Expand Down
12 changes: 6 additions & 6 deletions apix/api_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (s *apiServiceImpl) execute(client *restify.Client, endpoint EndpointConfig
telegramSvc := telegram.NewTelegramService(telegramConf, *options)
s.telegramSvc = telegramSvc

client.SetHostURL(host)
client.SetBaseURL(host)
client.SetHeaders(headers)
client.SetDebug(endpoint.DebugMode)

Expand Down Expand Up @@ -214,24 +214,24 @@ func (s *apiServiceImpl) alert(endpoint EndpointConfig, response *restify.Respon
}
message.WriteString(fmt.Sprintf("(`%s`) URL: `%s`\n", response.Request.Method, response.Request.URL))
message.WriteString("\n---\n")
if endpoint.AvailableHeaders() {
if endpoint.AvailableHeaders() && !endpoint.TelegramOptions.SkipMessageHeader {
message.WriteString(fmt.Sprintf("Header(s): \n\t`%s`\n", coltx.MapString2Table(endpoint.Headers)))
}
if endpoint.AvailableQueryParams() {
if endpoint.AvailableQueryParams() && !endpoint.TelegramOptions.SkipMessageQueryParam {
message.WriteString(fmt.Sprintf("Query Param(s): `%s`\n", coltx.MapString2Table(endpoint.QueryParams)))
}
if endpoint.AvailablePathParams() {
if endpoint.AvailablePathParams() && !endpoint.TelegramOptions.SkipMessagePathParam {
message.WriteString(fmt.Sprintf("Path Param(s): `%s`\n", coltx.MapString2Table(endpoint.PathParams)))
}
if endpoint.AvailableBody() {
if endpoint.AvailableBody() && !endpoint.TelegramOptions.SkipMessageRequestBody {
message.WriteString(fmt.Sprintf("Request Body: `%s`\n", utils.ToJson(endpoint.Body)))
}
if endpoint.Retry.AvailableRetryOnStatus() {
message.WriteString(fmt.Sprintf("Retry On Status: `%s`\n", utils.ToJson(endpoint.Retry.RetryOnStatus)))
}
message.WriteString("\n---\n")
message.WriteString(fmt.Sprintf("Status Code: %v\n", response.StatusCode()))
if utils.IsNotEmpty(response.String()) {
if utils.IsNotEmpty(response.String()) && !endpoint.TelegramOptions.SkipMessageResponseBody {
message.WriteString(fmt.Sprintf("Response: `%v`\n", response.String()))
}
message.WriteString(fmt.Sprintf("No. attempt: %v\n", response.Request.Attempt))
Expand Down

0 comments on commit 8b61110

Please sign in to comment.