Skip to content

Commit

Permalink
feat: update via SDK Studio (#1533)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-app[bot] committed Mar 11, 2024
1 parent 84da7e0 commit a71d2c8
Show file tree
Hide file tree
Showing 41 changed files with 9,764 additions and 624 deletions.
2 changes: 1 addition & 1 deletion .stats.yml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
configured_endpoints: 1255
configured_endpoints: 1288
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,31 @@ This library provides some conveniences for working with paginated list endpoint
You can use `.ListAutoPaging()` methods to iterate through items across all pages:

```go
// TODO
iter := client.Accounts.ListAutoPaging(context.TODO(), accounts.AccountListParams{})
// Automatically fetches more pages as needed.
for iter.Next() {
accountListResponse := iter.Current()
fmt.Printf("%+v\n", accountListResponse)
}
if err := iter.Err(); err != nil {
panic(err.Error())
}
```

Or you can use simple `.List()` methods to fetch a single page and receive a standard response object
with additional helper methods like `.GetNextPage()`, e.g.:

```go
// TODO
page, err := client.Accounts.List(context.TODO(), accounts.AccountListParams{})
for page != nil {
for _, account := range page.Result {
fmt.Printf("%+v\n", account)
}
page, err = page.GetNextPage()
}
if err != nil {
panic(err.Error())
}
```

### Errors
Expand Down
186 changes: 158 additions & 28 deletions api.md

Large diffs are not rendered by default.

281 changes: 281 additions & 0 deletions audit_logs/auditlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
package audit_logs

import (
"context"
"fmt"
"net/http"
"net/url"
"time"

"github.com/cloudflare/cloudflare-go/v2/internal/apijson"
"github.com/cloudflare/cloudflare-go/v2/internal/apiquery"
"github.com/cloudflare/cloudflare-go/v2/internal/param"
"github.com/cloudflare/cloudflare-go/v2/internal/requestconfig"
"github.com/cloudflare/cloudflare-go/v2/internal/shared"
"github.com/cloudflare/cloudflare-go/v2/option"
)

Expand All @@ -22,3 +33,273 @@ func NewAuditLogService(opts ...option.RequestOption) (r *AuditLogService) {
r.Options = opts
return
}

// Gets a list of audit logs for an account. Can be filtered by who made the
// change, on which zone, and the timeframe of the change.
func (r *AuditLogService) List(ctx context.Context, params AuditLogListParams, opts ...option.RequestOption) (res *shared.V4PagePaginationArray[AuditLogListResponse], err error) {
var raw *http.Response
opts = append(r.Options, opts...)
opts = append([]option.RequestOption{option.WithResponseInto(&raw)}, opts...)
path := fmt.Sprintf("accounts/%s/audit_logs", params.AccountID)
cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodGet, path, params, &res, opts...)
if err != nil {
return nil, err
}
err = cfg.Execute()
if err != nil {
return nil, err
}
res.SetPageConfig(cfg, raw)
return res, nil
}

// Gets a list of audit logs for an account. Can be filtered by who made the
// change, on which zone, and the timeframe of the change.
func (r *AuditLogService) ListAutoPaging(ctx context.Context, params AuditLogListParams, opts ...option.RequestOption) *shared.V4PagePaginationArrayAutoPager[AuditLogListResponse] {
return shared.NewV4PagePaginationArrayAutoPager(r.List(ctx, params, opts...))
}

type AuditLogListResponse struct {
// A string that uniquely identifies the audit log.
ID string `json:"id"`
Action AuditLogListResponseAction `json:"action"`
Actor AuditLogListResponseActor `json:"actor"`
// The source of the event.
Interface string `json:"interface"`
// An object which can lend more context to the action being logged. This is a
// flexible value and varies between different actions.
Metadata interface{} `json:"metadata"`
// The new value of the resource that was modified.
NewValue string `json:"newValue"`
// The value of the resource before it was modified.
OldValue string `json:"oldValue"`
Owner AuditLogListResponseOwner `json:"owner"`
Resource AuditLogListResponseResource `json:"resource"`
// A UTC RFC3339 timestamp that specifies when the action being logged occured.
When time.Time `json:"when" format:"date-time"`
JSON auditLogListResponseJSON `json:"-"`
}

// auditLogListResponseJSON contains the JSON metadata for the struct
// [AuditLogListResponse]
type auditLogListResponseJSON struct {
ID apijson.Field
Action apijson.Field
Actor apijson.Field
Interface apijson.Field
Metadata apijson.Field
NewValue apijson.Field
OldValue apijson.Field
Owner apijson.Field
Resource apijson.Field
When apijson.Field
raw string
ExtraFields map[string]apijson.Field
}

