Skip to content

Commit

Permalink
added notification rules,fix: fields in integrations
Browse files Browse the repository at this point in the history
Added error message for API call
Added contact method and notification rules module
  • Loading branch information
dheeruk12 committed Jul 28, 2022
1 parent 51f78d0 commit fcdcffc
Show file tree
Hide file tree
Showing 9 changed files with 189 additions and 20 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ go get github.com/Zenduty/zenduty-go-sdk
Before you begin making use of the SDK, make sure you have your Zenduty Access Token.

```
import "github.com/Zenduty/zenduty-go-sdk"
import "github.com/Zenduty/zenduty-go-sdk/client"
```
Configure the Token and Url

```
config := &client.Config{
Token: "", // enter token for authentication
BaseURL: "", // your url
}
```
Based on the service you want to communicate with,Create object for required class,For example, to create Team
Expand All @@ -32,21 +32,21 @@ package main
import (
"fmt"

"github.com/Zenduty/zenduty-go-sdk"
"github.com/Zenduty/zenduty-go-sdk/client"
)


func main() {
config := &client.Config{
Token: "", // enter token for authentication
Token: "",
}
c, err := client.NewClient(config)
if err != nil {
panic(err)
}

newteam := &client.Team{}
newteam.Name = "test"
newteam := &client.CreateTeams{}
newteam.Name = "demoteam"

resp, err := c.Teams.CreateTeam(newteam)
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ type Client struct {
Priority *PriorityService
Tags *TagsService
MaintenanceWindow *MaintenanceWindowService
NotificationRules *NotificationRulesService
ContactMethod *ContactMethodService
}

type Response struct {
Expand Down Expand Up @@ -81,6 +83,8 @@ func NewClient(config *Config) (*Client, error) {
c.Priority = &PriorityService{c}
c.Tags = &TagsService{c}
c.MaintenanceWindow = &MaintenanceWindowService{c}
c.NotificationRules = &NotificationRulesService{c}
c.ContactMethod = &ContactMethodService{c}

return c, nil

Expand Down Expand Up @@ -159,7 +163,7 @@ func (c *Client) decodeErrorResponse(res *Response) error {
v := &errorResponse{Error: &Error{ErrorResponse: res, Code: res.Response.StatusCode}}
if err := c.DecodeJSON(res, v); err != nil {

return fmt.Errorf("%s APIs call to %s failed: %v", res.Response.Request.Method, res.Response.Request.URL.String(), res.Response.Status)
return fmt.Errorf("%s APIs call to %s failed: %v error: %s", res.Response.Request.Method, res.Response.Request.URL.String(), res.Response.Status, string(res.BodyBytes))
}

return v.Error
Expand Down
44 changes: 44 additions & 0 deletions client/contact_method.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package client

import (
"encoding/json"
"fmt"
)

type ContactMethodService service

type ContactMethod struct {
UniqueID string `json:"unique_id"`
Name string `json:"name"`
ContactType int `json:"contact_type"`
Value string `json:"value"`
CreationDate string `json:"creation_date"`
}

func (c *ContactMethodService) GetContactMethods(username string) ([]ContactMethod, error) {
path := fmt.Sprintf("/api/account/users/%s/contacts/", username)
body, err := c.client.newRequestDo("GET", path, nil)
if err != nil {
return nil, err
}
var s []ContactMethod
err = json.Unmarshal(body.BodyBytes, &s)
if err != nil {
return nil, err
}
return s, nil
}

func (c *ContactMethodService) GetContactMethodByID(username string, contactMethodID string) (*ContactMethod, error) {
path := fmt.Sprintf("/api/account/users/%s/contacts/%s/", username, contactMethodID)
body, err := c.client.newRequestDo("GET", path, nil)
if err != nil {
return nil, err
}
var s1 ContactMethod
err = json.Unmarshal(body.BodyBytes, &s1)
if err != nil {
return nil, err
}
return &s1, nil
}
2 changes: 1 addition & 1 deletion client/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ type Error struct {
}

func (e *Error) Error() string {
return fmt.Sprintf("%s API call to %s failed %v. Errors: %s, Message: %s", e.ErrorResponse.Response.Request.Method, e.ErrorResponse.Response.Request.URL.String(), e.ErrorResponse.Response.Status, e.ErrorResponse.Response.Request.Body, e.Message)
return fmt.Sprintf("%s API call to %s failed %v. Errors: %s, Message: %s", e.ErrorResponse.Response.Request.Method, e.ErrorResponse.Response.Request.URL.String(), e.ErrorResponse.Response.Status, string(e.ErrorResponse.BodyBytes), e.Message)
}
2 changes: 1 addition & 1 deletion client/esp.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Rules struct {

type EscalationPolicy struct {
Name string `json:"name"`
Description string `json:"description"`
Description string `json:"description,omitempty"`
Summary string `json:"summary"`
Team string `json:"team"`
Unique_Id string `json:"unique_id"`
Expand Down
28 changes: 23 additions & 5 deletions client/integrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ type ApplicationReference struct {
}

type IntegrationCreate struct {
Name string `json:"name"`
Summary string `json:"summary"`
Application string `json:"application"`
Name string `json:"name"`
Summary string `json:"summary"`
Application string `json:"application"`
Is_Enabled bool `json:"is_enabled"`
Create_Incident_For int `json:"create_incidents_for"`
Default_Urgency int `json:"default_urgency"`
}

type Integration struct {
Expand All @@ -40,9 +43,9 @@ type Integration struct {
Webhook_url string `json:"webhook_url"`
Created_By string `json:"created_by"`
Is_Enabled bool `json:"is_enabled"`
Create_Incident_For int `json:"create_incident_for"`
Create_Incident_For int `json:"create_incidents_for"`
Integration_Type int `json:"integration_type"`
Default_Urgency int `json:"default_urggency"`
Default_Urgency int `json:"default_urgency"`
}

func (c *IntegrationServerice) CreateIntegration(team string, service_id string, integration *IntegrationCreate) (*Integration, error) {
Expand All @@ -60,6 +63,21 @@ func (c *IntegrationServerice) CreateIntegration(team string, service_id string,
return &i, nil
}

func (c *IntegrationServerice) UpdateIntegration(team string, service_id string, integration_id string, integration *IntegrationCreate) (*Integration, error) {
path := fmt.Sprintf("/api/account/teams/%s/services/%s/integrations/%s/", team, service_id, integration_id)

body, err := c.client.newRequestDo("PATCH", path, integration)
if err != nil {
return nil, err
}
var i Integration
err = json.Unmarshal(body.BodyBytes, &i)
if err != nil {
return nil, err
}
return &i, nil
}

func (c *IntegrationServerice) GetIntegrations(team, service_id string) ([]Integration, error) {
path := fmt.Sprintf("/api/account/teams/%s/services/%s/integrations/", team, service_id)
body, err := c.client.newRequestDo("GET", path, nil)
Expand Down
88 changes: 88 additions & 0 deletions client/notification_rules.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package client

import (
"encoding/json"
"fmt"
)

type NotificationRulesService service

type NotificationRules struct {
UniqueID string `json:"unique_id"`
StartDelay int `json:"start_delay"`
Type string `json:"type"`
Contact string `json:"contact"`
Urgency int `json:"urgency"`
}

type CreateNotificationRules struct {
Contact string `json:"contact"`
StartDelay int `json:"start_delay"`
Urgency int `json:"urgency"`
}

func (c *NotificationRulesService) CreateNotificationRules(username string, notificationRule *CreateNotificationRules) (*NotificationRules, error) {
path := fmt.Sprintf("/api/account/users/%s/notification_rules/", username)
body, err := c.client.newRequestDo("POST", path, notificationRule)
if err != nil {
return nil, err
}
var s NotificationRules
err = json.Unmarshal(body.BodyBytes, &s)
if err != nil {
return nil, err
}
return &s, nil
}

func (c *NotificationRulesService) GetNotificationRules(username string) ([]NotificationRules, error) {
path := fmt.Sprintf("/api/account/users/%s/notification_rules/", username)
body, err := c.client.newRequestDo("GET", path, nil)
if err != nil {
return nil, err
}
var s []NotificationRules
err = json.Unmarshal(body.BodyBytes, &s)
if err != nil {
return nil, err
}
return s, nil
}

func (c *NotificationRulesService) GetNotificationRulesByID(username, notificationRuleID string) (*NotificationRules, error) {
path := fmt.Sprintf("/api/account/users/%s/notification_rules/%s/", username, notificationRuleID)
body, err := c.client.newRequestDo("GET", path, nil)
if err != nil {
return nil, err
}
var s NotificationRules
err = json.Unmarshal(body.BodyBytes, &s)
if err != nil {
return nil, err
}
return &s, nil
}

func (c *NotificationRulesService) DeleteNotificationRules(username string, notificationRuleID string) error {
path := fmt.Sprintf("/api/account/users/%s/notification_rules/%s/", username, notificationRuleID)
_, err := c.client.newRequestDo("DELETE", path, nil)
if err != nil {
return err
}
return nil
}

func (c *NotificationRulesService) UpdateNotificationRules(username string, notificationRuleID string, notificationRule *NotificationRules) (*NotificationRules, error) {
path := fmt.Sprintf("/api/account/users/%s/notification_rules/%s/", username, notificationRuleID)
body, err := c.client.newRequestDo("PUT", path, notificationRule)
if err != nil {
return nil, err
}
var s NotificationRules
err = json.Unmarshal(body.BodyBytes, &s)
if err != nil {
return nil, err
}
return &s, nil

}
15 changes: 15 additions & 0 deletions client/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@ func (c *RoleService) GetRoles(team string) ([]Roles, error) {
return r, nil
}

func (c *RoleService) GetRolesById(team, id string) (*Roles, error) {
path := fmt.Sprintf("/api/account/teams/%s/roles/%s/", team, id)
body, err := c.client.newRequestDo("GET", path, nil)
if err != nil {
return nil, err
}
var r Roles
err = json.Unmarshal(body.BodyBytes, &r)
if err != nil {
return nil, err
}
return &r, nil

}

func (c *RoleService) UpdateRoles(team string, role *Roles) (*Roles, error) {
path := fmt.Sprintf("/api/account/teams/%s/roles/%s/", team, role.Unique_Id)

Expand Down
12 changes: 6 additions & 6 deletions client/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ type Services struct {
Creation_Date string `json:"creation_date"`
Summary string `json:"summary"`
Description string `json:"description"`
Unique_Id string `json:"unique_id",omitempty`
Auto_Resolve_Timeout int `json:"auto_resolve_timeout",omitempty`
Created_By string `json:"created_by",omitempty`
Unique_Id string `json:"unique_id,omitempty"`
Auto_Resolve_Timeout int `json:"auto_resolve_timeout,omitempty"`
Created_By string `json:"created_by,omitempty"`
Team_Priority string `json:"team_priority"`
Task_Template string `json:"task_template"`
Acknowledgment_Timeout int `json:"acknowledge_timeout",omitempty`
Status int `json:"status",omitempty`
Acknowledgment_Timeout int `json:"acknowledge_timeout,omitempty"`
Status int `json:"status,omitempty"`
Escalation_Policy string `json:"escalation_policy"`
Team string `json:"team"`
Sla string `json:"sla"`
Collation_Time int `json:"collation_time"`
Collation int `json:"collation"`
Under_Maintenance bool `json:"under_maintenance",omitempty`
Under_Maintenance bool `json:"under_maintenance,omitempty"`
}

func (c *Service) CreateService(team string, service *Services) (*Services, error) {
Expand Down

0 comments on commit fcdcffc

Please sign in to comment.