github.com/schmorrison/Zoho@v1.1.4/recruit/tags.go (about)

     1  package recruit
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	zoho "github.com/schmorrison/Zoho"
     8  )
     9  
    10  // https://www.zoho.com/recruit/developer-guide/apiv2/create-tag.html
    11  func (c *API) CreateTags(
    12  	request CreateTagsRequest,
    13  	params map[string]zoho.Parameter,
    14  ) (data CreateTagsResponse, err error) {
    15  	endpoint := zoho.Endpoint{
    16  		Name:         "CreateTags",
    17  		URL:          fmt.Sprintf("https://recruit.zoho.%s/recruit/v2/settings/tags", c.ZohoTLD),
    18  		Method:       zoho.HTTPPost,
    19  		ResponseData: &CreateTagsResponse{},
    20  		RequestBody:  request,
    21  		BodyFormat:   zoho.JSON,
    22  		URLParameters: map[string]zoho.Parameter{
    23  			"module": "", // mandatory
    24  		},
    25  	}
    26  
    27  	if len(params) > 0 {
    28  		for k, v := range params {
    29  			endpoint.URLParameters[k] = v
    30  		}
    31  	}
    32  
    33  	// pp.Printf("%s\n", endpoint)
    34  
    35  	err = c.Zoho.HTTPRequest(&endpoint)
    36  	if err != nil {
    37  		return CreateTagsResponse{}, fmt.Errorf("failed to create Tag(s): %s", err)
    38  	}
    39  
    40  	if v, ok := endpoint.ResponseData.(*CreateTagsResponse); ok {
    41  		for _, resp := range v.Tags {
    42  			if resp.Code != "SUCCESS" {
    43  				return CreateTagsResponse{}, fmt.Errorf(
    44  					"failed to create Tag(s): %s: %s",
    45  					resp.Code,
    46  					resp.Message,
    47  				)
    48  			}
    49  		}
    50  		return *v, nil
    51  	}
    52  
    53  	return CreateTagsResponse{}, fmt.Errorf("data returned was not 'CreateTagsResponse'")
    54  }
    55  
    56  type CreateTagsRequest struct {
    57  	Tags []Tags `json:"tags"`
    58  }
    59  type Tags struct {
    60  	Name string `json:"name"`
    61  }
    62  
    63  type CreateTagsResponse struct {
    64  	Tags []struct {
    65  		Code    string `json:"code"`
    66  		Details struct {
    67  			CreatedTime  time.Time `json:"created_time"`
    68  			ModifiedTime time.Time `json:"modified_time"`
    69  			ModifiedBy   struct {
    70  				Name string `json:"name"`
    71  				ID   string `json:"id"`
    72  			} `json:"modified_by"`
    73  			ID        string `json:"id"`
    74  			CreatedBy struct {
    75  				Name string `json:"name"`
    76  				ID   string `json:"id"`
    77  			} `json:"created_by"`
    78  		} `json:"details"`
    79  		Message string `json:"message"`
    80  		Status  string `json:"status"`
    81  	} `json:"tags"`
    82  }
    83  
    84  // https://www.zoho.com/recruit/developer-guide/apiv2/add-tags.html
    85  func (c *API) AddTagsToIDs(
    86  	module Module,
    87  	params map[string]zoho.Parameter,
    88  ) (data AddTagsResponse, err error) {
    89  	endpoint := zoho.Endpoint{
    90  		Name: "AddTagsToIDs",
    91  		URL: fmt.Sprintf(
    92  			"https://recruit.zoho.%s/recruit/v2/%s/actions/add_tags",
    93  			c.ZohoTLD,
    94  			module,
    95  		),
    96  		Method:       zoho.HTTPPost,
    97  		ResponseData: &AddTagsResponse{},
    98  		URLParameters: map[string]zoho.Parameter{
    99  			"tag_names": "",
   100  			"ids":       "",
   101  		},
   102  	}
   103  
   104  	if len(params) > 0 {
   105  		for k, v := range params {
   106  			endpoint.URLParameters[k] = v
   107  		}
   108  	}
   109  
   110  	err = c.Zoho.HTTPRequest(&endpoint)
   111  	if err != nil {
   112  		return AddTagsResponse{}, fmt.Errorf("failed to insert Tag(s): %s", err)
   113  	}
   114  
   115  	if v, ok := endpoint.ResponseData.(*AddTagsResponse); ok {
   116  		return *v, nil
   117  	}
   118  
   119  	return AddTagsResponse{}, fmt.Errorf("data returned was not 'AddTagsResponse'")
   120  }
   121  
   122  // https://www.zoho.com/recruit/developer-guide/apiv2/add-tags.html
   123  func (c *API) AddTagsToId(
   124  	module Module,
   125  	ID string,
   126  	params map[string]zoho.Parameter,
   127  ) (data AddTagsResponse, err error) {
   128  	endpoint := zoho.Endpoint{
   129  		Name: "AddTagsToId",
   130  		URL: fmt.Sprintf(
   131  			"https://recruit.zoho.%s/recruit/v2/%s/%s/actions/add_tags",
   132  			c.ZohoTLD,
   133  			module,
   134  			ID,
   135  		),
   136  		Method:       zoho.HTTPPost,
   137  		ResponseData: &AddTagsResponse{},
   138  		URLParameters: map[string]zoho.Parameter{
   139  			"tag_names": "",
   140  		},
   141  	}
   142  
   143  	if len(params) > 0 {
   144  		for k, v := range params {
   145  			endpoint.URLParameters[k] = v
   146  		}
   147  	}
   148  
   149  	err = c.Zoho.HTTPRequest(&endpoint)
   150  	if err != nil {
   151  		return AddTagsResponse{}, fmt.Errorf("failed to add Tag(s): %s", err)
   152  	}
   153  
   154  	if v, ok := endpoint.ResponseData.(*AddTagsResponse); ok {
   155  		for _, resp := range v.Data {
   156  			if resp.Code != "SUCCESS" {
   157  				return AddTagsResponse{}, fmt.Errorf(
   158  					"failed to add Tag(s): %s: %s",
   159  					resp.Code,
   160  					resp.Message,
   161  				)
   162  			}
   163  		}
   164  		return *v, nil
   165  	}
   166  
   167  	return AddTagsResponse{}, fmt.Errorf("data returned was not 'AddTagsResponse'")
   168  }
   169  
   170  type AddTagsResponse struct {
   171  	Data []struct {
   172  		Code    string `json:"code"`
   173  		Details struct {
   174  			ID   int64    `json:"id"`
   175  			Tags []string `json:"tags"`
   176  		} `json:"details"`
   177  		Message string `json:"message"`
   178  		Status  string `json:"status"`
   179  	} `json:"data"`
   180  }
   181  
   182  // https://www.zoho.com/recruit/developer-guide/apiv2/delete-tag.html
   183  func (c *API) DeleteTagById(tagID string) (data DeleteTagResponse, err error) {
   184  	if len(tagID) == 0 {
   185  		return DeleteTagResponse{}, fmt.Errorf("failed to delete Tag, must provide tagID")
   186  	}
   187  
   188  	endpoint := zoho.Endpoint{
   189  		Name: "DeleteTagById",
   190  		URL: fmt.Sprintf(
   191  			"https://recruit.zoho.%s/recruit/v2/settings/tags/%s",
   192  			c.ZohoTLD,
   193  			tagID,
   194  		),
   195  		Method:       zoho.HTTPDelete,
   196  		ResponseData: &DeleteTagResponse{},
   197  	}
   198  
   199  	err = c.Zoho.HTTPRequest(&endpoint)
   200  	if err != nil {
   201  		return DeleteTagResponse{}, fmt.Errorf("failed to delete Tag: %s", err)
   202  	}
   203  
   204  	if v, ok := endpoint.ResponseData.(*DeleteTagResponse); ok {
   205  		return *v, nil
   206  	}
   207  
   208  	return DeleteTagResponse{}, fmt.Errorf("data retrieved was not 'DeleteTagResponse'")
   209  }
   210  
   211  type DeleteTagResponse struct {
   212  	Tags struct {
   213  		Code    string `json:"code"`
   214  		Details struct {
   215  			ID int64 `json:"id"`
   216  		} `json:"details"`
   217  		Message string `json:"message"`
   218  		Status  string `json:"status"`
   219  	} `json:"tags"`
   220  }
   221  
   222  // https://www.zoho.com/recruit/developer-guide/apiv2/get-tag-list.html
   223  func (c *API) GetTagsList(
   224  	module Module,
   225  	params map[string]zoho.Parameter,
   226  ) (data TagsListResponse, err error) {
   227  	if len(module) == 0 {
   228  		return TagsListResponse{}, fmt.Errorf("failed to list Tags, module name is missing")
   229  	}
   230  	endpoint := zoho.Endpoint{
   231  		Name:         "GetTagsList",
   232  		URL:          fmt.Sprintf("https://recruit.zoho.%s/recruit/v2/settings/tags", c.ZohoTLD),
   233  		Method:       zoho.HTTPGet,
   234  		ResponseData: &TagsListResponse{},
   235  		URLParameters: map[string]zoho.Parameter{
   236  			"module":  zoho.Parameter(module), // mandatory
   237  			"my_tags": "",
   238  		},
   239  	}
   240  
   241  	if len(params) > 0 {
   242  		for k, v := range params {
   243  			endpoint.URLParameters[k] = v
   244  		}
   245  	}
   246  
   247  	// log.Printf("endpoint: %+v", endpoint)
   248  
   249  	err = c.Zoho.HTTPRequest(&endpoint)
   250  	if err != nil {
   251  		return TagsListResponse{}, fmt.Errorf(
   252  			"failed to retrieve %s TagsList: %s",
   253  			params["module"],
   254  			err,
   255  		)
   256  	}
   257  
   258  	if v, ok := endpoint.ResponseData.(*TagsListResponse); ok {
   259  		return *v, nil
   260  	}
   261  
   262  	return TagsListResponse{}, fmt.Errorf("data returned was not 'TagsListResponse'")
   263  }
   264  
   265  type TagsListResponse struct {
   266  	Data struct {
   267  		Tags []struct {
   268  			CreatedTime  time.Time `json:"created_time"`
   269  			ModifiedTime time.Time `json:"modified_time"`
   270  			ModifiedBy   struct {
   271  				Name string `json:"name"`
   272  				ID   string `json:"id"`
   273  			} `json:"modified_by"`
   274  			Name      string `json:"name"`
   275  			ID        string `json:"id"`
   276  			CreatedBy struct {
   277  				Name string `json:"name"`
   278  				ID   string `json:"id"`
   279  			} `json:"created_by"`
   280  		} `json:"tags"`
   281  		Info PageInfo `json:"info"`
   282  	} `json:"data"`
   283  }
   284  
   285  // https://www.zoho.com/recruit/developer-guide/apiv2/update-tags.html
   286  func (c *API) UpdateTag(ID string, request UpdateTagRequest) (data UpdateTagResponse, err error) {
   287  	endpoint := zoho.Endpoint{
   288  		Name: "UpdateTag",
   289  		URL: fmt.Sprintf(
   290  			"https://recruit.zoho.%s/recruit/v2/settings/tags/%s",
   291  			c.ZohoTLD,
   292  			ID,
   293  		),
   294  		Method:       zoho.HTTPPut,
   295  		ResponseData: &UpdateTagResponse{},
   296  		RequestBody:  request,
   297  		BodyFormat:   zoho.JSON,
   298  	}
   299  
   300  	err = c.Zoho.HTTPRequest(&endpoint)
   301  	if err != nil {
   302  		return UpdateTagResponse{}, fmt.Errorf("failed to update Tag: %s", err)
   303  	}
   304  
   305  	if v, ok := endpoint.ResponseData.(*UpdateTagResponse); ok {
   306  		return *v, nil
   307  	}
   308  
   309  	return UpdateTagResponse{}, fmt.Errorf("data returned was not 'UpdateTagResponse'")
   310  }
   311  
   312  type UpdateTagRequest struct {
   313  	Tags []struct {
   314  		Name string `json:"name"`
   315  	} `json:"tags"`
   316  }
   317  type UpdateTagResponse struct {
   318  	Tags []struct {
   319  		Code    string `json:"code"`
   320  		Details struct {
   321  			CreatedTime  time.Time `json:"created_time"`
   322  			ModifiedTime time.Time `json:"modified_time"`
   323  			ModifiedBy   struct {
   324  				Name string `json:"name"`
   325  				ID   string `json:"id"`
   326  			} `json:"modified_by"`
   327  			Name      string `json:"name"`
   328  			ID        int64  `json:"id"`
   329  			CreatedBy struct {
   330  				Name string `json:"name"`
   331  				ID   string `json:"id"`
   332  			} `json:"created_by"`
   333  		} `json:"details"`
   334  		Message string `json:"message"`
   335  		Status  string `json:"status"`
   336  	} `json:"tags"`
   337  }
   338  
   339  // https://www.zoho.com/recruit/developer-guide/apiv2/remove-tags.html
   340  func (c *API) RemoveTagsFromIDs(
   341  	module Module,
   342  	params map[string]zoho.Parameter,
   343  ) (data RemoveTagsResponse, err error) {
   344  	endpoint := zoho.Endpoint{
   345  		Name: "RemoveTagsFromIDs",
   346  		URL: fmt.Sprintf(
   347  			"https://recruit.zoho.%s/recruit/v2/%s/actions/remove_tags",
   348  			c.ZohoTLD,
   349  			module,
   350  		),
   351  		Method:       zoho.HTTPPost,
   352  		ResponseData: &RemoveTagsResponse{},
   353  		URLParameters: map[string]zoho.Parameter{
   354  			"tag_names": "",
   355  			"ids":       "",
   356  		},
   357  	}
   358  
   359  	if len(params) > 0 {
   360  		for k, v := range params {
   361  			endpoint.URLParameters[k] = v
   362  		}
   363  	}
   364  
   365  	err = c.Zoho.HTTPRequest(&endpoint)
   366  	if err != nil {
   367  		return RemoveTagsResponse{}, fmt.Errorf("failed to insert Tag(s): %s", err)
   368  	}
   369  
   370  	if v, ok := endpoint.ResponseData.(*RemoveTagsResponse); ok {
   371  		return *v, nil
   372  	}
   373  
   374  	return RemoveTagsResponse{}, fmt.Errorf("data returned was not 'RemoveTagsResponse'")
   375  }
   376  
   377  // https://www.zoho.com/recruit/developer-guide/apiv2/remove-tags.html
   378  func (c *API) RemoveTagsFromId(
   379  	module Module,
   380  	ID string,
   381  	params map[string]zoho.Parameter,
   382  ) (data RemoveTagsResponse, err error) {
   383  	endpoint := zoho.Endpoint{
   384  		Name: "RemoveTagsFromId",
   385  		URL: fmt.Sprintf(
   386  			"https://recruit.zoho.%s/recruit/v2/%s/%s/actions/remove_tags",
   387  			c.ZohoTLD,
   388  			module,
   389  			ID,
   390  		),
   391  		Method:       zoho.HTTPPost,
   392  		ResponseData: &RemoveTagsResponse{},
   393  		URLParameters: map[string]zoho.Parameter{
   394  			"tag_names": "",
   395  		},
   396  	}
   397  
   398  	if len(params) > 0 {
   399  		for k, v := range params {
   400  			endpoint.URLParameters[k] = v
   401  		}
   402  	}
   403  
   404  	err = c.Zoho.HTTPRequest(&endpoint)
   405  	if err != nil {
   406  		return RemoveTagsResponse{}, fmt.Errorf("failed to remove Tag(s): %s", err)
   407  	}
   408  
   409  	if v, ok := endpoint.ResponseData.(*RemoveTagsResponse); ok {
   410  		for _, resp := range v.Data {
   411  			if resp.Code != "SUCCESS" {
   412  				return RemoveTagsResponse{}, fmt.Errorf(
   413  					"failed to remove Tag(s): %s: %s",
   414  					resp.Code,
   415  					resp.Message,
   416  				)
   417  			}
   418  		}
   419  		return *v, nil
   420  	}
   421  
   422  	return RemoveTagsResponse{}, fmt.Errorf("data returned was not 'RemoveTagsResponse'")
   423  }
   424  
   425  type RemoveTagsResponse struct {
   426  	Data []struct {
   427  		Code    string `json:"code"`
   428  		Details struct {
   429  			ID   int64    `json:"id"`
   430  			Tags []string `json:"tags"`
   431  		} `json:"details"`
   432  		Message string `json:"message"`
   433  		Status  string `json:"status"`
   434  	} `json:"data"`
   435  }