github.com/blend/go-sdk@v1.20220411.3/pagerduty/list_incidents.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package pagerduty 9 10 import ( 11 "context" 12 "encoding/json" 13 "fmt" 14 "net/http" 15 16 "github.com/blend/go-sdk/ex" 17 "github.com/blend/go-sdk/r2" 18 ) 19 20 // ListIncidents lists incidents. 21 // 22 // Use the variadic options to set constraining query parameters to filter or sort the incidents. 23 func (hc HTTPClient) ListIncidents(ctx context.Context, opts ...ListIncidentOption) (output ListIncidentsOutput, err error) { 24 var options ListIncidentsOptions 25 for _, opt := range opts { 26 opt(&options) 27 } 28 var res *http.Response 29 res, err = hc.Request(ctx, 30 append([]r2.Option{ 31 r2.OptGet(), 32 r2.OptPath("/incidents"), 33 }, options.Options()...)..., 34 ).Do() 35 if err != nil { 36 return 37 } 38 if statusCode := res.StatusCode; statusCode < 200 || statusCode > 299 { 39 err = ex.New(ErrNon200Status, ex.OptMessagef("method: post, path: /incidents, status: %d", statusCode)) 40 return 41 } 42 defer res.Body.Close() 43 if err = json.NewDecoder(res.Body).Decode(&output); err != nil { 44 err = ex.New(err) 45 return 46 } 47 return 48 } 49 50 // OptListIncidentsDateRange sets a field on the options. 51 func OptListIncidentsDateRange(dateRange string) ListIncidentOption { 52 return func(lio *ListIncidentsOptions) { lio.DateRange = dateRange } 53 } 54 55 // OptListIncidentsIncidentKey sets a field on the options. 56 func OptListIncidentsIncidentKey(incidentKey string) ListIncidentOption { 57 return func(lio *ListIncidentsOptions) { lio.IncidentKey = incidentKey } 58 } 59 60 // OptListIncidentsInclude sets the "include" query string value. 61 // 62 // Include sets if we should add additional data to the response for 63 // corresponding fields on the output object. 64 func OptListIncidentsInclude(include ...Include) ListIncidentOption { 65 return func(lio *ListIncidentsOptions) { lio.Include = include } 66 } 67 68 // OptListIncidentsLimit sets a field on the options. 69 func OptListIncidentsLimit(limit int) ListIncidentOption { 70 return func(lio *ListIncidentsOptions) { lio.Limit = limit } 71 } 72 73 // OptListIncidentsOffset sets a field on the options. 74 func OptListIncidentsOffset(offset int) ListIncidentOption { 75 return func(lio *ListIncidentsOptions) { lio.Offset = offset } 76 } 77 78 // OptListIncidentsServiceIDs sets a field on the options. 79 func OptListIncidentsServiceIDs(serviceIDs ...string) ListIncidentOption { 80 return func(lio *ListIncidentsOptions) { lio.ServiceIDs = serviceIDs } 81 } 82 83 // OptListIncidentsSince sets a field on the options. 84 func OptListIncidentsSince(since string) ListIncidentOption { 85 return func(lio *ListIncidentsOptions) { lio.Since = since } 86 } 87 88 // OptListIncidentsSortBy sets a field on the options. 89 func OptListIncidentsSortBy(sortBy string) ListIncidentOption { 90 return func(lio *ListIncidentsOptions) { lio.SortBy = sortBy } 91 } 92 93 // OptListIncidentsStatuses sets a field on the options. 94 func OptListIncidentsStatuses(statuses ...IncidentStatus) ListIncidentOption { 95 return func(lio *ListIncidentsOptions) { lio.Statuses = statuses } 96 } 97 98 // OptListIncidentsTeamIDs sets a field on the options. 99 func OptListIncidentsTeamIDs(teamIDs ...string) ListIncidentOption { 100 return func(lio *ListIncidentsOptions) { lio.TeamIDs = teamIDs } 101 } 102 103 // OptListIncidentsTimeZone sets a field on the options. 104 func OptListIncidentsTimeZone(timeZone string) ListIncidentOption { 105 return func(lio *ListIncidentsOptions) { lio.TimeZone = timeZone } 106 } 107 108 // OptListIncidentsTotal sets a field on the options. 109 func OptListIncidentsTotal(total bool) ListIncidentOption { 110 return func(lio *ListIncidentsOptions) { lio.Total = &total } 111 } 112 113 // OptListIncidentsUntil sets a field on the options. 114 func OptListIncidentsUntil(until string) ListIncidentOption { 115 return func(lio *ListIncidentsOptions) { lio.Until = until } 116 } 117 118 // OptListIncidentsUrgencies sets a field on the options. 119 func OptListIncidentsUrgencies(urgencies ...string) ListIncidentOption { 120 return func(lio *ListIncidentsOptions) { lio.Urgencies = urgencies } 121 } 122 123 // OptListIncidentsUserIDs sets a field on the options. 124 func OptListIncidentsUserIDs(userIDs ...string) ListIncidentOption { 125 return func(lio *ListIncidentsOptions) { lio.UserIDs = userIDs } 126 } 127 128 // ListIncidentOption mutates the list incidents options. 129 type ListIncidentOption func(*ListIncidentsOptions) 130 131 // ListIncidentsOptions are all the options for a list incidents call. 132 type ListIncidentsOptions struct { 133 DateRange string 134 IncidentKey string 135 Include []Include 136 Limit int 137 Offset int 138 ServiceIDs []string 139 Since string 140 SortBy string 141 Statuses []IncidentStatus 142 TeamIDs []string 143 TimeZone string 144 Total *bool 145 Until string 146 Urgencies []string 147 UserIDs []string 148 } 149 150 // Options yields the r2 options for the options. 151 // 152 // _Allow myself to introduce ... myself_ 153 func (lio ListIncidentsOptions) Options() (output []r2.Option) { 154 if lio.DateRange != "" { 155 output = append(output, r2.OptQueryValue("date_range", lio.DateRange)) 156 } 157 if lio.IncidentKey != "" { 158 output = append(output, r2.OptQueryValue("incident_key", lio.IncidentKey)) 159 } 160 if len(lio.Include) > 0 { 161 for _, include := range lio.Include { 162 output = append(output, r2.OptQueryValueAdd("include[]", string(include))) 163 } 164 } 165 if lio.Limit > 0 { 166 output = append(output, r2.OptQueryValue("limit ", fmt.Sprint(lio.Limit))) 167 } 168 if lio.Offset > 0 { 169 output = append(output, r2.OptQueryValue("offset", fmt.Sprint(lio.Offset))) 170 } 171 if len(lio.ServiceIDs) > 0 { 172 for _, serviceID := range lio.ServiceIDs { 173 output = append(output, r2.OptQueryValueAdd("service_ids[]", serviceID)) 174 } 175 } 176 if lio.Since != "" { 177 output = append(output, r2.OptQueryValue("since", lio.Since)) 178 } 179 if lio.SortBy != "" { 180 output = append(output, r2.OptQueryValue("sort_by", lio.SortBy)) 181 } 182 if len(lio.Statuses) > 0 { 183 for _, status := range lio.Statuses { 184 output = append(output, r2.OptQueryValueAdd("statuses[]", string(status))) 185 } 186 } 187 if len(lio.TeamIDs) > 0 { 188 for _, teamID := range lio.TeamIDs { 189 output = append(output, r2.OptQueryValueAdd("team_ids[]", teamID)) 190 } 191 } 192 if lio.TimeZone != "" { 193 output = append(output, r2.OptQueryValue("time_zone", lio.TimeZone)) 194 } 195 if lio.Total != nil { 196 output = append(output, r2.OptQueryValue("total", fmt.Sprint(*lio.Total))) 197 } 198 if lio.Until != "" { 199 output = append(output, r2.OptQueryValue("until", lio.Until)) 200 } 201 if len(lio.Urgencies) > 0 { 202 for _, urgency := range lio.Urgencies { 203 output = append(output, r2.OptQueryValueAdd("urgencies[]", urgency)) 204 } 205 } 206 if len(lio.UserIDs) > 0 { 207 for _, userID := range lio.UserIDs { 208 output = append(output, r2.OptQueryValueAdd("user_ids[]", userID)) 209 } 210 } 211 return 212 } 213 214 // ListIncidentsOutput is the output of a list incidents call. 215 type ListIncidentsOutput struct { 216 Offset int `json:"offset"` 217 Limit int `json:"limit"` 218 More bool `json:"more"` 219 Total *int `json:"total"` 220 Incidents []Incident `json:"incidents"` 221 }