Skip to content

Commit 022a37b

Browse files
Add dummy jobs client
1 parent 8853bd9 commit 022a37b

7 files changed

Lines changed: 237 additions & 3 deletions

File tree

‎databricks/internal/internal.go‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ type ClientOptions struct {
1414
Credentials auth.Credentials
1515
Timeout time.Duration
1616
Logger *slog.Logger
17-
Debug bool
1817
}
1918

2019
// Initialize initializes the client options and validates its configuration.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Package unstable contains functionalities that need to be shared
2+
// between SDK packages but are not considered part of the public API
3+
// of the Databricks SDKs.
4+
//
5+
// IMPORTANT: Clients should not directly depend on functionalities in
6+
// this package. All functionalities in this package are considered as
7+
// UNSTABLE and may change without notice.
8+
package unstable
9+
10+
import (
11+
"log/slog"
12+
13+
"github.com/databricks/sdk-go/databricks/internal"
14+
"github.com/databricks/sdk-go/databricks/options"
15+
)
16+
17+
// ResolvedOptions contains the resolved configuration values from ClientOptions.
18+
type ResolvedOptions struct {
19+
Logger *slog.Logger
20+
}
21+
22+
// Resolve applies all ClientOptions and returns the resolved configuration.
23+
func Resolve(opts ...options.ClientOption) (ResolvedOptions, error) {
24+
o := &internal.ClientOptions{}
25+
for _, opt := range opts {
26+
if err := opt(o); err != nil {
27+
return ResolvedOptions{}, err
28+
}
29+
}
30+
return ResolvedOptions{
31+
Logger: o.Logger,
32+
}, nil
33+
}

‎go.work‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
go 1.25.4
22

33
use (
4-
./auth
5-
./databricks
4+
./auth
5+
./databricks
6+
./jobs
67
)

‎jobs/go.mod‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/databricks/sdk-go/jobs
2+
3+
go 1.25.4

‎jobs/v2/genhelper.go‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package jobs
2+
3+
import (
4+
"io"
5+
"log/slog"
6+
"net/http"
7+
8+
"github.com/databricks/sdk-go/databricks/apierr"
9+
)
10+
11+
type httpCallOptions struct {
12+
req *http.Request
13+
client *http.Client
14+
logger *slog.Logger
15+
}
16+
17+
// lazyRequest implements slog.LogValuer for lazy evaluation of request
18+
// logging.
19+
type lazyRequest struct{ req *http.Request }
20+
21+
func (r lazyRequest) LogValue() slog.Value {
22+
return slog.GroupValue(
23+
slog.String("method", r.req.Method),
24+
slog.String("url", r.req.URL.String()),
25+
)
26+
}
27+
28+
// lazyResponse implements slog.LogValuer for lazy evaluation of response
29+
// logging.
30+
type lazyResponse struct {
31+
resp *http.Response
32+
body []byte
33+
}
34+
35+
func (r lazyResponse) LogValue() slog.Value {
36+
return slog.GroupValue(
37+
slog.Int("status", r.resp.StatusCode),
38+
slog.String("body", string(r.body)),
39+
)
40+
}
41+
42+
// executeHTTPCall executes an HTTP call and returns the response body or
43+
// and error. This function takes care of logging the request and response.
44+
func executeHTTPCall(opts httpCallOptions) ([]byte, error) {
45+
opts.logger.Debug("HTTP request", "request", lazyRequest{opts.req})
46+
resp, err := opts.client.Do(opts.req)
47+
if err != nil {
48+
return nil, err
49+
}
50+
defer resp.Body.Close()
51+
body, err := io.ReadAll(resp.Body)
52+
if err != nil {
53+
opts.logger.Debug("HTTP response", "response", lazyResponse{resp, nil})
54+
return nil, err
55+
}
56+
opts.logger.Debug("HTTP response", "response", lazyResponse{resp, body})
57+
if err := apierr.FromHTTPError(resp.StatusCode, resp.Header, body); err != nil {
58+
return nil, err
59+
}
60+
return body, nil
61+
}

