How to upgrade from v1 to v2

This guide will help you navigate through changes included in version 2 of the Go integration for a successful upgrade.

These changes aim to make the module more consistent and modular by simplifying the client structure, unifying its namespace, improving instantiation with the functional options pattern, and enhancing configuration error handling.

📘

Documentation for version 2.x.x

Please refer to the new documentation for detailed instructions on how to use version 2 of the module.

Overview of changes

  • Rename DataDomeStruct structure to Client
  • Remove DataDome prefix on fields of the Client structure
  • Replace sub-packages with a single modulego package
  • Update the NewClient signature to use the functional options pattern
  • Handle configuration errors during the client's instantiation

Migration steps

Step 1: Upgrade to the latest package version

go get github.com/datadome/module-go-package/v2
go mod tidy

Step 2: Update your integration

Update the instantiation of the DataDome Client and provide your server-side key in the first parameter of the NewClient function:

dd "github.com/datadome/module-go-package/v2"

func main() {
  client, err := dd.NewClient("DATADOME_SERVER_SIDE_KEY")
  if err != nil {
    // Error may be returned in case of misconfiguration
  }
}
dd "github.com/datadome/module-go-package"

func main() {
  client := &datadome.DataDomeStruct{
    DatadomeServerSideKey: "DATADOME_SERVER_SIDE_KEY",
  }
}

Step 3: Update your configuration

This list describes the optional fields that can be customized:

Option name (v1)Option name (v2)DescriptionFunctional optionDefault value
DatadomeServerSideKeyREMOVED (required parameter)Value of your Server Side Key available in the dashboard .--
DataDomeEndpointEndpointHost of the Protection API.WithEndpointapi.datadome.co
DataDomeTimeoutTimeoutTimeout in milliseconds, after which the request will be allowed.WithTimeout150
EnableGraphQLSupportEnableGraphQLSupportEnable the support of GraphQL requests.WithGraphQLSupportfalse
EnableReferrerRestorationEnableReferrerRestorationSet to true to restore original referrer after a challenge is passed.WithReferrerRestorationfalse
UrlPatternExclusionUrlPatternExclusionRegex to match to exclude requests from being processed with the Protection API.
If not defined, all requests will be processed.
WithUrlPatternExclusion(?i)\.(avi|flv|mka|mkv|mov|mp4|mpeg|mpg|mp3|flac|ogg|ogm|opus|wav|webm|webp|bmp|gif|ico|jpeg|jpg|png|svg|svgz|swf|eot|otf|ttf|woff|woff2|css|less|js|map|json)$
UrlPatternInclusionUrlPatternInclusionRegex to match to process the request with the Protection API. If not defined, all requests that don't match UrlPatternExclusion will be processed.WithUrlPatternInclusion-
LoggerThe level-based Logger of your choice.WithLoggerThe standard log package
DebugREMOVEDThe debug mode to log additional information.--

You can find a usage example of the functional options in the snippet below:

client, err := dd.NewClient(
  "Some key",
  dd.WithEndpoint("api.datadome.co"),
  dd.WithTimeout(150),
  dd.WithGraphQLSupport(true),
  dd.WithReferrerRestoration(true),
  dd.WithUrlPatternExclusion(`(?i)\.(avi|flv|mka|mkv|mov|mp4|mpeg|mpg|mp3|flac|ogg|ogm|opus|wav|webm|webp|bmp|gif|ico|jpeg|jpg|png|svg|svgz|swf|eot|otf|ttf|woff|woff2|css|less|js|map|json)$`),
  dd.WithUrlPatternInclusion(`(?i)\/included\/endpoint`),
  dd.Logger(dd.NewDefaultLogger()),
)