Skip to content

Commit 3c0bb42

Browse files
committed
Feature: Added endpoint_metadata information to the response struct for '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.
1 parent 4603eb5 commit 3c0bb42

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

endpoint.go

+30-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package convoy_go
22

33
import (
44
"context"
5+
"encoding/json"
56
"errors"
67
"fmt"
78
"time"
@@ -34,11 +35,37 @@ type CreateEndpointRequest struct {
3435
RateLimitDuration string `json:"rate_limit_duration,omitempty"`
3536
}
3637

38+
// customString is a reusable type to handle JSON values that can be either strings or integers.
39+
// It unmarshals JSON data into a string representation, regardless of whether the input is a string or an integer.
40+
type customString string
41+
42+
func (c *customString) UnmarshalJSON(b []byte) error {
43+
var strValue string
44+
var intValue int
45+
46+
if err := json.Unmarshal(b, &strValue); err == nil {
47+
*c = customString(strValue)
48+
return nil
49+
}
50+
51+
if err := json.Unmarshal(b, &intValue); err == nil {
52+
*c = customString(fmt.Sprintf("%d", intValue))
53+
return nil
54+
}
55+
56+
return fmt.Errorf("customstring: cannot unmarshal %v into Go value", string(b))
57+
}
58+
59+
func (c *customString) MarshalJSON() ([]byte, error) {
60+
return json.Marshal(string(*c))
61+
}
62+
3763
type EndpointResponse struct {
3864
UID string `json:"uid"`
3965
GroupID string `json:"group_id"`
4066
OwnerID string `json:"owner_id"`
4167
TargetUrl string `json:"target_url"`
68+
URL string `json:"url"`
4269
Title string `json:"title"`
4370
Description string `json:"description"`
4471

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

52-
HttpTimeout string `json:"http_timeout"`
53-
RateLimit int `json:"rate_limit"`
54-
RateLimitDuration string `json:"rate_limit_duration"`
79+
HttpTimeout customString `json:"http_timeout"`
80+
RateLimit customString `json:"rate_limit"`
81+
RateLimitDuration customString `json:"rate_limit_duration"`
5582

5683
Authentication *EndpointAuth `json:"authentication"`
5784
Events int64 `json:"events"`

subscription.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ type SubscriptionResponse struct {
5353
Type string `json:"type"`
5454
Status string `json:"status"`
5555

56-
Source *SourceResponse `json:"source_metadata,omitempty"`
56+
Source *SourceResponse `json:"source_metadata,omitempty"`
57+
EndpointMetaData *EndpointResponse `json:"endpoint_metadata"`
5758

5859
// subscription config
5960
AlertConfig *AlertConfiguration `json:"alert_config,omitempty"`

0 commit comments

Comments
 (0)