|
| 1 | +package lemlist |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + |
| 8 | + "github.com/amp-labs/connectors/common" |
| 9 | + "github.com/amp-labs/connectors/common/naming" |
| 10 | + "github.com/amp-labs/connectors/common/urlbuilder" |
| 11 | +) |
| 12 | + |
| 13 | +func (c *Connector) buildSingleObjectMetadataRequest(ctx context.Context, objectName string) (*http.Request, error) { |
| 14 | + url, err := urlbuilder.New(c.ProviderInfo().BaseURL, restAPIPrefix, objectName) |
| 15 | + if err != nil { |
| 16 | + return nil, err |
| 17 | + } |
| 18 | + |
| 19 | + url.WithQueryParam("version", "v2") |
| 20 | + |
| 21 | + return http.NewRequestWithContext(ctx, http.MethodGet, url.String(), nil) |
| 22 | +} |
| 23 | + |
| 24 | +func (c *Connector) parseSingleObjectMetadataResponse( |
| 25 | + ctx context.Context, |
| 26 | + objectName string, |
| 27 | + request *http.Request, |
| 28 | + response *common.JSONHTTPResponse, |
| 29 | +) (*common.ObjectMetadata, error) { |
| 30 | + var ( |
| 31 | + firstRecord map[string]any |
| 32 | + err error |
| 33 | + ) |
| 34 | + |
| 35 | + objectMetadata := common.ObjectMetadata{ |
| 36 | + FieldsMap: make(map[string]string), |
| 37 | + DisplayName: naming.CapitalizeFirstLetterEveryWord(objectName), |
| 38 | + } |
| 39 | + |
| 40 | + schema, fld := responseSchema(objectName) |
| 41 | + |
| 42 | + switch schema { |
| 43 | + case object: |
| 44 | + firstRecord, err = parseObject(response, fld) |
| 45 | + if err != nil { |
| 46 | + return nil, err |
| 47 | + } |
| 48 | + |
| 49 | + case list: |
| 50 | + firstRecord, err = parseList(response) |
| 51 | + if err != nil { |
| 52 | + return nil, err |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + for fld := range firstRecord { |
| 57 | + objectMetadata.FieldsMap[fld] = fld |
| 58 | + } |
| 59 | + |
| 60 | + return &objectMetadata, nil |
| 61 | +} |
| 62 | + |
| 63 | +func parseObject(response *common.JSONHTTPResponse, fld string) (map[string]any, error) { |
| 64 | + // We're unmarshaling the data to map[string]any, |
| 65 | + // all supported objects returns this data type. |
| 66 | + data, err := common.UnmarshalJSON[map[string]any](response) |
| 67 | + if err != nil { |
| 68 | + return nil, common.ErrFailedToUnmarshalBody |
| 69 | + } |
| 70 | + |
| 71 | + if len(*data) == 0 { |
| 72 | + return nil, common.ErrMissingExpectedValues |
| 73 | + } |
| 74 | + |
| 75 | + firstRecord := *data |
| 76 | + |
| 77 | + if fld != "" { |
| 78 | + // If this is the case, we're expecting the data in a certain field |
| 79 | + // in this current map. |
| 80 | + records, okay := (*data)[fld].([]any) |
| 81 | + if !okay { |
| 82 | + return nil, fmt.Errorf("couldn't convert the data response field data to an array: %w", common.ErrMissingExpectedValues) // nolint:lll |
| 83 | + } |
| 84 | + |
| 85 | + if len(records) == 0 { |
| 86 | + return nil, fmt.Errorf("%w: could not find a record to sample fields from", common.ErrMissingExpectedValues) |
| 87 | + } |
| 88 | + |
| 89 | + // Iterate over the first record. |
| 90 | + firstRecord, okay = records[0].(map[string]any) |
| 91 | + if !okay { |
| 92 | + return nil, fmt.Errorf("couldn't convert the first record data to a map: %w", common.ErrMissingExpectedValues) |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return firstRecord, nil |
| 97 | +} |
| 98 | + |
| 99 | +func parseList(response *common.JSONHTTPResponse) (map[string]any, error) { |
| 100 | + // We're unmarshaling the data to []map[string]any, |
| 101 | + // all supported objects returns this data type. |
| 102 | + data, err := common.UnmarshalJSON[[]map[string]any](response) |
| 103 | + if err != nil { |
| 104 | + return nil, common.ErrFailedToUnmarshalBody |
| 105 | + } |
| 106 | + |
| 107 | + if len(*data) == 0 { |
| 108 | + return nil, common.ErrMissingExpectedValues |
| 109 | + } |
| 110 | + |
| 111 | + return (*data)[0], nil |
| 112 | +} |
0 commit comments