github.com/grokify/go-ringcentral-client@v0.3.31/engagedigital/v1/examples/category_create_update_delete/main.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"log"
     7  	"strings"
     8  
     9  	"github.com/antihax/optional"
    10  	"github.com/grokify/mogo/fmt/fmtutil"
    11  	"github.com/jessevdk/go-flags"
    12  
    13  	engagedigital "github.com/grokify/go-ringcentral-client/engagedigital/v1/client"
    14  	utils "github.com/grokify/go-ringcentral-client/engagedigital/v1/util"
    15  )
    16  
    17  type options struct {
    18  	Site   string `short:"s" long:"site" description:"A site" required:"true"`
    19  	Token  string `short:"t" long:"token" description:"A token" required:"true"`
    20  	Action string `short:"a" long:"action" description:"A action (create|update|delete)" required:"true"`
    21  	Name   string `short:"n" long:"name" description:"A tag name" required:"false"`
    22  	Id     string `short:"i" long:"id" description:"A tag id" required:"false"`
    23  }
    24  
    25  func main() {
    26  	opts := options{}
    27  	_, err := flags.Parse(&opts)
    28  	if err != nil {
    29  		log.Fatal(err)
    30  	}
    31  
    32  	client := utils.NewApiClient(opts.Site, opts.Token)
    33  
    34  	switch opts.Action {
    35  	case "create":
    36  		fmt.Println("I_CREATING_CATEGORY")
    37  		opts.Name = strings.TrimSpace(opts.Name)
    38  		if len(opts.Name) == 0 {
    39  			log.Fatal("E_CREATE_NO_CATEGORY_NAME")
    40  		}
    41  		apiOpts := &engagedigital.CreateCategoryOpts{
    42  			Name: optional.NewString(opts.Name)}
    43  		info, resp, err := client.CategoriesApi.CreateCategory(
    44  			context.Background(),
    45  			apiOpts)
    46  		if err != nil {
    47  			log.Fatal(err)
    48  		} else if resp.StatusCode != 200 {
    49  			log.Fatal(resp.StatusCode)
    50  		}
    51  		fmtutil.PrintJSON(info)
    52  	case "update":
    53  		opts.Id = strings.TrimSpace(opts.Id)
    54  
    55  		info, resp, err := client.CategoriesApi.GetCategory(context.Background(), opts.Id)
    56  		if err != nil {
    57  			log.Fatal(err)
    58  		} else if resp.StatusCode != 200 {
    59  			log.Fatal(resp.StatusCode)
    60  		}
    61  		apiOpts := &engagedigital.UpdateCategoryOpts{
    62  			Name:         optional.NewString(opts.Name),
    63  			Unselectable: optional.NewBool(false)}
    64  		if !info.Unselectable {
    65  			apiOpts.Unselectable = optional.NewBool(true)
    66  		}
    67  		info, resp, err = client.CategoriesApi.UpdateCategory(
    68  			context.Background(), opts.Id, apiOpts)
    69  		if err != nil {
    70  			log.Fatal(err)
    71  		} else if resp.StatusCode != 200 {
    72  			log.Fatal(resp.StatusCode)
    73  		}
    74  		fmtutil.PrintJSON(info)
    75  	case "delete":
    76  		opts.Id = strings.TrimSpace(opts.Id)
    77  		if len(opts.Id) == 0 {
    78  			log.Fatal("E_DELETE_NO_CATEGORY_ID")
    79  		}
    80  		info, resp, err := client.CategoriesApi.DeleteCategory(
    81  			context.Background(), opts.Id, nil)
    82  		if err != nil {
    83  			log.Fatal(err)
    84  		} else if resp.StatusCode != 200 {
    85  			log.Fatal(resp.StatusCode)
    86  		}
    87  		fmtutil.PrintJSON(info)
    88  	}
    89  
    90  	fmt.Println("DONE")
    91  }