Example Golang

A example for a golang client that enrolls data within our solution

Introduction and project setup

Go (Golang) is a statically typed, compiled programming language developed by Google, known for its simplicity, efficiency, and excellent concurrency support, making it ideal for building scalable web services, APIs, and microservices. To manage project dependencies in Go, you use Go modules. You can initialize a new Go module in your project directory by running the terminal command go mod init <module-name>, where <module-name> is typically your repository URL (e.g., github.com/username/project). This creates a go.mod file that tracks your dependencies. When you import packages and run go mod tidy, Go automatically downloads and manages the required dependencies. Go’s module system ensures reproducible builds and proper version management. You can build your project with go build and run it directly with go run main.go. This approach keeps your projects organized and ensures consistent dependency management across different environments.

Lets go

We highly recommend to use a unix based development environment. This is not only possible by using linux or macOS but also by installing Linux Subsystem for Windows. If you use a plan windows you might need to adapt in certain steps.

Generate API code

For golang we recommend using ogen, to generate client code required to interact with the API.

mkdir omniac-client
cd omniac-client
go mod init github.com/yourusername/omniac-client

Now add the following to your generated go.mod file to add the dependency ogen:

module o4b_client_test

go 1.25.3

tool github.com/ogen-go/ogen/cmd/ogen
require (
    [...]
)

Now run go mod tidy and then create a generate.go file with the following content:

package main

//go:generate go tool ogen --target pkg/o4b --package o4b --clean openapi.yaml

If you now place the omniac Business OpenAPI specification as openapi.yaml in your project root, you can generate the API client with the following command:

go generate

Authentication setup

Now create a main.go file and import the generated code to configure the client. You will need the url as well as your api key. This code needs to be executed before any api call.

package main

import (
	"context"
	omniac "o4b_client_test/pkg/o4b"
)

type APIKeyProvider struct {
	ApiKey string
}

func (a APIKeyProvider) ApiKeyAuth(ctx context.Context, operationName omniac.OperationName) (omniac.ApiKeyAuth, error) {
	return omniac.ApiKeyAuth{APIKey: a.ApiKey}, nil
}

func main() {
	sec := APIKeyProvider{
		ApiKey: "your_api_key_here",
	}
	client, err := omniac.NewClient("https://api.omniac.de", sec)
	if err != nil {
		panic(err)
	}

}

Get tenant info

After initializing the api instance you can use the GetTenant function of your client to get information about your tenant.

[...]

ctx := context.Background()
tenantRes, err := client.GetTenant(ctx)
if err != nil {
    panic(err)
}

switch res := tenantRes.(type) {
case *omniac.Tenant:
    fmt.Println(res)
case *omniac.GetTenantBadRequest:
    fmt.Println("Bad Request:", res)
case *omniac.GetTenantUnauthorized:
    fmt.Println("Unauthorized:", res)
case *omniac.GetTenantInternalServerError:
    fmt.Println("Internal Server Error:", res)
default:
    fmt.Println("Unexpected response type")
}

Add profile to tenant

In order to start the monitoring of attributes and to receive alerts you need to configure a user for your tenant. After successfully creating the profile, you can start adding attributes.

[...]
profileRes, err := client.CreateProfile(ctx, &omniac.ProfileCreateRequest{
    UserLanguage: omniac.NewOptString("de"),
    Name:         "test",
})
if err != nil {
    panic(err)
}

var profile *omniac.Profile
switch res := profileRes.(type) {
case *omniac.Profile:
    profile = res
    fmt.Println(res)
case *omniac.CreateProfileBadRequest:
    fmt.Println("Bad Request:", res)
case *omniac.CreateProfileUnauthorized:
    fmt.Println("Unauthorized:", res)
case *omniac.CreateProfileInternalServerError:
    fmt.Println("Internal Server Error:", res)
default:
    fmt.Println("Unexpected response type")
}

attributeRes, err := client.ReplaceAttributes(ctx, []omniac.AttributeCreateRequest{
    {
        Type: "email",
        Value: "test@example.com",
    }},
    omniac.ReplaceAttributesParams{
        ProfileID: profile.ID,
    })
if err != nil {
    panic(err)
}

switch res := attributeRes.(type) {
case *omniac.ReplaceAttributesOK:
    fmt.Println(res)
case *omniac.ReplaceAttributesBadRequest:
    fmt.Println("Bad Request:", res)
case *omniac.ReplaceAttributesUnauthorized:
    fmt.Println("Unauthorized:", res)
case *omniac.ReplaceAttributesInternalServerError:
    fmt.Println("Internal Server Error:", res)
default:
    fmt.Println("Unexpected response type")
}

Create multiple profiles as a batch

In addition to the option of creating individual profiles, omniac Business also offers the ability to create up to 500 profiles with attributes at the same time.

	batchProfileRes, err := client.CreateProfiles(ctx, []omniac.ProfileCreateBatchRequest{
		{
			UserLanguage: omniac.NewOptString("de"),
			Name:         "batch_test_1",
			Attributes: []omniac.AttributeCreateRequest{
				{
					Type:  "email",
					Value: "test@example.com",
				},
			},
		},
		{
			UserLanguage: omniac.NewOptString("en"),
			Name:         "batch_test_2",
			Attributes: []omniac.AttributeCreateRequest{
				{
					Type:  "email",
					Value: "test2@example.com",
				},
			},
		},
	})

	if err != nil {
		panic(err)
	}

	switch res := batchProfileRes.(type) {
	case *omniac.CreateProfilesCreated:
		fmt.Println(res)
	case *omniac.CreateProfilesBadRequest:
		fmt.Println("Bad Request:", res)
	case *omniac.CreateProfilesUnauthorized:
		fmt.Println("Unauthorized:", res)
	case *omniac.CreateProfilesInternalServerError:
		fmt.Println("Internal Server Error:", res)
	default:
		fmt.Println("Unexpected response type")
	}

Get alerts

After waiting some minutes alerts will appear, if there are any leaks found. You can pull them by using the getprofile function with your profile.id. The two parameters after the profileID let you also return the users attributes (hashed and encrypted) as well as the alerts.

[...]
alertRes, err := client.GetAlerts(ctx, omniac.GetAlertsParams{
    ProfileID: omniac.NewOptString(profile.ID),
})
if err != nil {
    panic(err)
}

switch res := alertRes.(type) {
case *omniac.GetAlertsOK:
    fmt.Println(res.Data)
case *omniac.GetAlertsBadRequest:
    fmt.Println("Bad Request:", res)
case *omniac.GetAlertsUnauthorized:
    fmt.Println("Unauthorized:", res)
case *omniac.GetAlertsInternalServerError:
    fmt.Println("Internal Server Error:", res)
default:
    fmt.Println("Unexpected response type")
}

Provide a push alert endpoint

To get notified even faster, we provide a push api for new alerts. As soon as something is found, we will publish the alert into your system. This is done over a HTTP-call. The delivery will be marked as done on our side as soon as your api returns a 2xx http status code.

To use this feature, you need to contact us, so we can enable the feature for your tenant.