klefki/pkg/client/client.go

42 lines
1.5 KiB
Go
Raw Permalink Normal View History

// Copyright (C) 2025 klefki contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: AGPL-3.0
package client
import (
"fmt"
pbgrpcv1 "git.rgst.io/homelab/klefki/internal/server/grpc/generated/go/rgst/klefki/v1"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
// Dial creates a new [pbgrpcv1.KlefkiServiceClient] at the given
// address with the provided options. Returned is the client and a
// closer function that can be used to close the underlying transport.
func Dial(address string, opts ...grpc.DialOption) (pbgrpcv1.KlefkiServiceClient, func() error, error) {
if opts == nil {
opts = []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
}
conn, err := grpc.NewClient(address, opts...)
if err != nil {
return nil, nil, fmt.Errorf("failed to connect to klefki server: %w", err)
}
return pbgrpcv1.NewKlefkiServiceClient(conn), conn.Close, nil
}