contact.go raw

   1  package goinwx
   2  
   3  import (
   4  	"github.com/fatih/structs"
   5  	"github.com/go-viper/mapstructure/v2"
   6  )
   7  
   8  const (
   9  	methodContactInfo   = "contact.info"
  10  	methodContactList   = "contact.list"
  11  	methodContactCreate = "contact.create"
  12  	methodContactDelete = "contact.delete"
  13  	methodContactUpdate = "contact.update"
  14  )
  15  
  16  // ContactService API access to Contact.
  17  type ContactService service
  18  
  19  // Create Creates a contact.
  20  func (s *ContactService) Create(request *ContactCreateRequest) (int, error) {
  21  	req := s.client.NewRequest(methodContactCreate, structs.Map(request))
  22  
  23  	resp, err := s.client.Do(req)
  24  	if err != nil {
  25  		return 0, err
  26  	}
  27  
  28  	result := make(map[string]int)
  29  
  30  	err = mapstructure.Decode(resp, &result)
  31  	if err != nil {
  32  		return 0, err
  33  	}
  34  
  35  	return result["id"], nil
  36  }
  37  
  38  // Delete Deletes a contact.
  39  func (s *ContactService) Delete(roID int) error {
  40  	req := s.client.NewRequest(methodContactDelete, map[string]any{
  41  		"id": roID,
  42  	})
  43  
  44  	_, err := s.client.Do(req)
  45  
  46  	return err
  47  }
  48  
  49  // Update Updates a contact.
  50  func (s *ContactService) Update(request *ContactUpdateRequest) error {
  51  	req := s.client.NewRequest(methodContactUpdate, structs.Map(request))
  52  
  53  	_, err := s.client.Do(req)
  54  
  55  	return err
  56  }
  57  
  58  // Info Get information about a contact.
  59  func (s *ContactService) Info(contactID int) (*ContactInfoResponse, error) {
  60  	requestMap := make(map[string]any)
  61  	requestMap["wide"] = 1
  62  
  63  	if contactID != 0 {
  64  		requestMap["id"] = contactID
  65  	}
  66  
  67  	req := s.client.NewRequest(methodContactInfo, requestMap)
  68  
  69  	resp, err := s.client.Do(req)
  70  	if err != nil {
  71  		return nil, err
  72  	}
  73  
  74  	result := ContactInfoResponse{}
  75  
  76  	err = mapstructure.Decode(resp, &result)
  77  	if err != nil {
  78  		return nil, err
  79  	}
  80  
  81  	return &result, nil
  82  }
  83  
  84  // List Search contacts.
  85  func (s *ContactService) List(search string) (*ContactListResponse, error) {
  86  	requestMap := make(map[string]any)
  87  
  88  	if search != "" {
  89  		requestMap["search"] = search
  90  	}
  91  
  92  	req := s.client.NewRequest(methodContactList, requestMap)
  93  
  94  	resp, err := s.client.Do(req)
  95  	if err != nil {
  96  		return nil, err
  97  	}
  98  
  99  	result := ContactListResponse{}
 100  
 101  	err = mapstructure.Decode(resp, &result)
 102  	if err != nil {
 103  		return nil, err
 104  	}
 105  
 106  	return &result, nil
 107  }
 108  
 109  // ContactCreateRequest API model.
 110  type ContactCreateRequest struct {
 111  	Type          string `structs:"type"`
 112  	Name          string `structs:"name"`
 113  	Org           string `structs:"org,omitempty"`
 114  	Street        string `structs:"street"`
 115  	City          string `structs:"city"`
 116  	PostalCode    string `structs:"pc"`
 117  	StateProvince string `structs:"sp,omitempty"`
 118  	CountryCode   string `structs:"cc"`
 119  	Voice         string `structs:"voice"`
 120  	Fax           string `structs:"fax,omitempty"`
 121  	Email         string `structs:"email"`
 122  	Remarks       string `structs:"remarks,omitempty"`
 123  	Protection    bool   `structs:"protection,omitempty"`
 124  	Testing       bool   `structs:"testing,omitempty"`
 125  }
 126  
 127  // ContactUpdateRequest API model.
 128  type ContactUpdateRequest struct {
 129  	ID            int    `structs:"id"`
 130  	Name          string `structs:"name,omitempty"`
 131  	Org           string `structs:"org,omitempty"`
 132  	Street        string `structs:"street,omitempty"`
 133  	City          string `structs:"city,omitempty"`
 134  	PostalCode    string `structs:"pc,omitempty"`
 135  	StateProvince string `structs:"sp,omitempty"`
 136  	CountryCode   string `structs:"cc,omitempty"`
 137  	Voice         string `structs:"voice,omitempty"`
 138  	Fax           string `structs:"fax,omitempty"`
 139  	Email         string `structs:"email,omitempty"`
 140  	Remarks       string `structs:"remarks,omitempty"`
 141  	Protection    bool   `structs:"protection,omitempty"`
 142  	Testing       bool   `structs:"testing,omitempty"`
 143  }
 144  
 145  // ContactInfoResponse API model.
 146  type ContactInfoResponse struct {
 147  	Contact Contact `mapstructure:"contact"`
 148  }
 149  
 150  // ContactListResponse API model.
 151  type ContactListResponse struct {
 152  	Count    int       `mapstructure:"count"`
 153  	Contacts []Contact `mapstructure:"contact"`
 154  }
 155