Skip to content

RewriteToday/golang

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Rewrite Go SDK

github.com/rewritetoday/golang, the official Go SDK for the Rewrite API.

It wraps authentication, typed REST calls, and resource helpers on top of the SDK REST and API layers.

Installation

Use your preferred Go workflow:

go get github.com/rewritetoday/golang

Quick Start

package main

import (
	"context"
	"fmt"
	"log"

	rewrite "github.com/rewritetoday/golang"
)

func main() {
	client, err := rewrite.New("rw_abc")

	if err != nil {
		log.Fatal(err)
	}

	hooks, err := client.Webhooks.List(
		context.Background(),
		"123456789012345678",
		&rewrite.RESTGetListWebhooksQueryParams{Limit: 10},
	)

	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("%+v\n", hooks)
}

Create The Client

You can pass the API key directly or use the full options object.

package main

import (
	"time"

	rewrite "github.com/rewritetoday/golang"
)

func buildClient() (*rewrite.Client, error) {
	return rewrite.New(rewrite.RewriteOptions{
		Secret: "rw_abc",
		Rest: &rewrite.RESTOptions{
			Timeout: 10 * time.Second,
			Headers: map[string]string{
				"x-trace-id": "my-service",
			},
			Retry: &rewrite.RetryOptions{
				Max: 3,
				Delay: func(attempt int) time.Duration {
					return time.Duration(attempt) * 250 * time.Millisecond
				},
			},
		},
	})
}

Templates

projectId := "123456789012345678"

created, err := client.Templates.Create(context.Background(), rewrite.CreateTemplateOptions{
	Project: projectId,
	RESTPostCreateTemplateBody: rewrite.RESTPostCreateTemplateBody{
		Name:    "welcome_sms",
		Content: "Hi {{name}}, welcome to {{company}}.",
		Variables: []rewrite.APITemplateVariable{
			{Name: "name", Fallback: "customer"},
			{Name: "company", Fallback: "Rewrite"},
		},
	},
})

if err != nil {
	log.Fatal(err)
}

templates, err := client.Templates.List(context.Background(), projectId, &rewrite.RESTGetListTemplatesQueryParams{Limit: 20})

if err != nil {
	log.Fatal(err)
}

fmt.Printf("created=%+v templates=%+v\n", created, templates)

Webhooks

projectId := "123456789012345678"

hook, err := client.Webhooks.Create(context.Background(), rewrite.CreateWebhookOptions{
	Project:  projectId,
	Name:     "delivery-events",
	Endpoint: "https://example.com/webhooks/rewrite",
	Events: []rewrite.WebhookEventType{
		rewrite.WebhookEventTypeSMSDelivered,
		rewrite.WebhookEventTypeSMSFailed,
	},
})

if err != nil {
	log.Fatal(err)
}

_, err = client.Webhooks.Update(context.Background(), string(hook.Data.ID), rewrite.UpdateWebhookOptions{
	Project: projectId,
	RESTPatchUpdateWebhookBody: rewrite.RESTPatchUpdateWebhookBody{
		Status: rewrite.WebhookStatusInactive,
	},
})

if err != nil {
	log.Fatal(err)
}

hooks, err := client.Webhooks.List(context.Background(), projectId, &rewrite.RESTGetListWebhooksQueryParams{Limit: 10})

if err != nil {
	log.Fatal(err)
}

fmt.Printf("%+v\n", hooks)

API Keys

projectId := "123456789012345678"

key, err := client.APIKeys.Create(context.Background(), rewrite.CreateAPIKeyOptions{
	Project: projectId,
	RESTPostCreateAPIKeyBody: rewrite.RESTPostCreateAPIKeyBody{
		Name: "backend-prod",
		Scopes: []rewrite.APIKeyScope{
			rewrite.APIKeyScopeReadProject,
			rewrite.APIKeyScopeReadTemplates,
		},
	},
})

if err != nil {
	log.Fatal(err)
}

fmt.Printf("%+v\n", key)

Error Handling

Requests run through the SDK REST client. HTTP failures can return HTTPError.

_, err := client.APIKeys.List(context.Background(), "invalid_id", nil)

if err != nil {
	var httpErr *rewrite.HTTPError

	if errors.As(err, &httpErr) {
		fmt.Println("HTTP Error:", httpErr.Status, httpErr.Method, httpErr.URL)
	}
}

Made with 🤍 by the Rewrite team.
SMS the way it should be.

About

Rewrite's official Golang SDK

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Sponsor this project

Packages

 
 
 

Contributors

Languages