Skip to content

Commit

Permalink
#960 Amplitude Destination: Allow to provide alternative API endpoint…
Browse files Browse the repository at this point in the history
… (For projects with EU data residency)
  • Loading branch information
absorbb committed Aug 10, 2022
1 parent 3038533 commit 24424e4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
3 changes: 2 additions & 1 deletion configurator/backend/destinations/config_mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,8 @@ func mapAmplitude(aDestination *entities.Destination) (*enconfig.DestinationConf
}

cfg := &enadapters.AmplitudeConfig{
APIKey: aFormData.APIKey,
APIKey: aFormData.APIKey,
Endpoint: aFormData.Endpoint,
}
cfgMap := map[string]interface{}{}
err = mapstructure.Decode(cfg, &cfgMap)
Expand Down
3 changes: 2 additions & 1 deletion configurator/backend/entities/form_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ type AmplitudeFormData struct {
Mode string `firestore:"mode" json:"mode"`
TableName string `firestore:"tableName" json:"tableName"`

APIKey string `firestore:"apiKey" json:"apiKey"`
APIKey string `firestore:"apiKey" json:"apiKey"`
Endpoint string `firestore:"endpoint" json:"endpoint"`
}

//HubSpotFormData entity is stored in main storage (Firebase/Redis)
Expand Down
15 changes: 15 additions & 0 deletions configurator/frontend/catalog/src/destinations/lib/amplitude.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,21 @@ return toAmplitude($)`,
</>
),
},
{
id: "_formData.endpoint",
displayName: "API Endpoint",
required: false,
type: stringType,
documentation: (
<>
Alternative Amplitude API endpoint
<br />
For project with EU data residency set:
<br />
<code>https://api.eu.amplitude.com/2/httpapi</code>
</>
),
},
],
} as const

Expand Down
19 changes: 11 additions & 8 deletions server/adapters/amplitude.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/jitsucom/jitsu/server/utils"
"io/ioutil"
"net/http"
)

const (
amplitudeAPIURL = "https://api.amplitude.com/2/httpapi"
amplitudeDefaultAPIURL = "https://api2.amplitude.com/2/httpapi"
)

//AmplitudeRequest is a dto for sending requests to Amplitude
Expand All @@ -27,12 +28,13 @@ type AmplitudeResponse struct {

//AmplitudeRequestFactory is a factory for building Amplitude HTTP requests from input events
type AmplitudeRequestFactory struct {
apiKey string
apiKey string
endpoint string
}

//newAmplitudeRequestFactory returns configured HTTPRequestFactory instance for amplitude requests
func newAmplitudeRequestFactory(apiKey string) (HTTPRequestFactory, error) {
return &AmplitudeRequestFactory{apiKey: apiKey}, nil
func newAmplitudeRequestFactory(apiKey, endpoint string) (HTTPRequestFactory, error) {
return &AmplitudeRequestFactory{apiKey: apiKey, endpoint: endpoint}, nil
}

//Create returns created amplitude request
Expand All @@ -50,7 +52,7 @@ func (arf *AmplitudeRequestFactory) Create(object map[string]interface{}) (*Requ
return nil, fmt.Errorf("Error marshalling amplitude request [%v]: %v", req, err)
}
return &Request{
URL: amplitudeAPIURL,
URL: utils.NvlString(arf.endpoint, amplitudeDefaultAPIURL),
Method: http.MethodPost,
Body: b,
Headers: map[string]string{"Content-Type": "application/json"},
Expand All @@ -62,7 +64,8 @@ func (arf *AmplitudeRequestFactory) Close() {

//AmplitudeConfig is a dto for parsing Amplitude configuration
type AmplitudeConfig struct {
APIKey string `mapstructure:"api_key" json:"api_key,omitempty" yaml:"api_key,omitempty"`
APIKey string `mapstructure:"api_key" json:"api_key,omitempty" yaml:"api_key,omitempty"`
Endpoint string `mapstructure:"endpoint" json:"endpoint,omitempty" yaml:"endpoint,omitempty"`
}

//Validate returns err if invalid
Expand All @@ -86,7 +89,7 @@ type Amplitude struct {

//NewAmplitude returns configured Amplitude adapter instance
func NewAmplitude(config *AmplitudeConfig, httpAdapterConfiguration *HTTPAdapterConfiguration) (*Amplitude, error) {
httpReqFactory, err := newAmplitudeRequestFactory(config.APIKey)
httpReqFactory, err := newAmplitudeRequestFactory(config.APIKey, config.Endpoint)
if err != nil {
return nil, err
}
Expand All @@ -109,7 +112,7 @@ func NewTestAmplitude(config *AmplitudeConfig) *Amplitude {

//TestAccess sends test request (empty POST) to Amplitude and check if error has occurred
func (a *Amplitude) TestAccess() error {
httpReqFactory, err := newAmplitudeRequestFactory(a.config.APIKey)
httpReqFactory, err := newAmplitudeRequestFactory(a.config.APIKey, a.config.Endpoint)
if err != nil {
return err
}
Expand Down

0 comments on commit 24424e4

Please sign in to comment.