func (r *AuditLogListResponse) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}

func (r auditLogListResponseJSON) RawJSON() string {
return r.raw
}

type AuditLogListResponseAction struct {
// A boolean that indicates if the action attempted was successful.
Result bool `json:"result"`
// A short string that describes the action that was performed.
Type string `json:"type"`
JSON auditLogListResponseActionJSON `json:"-"`
}

// auditLogListResponseActionJSON contains the JSON metadata for the struct
// [AuditLogListResponseAction]
type auditLogListResponseActionJSON struct {
Result apijson.Field
Type apijson.Field
raw string
ExtraFields map[string]apijson.Field
}

func (r *AuditLogListResponseAction) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}

func (r auditLogListResponseActionJSON) RawJSON() string {
return r.raw
}

type AuditLogListResponseActor struct {
// The ID of the actor that performed the action. If a user performed the action,
// this will be their User ID.
ID string `json:"id"`
// The email of the user that performed the action.
Email string `json:"email" format:"email"`
// The IP address of the request that performed the action.
IP string `json:"ip"`
// The type of actor, whether a User, Cloudflare Admin, or an Automated System.
Type AuditLogListResponseActorType `json:"type"`
JSON auditLogListResponseActorJSON `json:"-"`
}

// auditLogListResponseActorJSON contains the JSON metadata for the struct
// [AuditLogListResponseActor]
type auditLogListResponseActorJSON struct {
ID apijson.Field
Email apijson.Field
IP apijson.Field
Type apijson.Field
raw string
ExtraFields map[string]apijson.Field
}

func (r *AuditLogListResponseActor) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}

func (r auditLogListResponseActorJSON) RawJSON() string {
return r.raw
}

// The type of actor, whether a User, Cloudflare Admin, or an Automated System.
type AuditLogListResponseActorType string

const (
AuditLogListResponseActorTypeUser AuditLogListResponseActorType = "user"
AuditLogListResponseActorTypeAdmin AuditLogListResponseActorType = "admin"
AuditLogListResponseActorTypeCloudflare AuditLogListResponseActorType = "Cloudflare"
)

type AuditLogListResponseOwner struct {
// Identifier
ID string `json:"id"`
JSON auditLogListResponseOwnerJSON `json:"-"`
}

// auditLogListResponseOwnerJSON contains the JSON metadata for the struct
// [AuditLogListResponseOwner]
type auditLogListResponseOwnerJSON struct {
ID apijson.Field
raw string
ExtraFields map[string]apijson.Field
}

func (r *AuditLogListResponseOwner) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}

func (r auditLogListResponseOwnerJSON) RawJSON() string {
return r.raw
}

type AuditLogListResponseResource struct {
// An identifier for the resource that was affected by the action.
ID string `json:"id"`
// A short string that describes the resource that was affected by the action.
Type string `json:"type"`
JSON auditLogListResponseResourceJSON `json:"-"`
}

// auditLogListResponseResourceJSON contains the JSON metadata for the struct
// [AuditLogListResponseResource]
type auditLogListResponseResourceJSON struct {
ID apijson.Field
Type apijson.Field
raw string
ExtraFields map[string]apijson.Field
}

func (r *AuditLogListResponseResource) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}

func (r auditLogListResponseResourceJSON) RawJSON() string {
return r.raw
}

type AuditLogListParams struct {
// Identifier
AccountID param.Field[string] `path:"account_id,required"`
// Finds a specific log by its ID.
ID param.Field[string] `query:"id"`
Action param.Field[AuditLogListParamsAction] `query:"action"`
Actor param.Field[AuditLogListParamsActor] `query:"actor"`
// Limits the returned results to logs older than the specified date. This can be a
// date string `2019-04-30` or an absolute timestamp that conforms to RFC3339.
Before param.Field[time.Time] `query:"before" format:"date-time"`
// Changes the direction of the chronological sorting.
Direction param.Field[AuditLogListParamsDirection] `query:"direction"`
// Indicates that this request is an export of logs in CSV format.
Export param.Field[bool] `query:"export"`
// Indicates whether or not to hide user level audit logs.
HideUserLogs param.Field[bool] `query:"hide_user_logs"`
// Defines which page of results to return.
Page param.Field[float64] `query:"page"`
// Sets the number of results to return per page.
PerPage param.Field[float64] `query:"per_page"`
// Limits the returned results to logs newer than the specified date. This can be a
// date string `2019-04-30` or an absolute timestamp that conforms to RFC3339.
Since param.Field[time.Time] `query:"since" format:"date-time"`
Zone param.Field[AuditLogListParamsZone] `query:"zone"`
}

