Skip to content

Commit

Permalink
Feature: Added endpoint_metadata information to the response struct f…
Browse files Browse the repository at this point in the history
…or 'all' subscription endpoint. Created customString type to handle JSON values that can be either strings or integers. The api response sometimes returns a string or an integer depending on the endpoint that is being hit, so setting the type to a simple int does not resolve the issue - as it will break for other endpoints.

- Introduced customString type to manage JSON values that may be strings or integers.
- Implemented custom UnmarshalJSON and MarshalJSON methods for customString.
- Updated EndpointResponse struct to use customString for fields requiring flexible JSON handling.
  • Loading branch information
lil-sahil committed Jun 21, 2024
1 parent 4603eb5 commit 3c0bb42
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
33 changes: 30 additions & 3 deletions endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package convoy_go

import (
"context"
"encoding/json"
"errors"
"fmt"
"time"
Expand Down Expand Up @@ -34,11 +35,37 @@ type CreateEndpointRequest struct {
RateLimitDuration string `json:"rate_limit_duration,omitempty"`
}

// customString is a reusable type to handle JSON values that can be either strings or integers.
// It unmarshals JSON data into a string representation, regardless of whether the input is a string or an integer.
type customString string

func (c *customString) UnmarshalJSON(b []byte) error {
var strValue string
var intValue int

if err := json.Unmarshal(b, &strValue); err == nil {
*c = customString(strValue)
return nil
}

if err := json.Unmarshal(b, &intValue); err == nil {
*c = customString(fmt.Sprintf("%d", intValue))
return nil
}

return fmt.Errorf("customstring: cannot unmarshal %v into Go value", string(b))
}

func (c *customString) MarshalJSON() ([]byte, error) {
return json.Marshal(string(*c))
}

type EndpointResponse struct {
UID string `json:"uid"`
GroupID string `json:"group_id"`
OwnerID string `json:"owner_id"`
TargetUrl string `json:"target_url"`
URL string `json:"url"`
Title string `json:"title"`
Description string `json:"description"`

Expand All @@ -49,9 +76,9 @@ type EndpointResponse struct {
SupportEmail string `json:"support_email"`
IsDisabled bool `json:"is_disabled"`

HttpTimeout string `json:"http_timeout"`
RateLimit int `json:"rate_limit"`
RateLimitDuration string `json:"rate_limit_duration"`
HttpTimeout customString `json:"http_timeout"`
RateLimit customString `json:"rate_limit"`
RateLimitDuration customString `json:"rate_limit_duration"`

Authentication *EndpointAuth `json:"authentication"`
Events int64 `json:"events"`
Expand Down
3 changes: 2 additions & 1 deletion subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ type SubscriptionResponse struct {
Type string `json:"type"`
Status string `json:"status"`

Source *SourceResponse `json:"source_metadata,omitempty"`
Source *SourceResponse `json:"source_metadata,omitempty"`
EndpointMetaData *EndpointResponse `json:"endpoint_metadata"`

// subscription config
AlertConfig *AlertConfiguration `json:"alert_config,omitempty"`
Expand Down

0 comments on commit 3c0bb42

Please sign in to comment.