‎jobs/v2/jobs.go‎

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package jobs
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"encoding/json"
7+
"iter"
8+
"log/slog"
9+
"net/http"
10+
"net/url"
11+
12+
"github.com/databricks/sdk-go/databricks/api"
13+
"github.com/databricks/sdk-go/databricks/options"
14+
"github.com/databricks/sdk-go/databricks/options/unstable"
15+
"github.com/databricks/sdk-go/databricks/transport"
16+
)
17+
18+
type Client struct {
19+
httpClient *http.Client
20+
logger *slog.Logger
21+
host string
22+
}
23+
24+
func NewClient(ctx context.Context, opts ...options.ClientOption) (*Client, error) {
25+
resolved, err := unstable.Resolve(opts...)
26+
if err != nil {
27+
return nil, err
28+
}
29+
httpClient, err := transport.NewHTTPClient(ctx, opts...)
30+
if err != nil {
31+
return nil, err
32+
}
33+
34+
return &Client{
35+
httpClient: httpClient,
36+
logger: resolved.Logger,
37+
}, nil
38+
}
39+
40+
func (c *Client) CreateJob(ctx context.Context, req *CreateJobRequest, opts ...api.Option) (*Job, error) {
41+
body, err := json.Marshal(req)
42+
if err != nil {
43+
return nil, err
44+
}
45+
46+
headers := http.Header{}
47+
headers.Set("Content-Type", "application/json")
48+
49+
baseURL, err := url.Parse(c.host)
50+
if err != nil {
51+
return nil, err
52+
}
53+
queryParams := url.Values{}
54+
baseURL.RawQuery = queryParams.Encode()
55+
56+
resp := &Job{}
57+
call := func(ctx context.Context) error {
58+
httpReq, err := http.NewRequest("POST", baseURL.String(), bytes.NewBuffer(body))
59+
if err != nil {
60+
return err
61+
}
62+
httpReq = httpReq.WithContext(ctx)
63+
httpReq.Header = headers
64+
65+
respBody, err := executeHTTPCall(httpCallOptions{
66+
req: httpReq,
67+
client: c.httpClient,
68+
logger: c.logger,
69+
})
70+
if err != nil {
71+
return err
72+
}
73+
if err := json.Unmarshal(respBody, resp); err != nil {
74+
return err
75+
}
76+
return nil
77+
}
78+
79+
if err := api.Execute(ctx, call, opts...); err != nil {
80+
return nil, err
81+
}
82+
return resp, nil
83+
}
84+
85+
func ListJobs(ctx context.Context, req *ListJobsRequest, opts ...api.Option) (*ListJobsResponse, error) {
86+
return nil, nil
87+
}
88+
89+
func ListJobsIter(ctx context.Context, req *ListJobsRequest, opts ...api.Option) iter.Seq2[*Job, error] {
90+
return func(yield func(*Job, error) bool) {
91+
pageReq := &ListJobsRequest{
92+
PageSize: req.PageSize,
93+
PageToken: req.PageToken,
94+
}
95+
for {
96+
resp, err := ListJobs(ctx, pageReq, opts...)
97+
if err != nil {
98+
yield(nil, err)
99+
return
100+
}
101+
for i := range resp.Jobs {
102+
if !yield(&resp.Jobs[i], nil) {
103+
return
104+
}
105+
}
106+
if resp.NextPageToken == "" {
107+
return
108+
}
109+
pageReq.PageToken = resp.NextPageToken
110+
}
111+
}
112+
}

‎jobs/v2/model.go‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package jobs
2+
3+
import "time"
4+
5+
type Job struct {
6+
ID string `json:"id"`
7+
Name string `json:"name"`
8+
Description string `json:"description"`
9+
CreatedAt time.Time `json:"created_at"`
10+
UpdatedAt time.Time `json:"updated_at"`
11+
}
12+
13+
type CreateJobRequest struct {
14+
Job Job `json:"job"`
15+
}
16+
17+
type ListJobsRequest struct {
18+
PageSize int `json:"page_size"`
19+
PageToken string `json:"page_token"`
20+
}
21+
22+
type ListJobsResponse struct {
23+
Jobs []Job `json:"jobs"`
24+
NextPageToken string `json:"next_page_token"`
25+
}

0 commit comments

Comments
 (0)