jacobbednarz / cloudflare-go-experimental

An experimental fork of cloudflare-go, don't use

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

cloudflare-go-experimental

An experimental fork of the cloudflare-go library. Not ready to be used.

Improvements

Consistent CRUD method signatures

Majority of entities follow a standard method signature.

  • Get(ctx, id): fetches a single entity by an identifer
  • List(ctx, ...params): fetches all entities and automatically paginates
  • Create(ctx, ...params): creates a new entity with the provided parameters
  • Update(ctx, id, ...params): updates an existing entity
  • Delete(ctx, id): deletes a single entity

Nested methods and services

Not all methods are defined at the top level. Instead, they are nested under service objects.

// old
client.ListZones(...)
client.ZoneLevelAccessServiceTokens(...)

// new
client.Zones.List()
client.Access.ServiceTokens(...)

This avoids polluting the global namespace and having more specific methods for services.

Examples

A zone is used below for the examples however, all entites will implement the same methods and interfaces.

initialising a new client with options like your own http.Client

params := cloudflare.ClientParams{
  Key: "3bc3be114fb6323adc5b0ad7422d193a",
  Email: "someone@example.com",
  HTTPClient: myCustomHTTPClient,
  // ...
}
c, err := cloudflare.New(params)

create a new zone

params := cloudflare.ClientParams{
  Key: "3bc3be114fb6323adc5b0ad7422d193a",
  Email: "someone@example.com",
}
c, err := cloudflare.New(params)

zParams := &cloudflare.ZoneParams{
  Name: "example.com",
  AccountID: "d8e8fca2dc0f896fd7cb4cb0031ba249"
}
z, _ := c.Zones.New(ctx, zParams)

fetching a known zone ID

params := cloudflare.ClientParams{
  Key: "3bc3be114fb6323adc5b0ad7422d193a",
  Email: "someone@example.com",
}
c, err := cloudflare.New(params)

z, _ := c.Zones.Get(ctx, "3e7705498e8be60520841409ebc69bc1")

fetching all zones matching a single account ID

params := cloudflare.ClientParams{
  Key: "3bc3be114fb6323adc5b0ad7422d193a",
  Email: "someone@example.com",
}
c, err := cloudflare.New(params)

zParams := &cloudflare.ZoneParams{
  AccountID: "d8e8fca2dc0f896fd7cb4cb0031ba249"
}
z, _ := c.Zones.List(ctx, zParams)

update a zone

params := cloudflare.ClientParams{
  Key: "3bc3be114fb6323adc5b0ad7422d193a",
  Email: "someone@example.com",
}
c, err := cloudflare.New(params)

zParams := &cloudflare.ZoneParams{
  Nameservers: cloudflare.StringSlice([]string{
    "ns1.example.com",
    "ns2.example.com"
  })
}
z, _ := c.Zones.Update(ctx, "b5163cf270a3fbac34827c4a2713eef4", zParams)

delete a zone

params := cloudflare.ClientParams{
  Key: "3bc3be114fb6323adc5b0ad7422d193a",
  Email: "someone@example.com",
}
c, err := cloudflare.New(params)

z, _ := c.Zones.Delete(ctx, "b5163cf270a3fbac34827c4a2713eef4")

About

An experimental fork of cloudflare-go, don't use


Languages

Language:Go 100.0%