// URLQuery serializes [AuditLogListParams]'s query parameters as `url.Values`.
func (r AuditLogListParams) URLQuery() (v url.Values) {
return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
ArrayFormat: apiquery.ArrayQueryFormatComma,
NestedFormat: apiquery.NestedQueryFormatBrackets,
})
}

type AuditLogListParamsAction struct {
// Filters by the action type.
Type param.Field[string] `query:"type"`
}

// URLQuery serializes [AuditLogListParamsAction]'s query parameters as
// `url.Values`.
func (r AuditLogListParamsAction) URLQuery() (v url.Values) {
return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
ArrayFormat: apiquery.ArrayQueryFormatComma,
NestedFormat: apiquery.NestedQueryFormatBrackets,
})
}

type AuditLogListParamsActor struct {
// Filters by the email address of the actor that made the change.
Email param.Field[string] `query:"email" format:"email"`
// Filters by the IP address of the request that made the change by specific IP
// address or valid CIDR Range.
IP param.Field[string] `query:"ip"`
}

// URLQuery serializes [AuditLogListParamsActor]'s query parameters as
// `url.Values`.
func (r AuditLogListParamsActor) URLQuery() (v url.Values) {
return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
ArrayFormat: apiquery.ArrayQueryFormatComma,
NestedFormat: apiquery.NestedQueryFormatBrackets,
})
}

// Changes the direction of the chronological sorting.
type AuditLogListParamsDirection string

const (
AuditLogListParamsDirectionDesc AuditLogListParamsDirection = "desc"
AuditLogListParamsDirectionAsc AuditLogListParamsDirection = "asc"
)

type AuditLogListParamsZone struct {
// Filters by the name of the zone associated to the change.
Name param.Field[string] `query:"name"`
}

// URLQuery serializes [AuditLogListParamsZone]'s query parameters as `url.Values`.
func (r AuditLogListParamsZone) URLQuery() (v url.Values) {
return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
ArrayFormat: apiquery.ArrayQueryFormatComma,
NestedFormat: apiquery.NestedQueryFormatBrackets,
})
}
60 changes: 60 additions & 0 deletions audit_logs/auditlog_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// File generated from our OpenAPI spec by Stainless.

package audit_logs_test

import (
"context"
"errors"
"os"
"testing"
"time"

"github.com/cloudflare/cloudflare-go/v2"
"github.com/cloudflare/cloudflare-go/v2/audit_logs"
"github.com/cloudflare/cloudflare-go/v2/internal/testutil"
"github.com/cloudflare/cloudflare-go/v2/option"
)

func TestAuditLogListWithOptionalParams(t *testing.T) {
t.Skip("skipped: tests are disabled for the time being")
baseURL := "http://localhost:4010"
if envURL, ok := os.LookupEnv("TEST_API_BASE_URL"); ok {
baseURL = envURL
}
if !testutil.CheckTestServer(t, baseURL) {
return
}
client := cloudflare.NewClient(
option.WithBaseURL(baseURL),
option.WithAPIKey("144c9defac04969c7bfad8efaa8ea194"),
option.WithAPIEmail("user@example.com"),
)
_, err := client.AuditLogs.List(context.TODO(), audit_logs.AuditLogListParams{
AccountID: cloudflare.F("023e105f4ecef8ad9ca31a8372d0c353"),
ID: cloudflare.F("f174be97-19b1-40d6-954d-70cd5fbd52db"),
Action: cloudflare.F(audit_logs.AuditLogListParamsAction{
Type: cloudflare.F("add"),
}),
Actor: cloudflare.F(audit_logs.AuditLogListParamsActor{
IP: cloudflare.F("17.168.228.63"),
Email: cloudflare.F("alice@example.com"),
}),
Before: cloudflare.F(time.Now()),
Direction: cloudflare.F(audit_logs.AuditLogListParamsDirectionDesc),
Export: cloudflare.F(true),
HideUserLogs: cloudflare.F(true),
Page: cloudflare.F(50.000000),
PerPage: cloudflare.F(25.000000),
Since: cloudflare.F(time.Now()),
Zone: cloudflare.F(audit_logs.AuditLogListParamsZone{
Name: cloudflare.F("example.com"),
}),
})
if err != nil {
var apierr *cloudflare.Error
if errors.As(err, &apierr) {
t.Log(string(apierr.DumpRequest(true)))
}
t.Fatalf("err should be nil: %s", err.Error())
}
}
Loading

0 comments on commit a71d2c8

Please sign in to comment.