Skip to content

Commit

Permalink
Fixes issue #20. Changes to how the client is created
Browse files Browse the repository at this point in the history
  • Loading branch information
jbogarin committed Aug 6, 2021
1 parent c420d0c commit 2f14ac0
Show file tree
Hide file tree
Showing 19 changed files with 165 additions and 165 deletions.
1 change: 1 addition & 0 deletions examples/people/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

// Client is Webex Teams API client
var Client *webexteams.Client
var Client2 *webexteams.Client

func main() {
Client = webexteams.NewClient()
Expand Down
2 changes: 1 addition & 1 deletion sdk/admin_audit_events_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (s *AdminAuditEventsService) ListAdminAuditEvents(listAdminAuditEventsQuery

queryString, _ := query.Values(listAdminAuditEventsQueryParams)

response, err := RestyClient.R().
response, err := s.client.R().
SetQueryString(queryString.Encode()).
SetResult(&AuditEvents{}).
SetError(&Error{}).
Expand Down
11 changes: 5 additions & 6 deletions sdk/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
)

// RestyClient is the REST Client
var RestyClient *resty.Client

const apiURL = "https://webexapis.com/v1"

Expand Down Expand Up @@ -35,23 +34,23 @@ type Client struct {
}

type service struct {
client *Client
client *resty.Client
}

// SetAuthToken defines the Authorization token sent in the request
func (s *Client) SetAuthToken(accessToken string) {
RestyClient.SetAuthToken(accessToken)
s.common.client.SetAuthToken(accessToken)
}

// NewClient creates a new API client. Requires a userAgent string describing your application.
// optionally a custom http.Client to allow for advanced features such as caching.
func NewClient() *Client {
client := resty.New()
c := &Client{}
RestyClient = client
RestyClient.SetHostURL(apiURL)
c.common.client = client
c.common.client.SetHostURL(apiURL)
if os.Getenv("WEBEX_TEAMS_ACCESS_TOKEN") != "" {
RestyClient.SetAuthToken(os.Getenv("WEBEX_TEAMS_ACCESS_TOKEN"))
c.common.client.SetAuthToken(os.Getenv("WEBEX_TEAMS_ACCESS_TOKEN"))
}

// API Services
Expand Down
2 changes: 1 addition & 1 deletion sdk/contents_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (s *ContentsService) GetContent(contentID string) (*resty.Response, error)
path := "/contents/{contentId}"
path = strings.Replace(path, "{"+"contentId"+"}", fmt.Sprintf("%v", contentID), -1)

response, err := RestyClient.R().
response, err := s.client.R().
SetError(&Error{}).
Get(path)

Expand Down
22 changes: 11 additions & 11 deletions sdk/devices_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ func (devices *Devices) AddDevice(item Device) []Device {
return devices.Items
}

func devicesPagination(linkHeader string, size, max int) *Devices {
func (s *DevicesService) devicesPagination(linkHeader string, size, max int) *Devices {
items := &Devices{}

for _, l := range link.Parse(linkHeader) {
if l.Rel == "next" {
response, err := RestyClient.R().
response, err := s.client.R().
SetResult(&Devices{}).
SetError(&Error{}).
Get(l.URI)
Expand All @@ -77,13 +77,13 @@ func devicesPagination(linkHeader string, size, max int) *Devices {
if size != 0 {
size = size + len(items.Items)
if size < max {
devices := devicesPagination(response.Header().Get("Link"), size, max)
devices := s.devicesPagination(response.Header().Get("Link"), size, max)
for _, device := range devices.Items {
items.AddDevice(device)
}
}
} else {
devices := devicesPagination(response.Header().Get("Link"), size, max)
devices := s.devicesPagination(response.Header().Get("Link"), size, max)
for _, device := range devices.Items {
items.AddDevice(device)
}
Expand All @@ -106,7 +106,7 @@ func (s *DevicesService) CreateDeviceActivationCode(deviceCodeRequest *DeviceCod

path := "/devices/activationCode"

response, err := RestyClient.R().
response, err := s.client.R().
SetBody(deviceCodeRequest).
SetResult(&DeviceCode{}).
SetError(&Error{}).
Expand All @@ -133,7 +133,7 @@ func (s *DevicesService) DeleteDevice(deviceID string) (*resty.Response, error)
path := "/devices/{deviceId}"
path = strings.Replace(path, "{"+"deviceId"+"}", fmt.Sprintf("%v", deviceID), -1)

response, err := RestyClient.R().
response, err := s.client.R().
SetError(&Error{}).
Delete(path)

Expand All @@ -155,7 +155,7 @@ func (s *DevicesService) GetDevice(deviceID string) (*Device, *resty.Response, e
path := "/devices/{deviceId}"
path = strings.Replace(path, "{"+"deviceId"+"}", fmt.Sprintf("%v", deviceID), -1)

response, err := RestyClient.R().
response, err := s.client.R().
SetResult(&Device{}).
SetError(&Error{}).
Get(path)
Expand Down Expand Up @@ -216,7 +216,7 @@ func (s *DevicesService) ListDevices(queryParams *ListDevicesQueryParams) (*Devi

queryParamsString, _ := query.Values(queryParams)

response, err := RestyClient.R().
response, err := s.client.R().
SetQueryString(queryParamsString.Encode()).
SetResult(&Devices{}).
SetError(&Error{}).
Expand All @@ -227,14 +227,14 @@ func (s *DevicesService) ListDevices(queryParams *ListDevicesQueryParams) (*Devi
}

result := response.Result().(*Devices)
if queryParams.Paginate == true {
items := devicesPagination(response.Header().Get("Link"), 0, 0)
if queryParams.Paginate {
items := s.devicesPagination(response.Header().Get("Link"), 0, 0)
for _, device := range items.Items {
result.AddDevice(device)
}
} else {
if len(result.Items) < queryParams.Max {
items := devicesPagination(response.Header().Get("Link"), len(result.Items), queryParams.Max)
items := s.devicesPagination(response.Header().Get("Link"), len(result.Items), queryParams.Max)
for _, device := range items.Items {
result.AddDevice(device)
}
Expand Down
4 changes: 2 additions & 2 deletions sdk/events_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (s *EventsService) ListEvents(listEventsQueryParams *ListEventsQueryParams)

queryString, _ := query.Values(listEventsQueryParams)

response, err := RestyClient.R().
response, err := s.client.R().
SetQueryString(queryString.Encode()).
SetResult(&Events{}).
SetError(&Error{}).
Expand All @@ -91,7 +91,7 @@ func (s *EventsService) GetEvent(eventID string) (*Event, *resty.Response, error
path := "/events/{eventId}"
path = strings.Replace(path, "{"+"eventId"+"}", fmt.Sprintf("%v", eventID), -1)

response, err := RestyClient.R().
response, err := s.client.R().
SetResult(&Event{}).
SetError(&Error{}).
Get(path)
Expand Down
18 changes: 9 additions & 9 deletions sdk/licenses_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ func (licenses *Licenses) AddLicense(item License) []License {
return licenses.Items
}

func licensesPagination(linkHeader string, size, max int) *Licenses {
func (s *LicensesService) licensesPagination(linkHeader string, size, max int) *Licenses {
items := &Licenses{}

for _, l := range link.Parse(linkHeader) {
if l.Rel == "next" {

response, err := RestyClient.R().
response, err := s.client.R().
SetResult(&Licenses{}).
SetError(&Error{}).
Get(l.URI)
Expand All @@ -49,13 +49,13 @@ func licensesPagination(linkHeader string, size, max int) *Licenses {
if size != 0 {
size = size + len(items.Items)
if size < max {
licenses := licensesPagination(response.Header().Get("Link"), size, max)
licenses := s.licensesPagination(response.Header().Get("Link"), size, max)
for _, license := range licenses.Items {
items.AddLicense(license)
}
}
} else {
licenses := licensesPagination(response.Header().Get("Link"), size, max)
licenses := s.licensesPagination(response.Header().Get("Link"), size, max)
for _, license := range licenses.Items {
items.AddLicense(license)
}
Expand All @@ -79,7 +79,7 @@ func (s *LicensesService) GetLicense(licenseID string) (*License, *resty.Respons
path := "/licenses/{licenseId}"
path = strings.Replace(path, "{"+"licenseId"+"}", fmt.Sprintf("%v", licenseID), -1)

response, err := RestyClient.R().
response, err := s.client.R().
SetResult(&License{}).
SetError(&Error{}).
Get(path)
Expand Down Expand Up @@ -113,7 +113,7 @@ func (s *LicensesService) ListLicenses(queryParams *ListLicensesQueryParams) (*L

queryParamsString, _ := query.Values(queryParams)

response, err := RestyClient.R().
response, err := s.client.R().
SetQueryString(queryParamsString.Encode()).
SetResult(&Licenses{}).
SetError(&Error{}).
Expand All @@ -124,14 +124,14 @@ func (s *LicensesService) ListLicenses(queryParams *ListLicensesQueryParams) (*L
}

result := response.Result().(*Licenses)
if queryParams.Paginate == true {
items := licensesPagination(response.Header().Get("Link"), 0, 0)
if queryParams.Paginate {
items := s.licensesPagination(response.Header().Get("Link"), 0, 0)
for _, license := range items.Items {
result.AddLicense(license)
}
} else {
if len(result.Items) < queryParams.Max {
items := licensesPagination(response.Header().Get("Link"), len(result.Items), queryParams.Max)
items := s.licensesPagination(response.Header().Get("Link"), len(result.Items), queryParams.Max)
for _, license := range items.Items {
result.AddLicense(license)
}
Expand Down
22 changes: 11 additions & 11 deletions sdk/meetings_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ func (meetings *Meetings) AddMeeting(item Meeting) []Meeting {
return meetings.Items
}

func meetingsPagination(linkHeader string, size, max int) *Meetings {
func (s *MeetingsService) meetingsPagination(linkHeader string, size, max int) *Meetings {
items := &Meetings{}

for _, l := range link.Parse(linkHeader) {
if l.Rel == "next" {

response, err := RestyClient.R().
response, err := s.client.R().
SetResult(&Meetings{}).
SetError(&Error{}).
Get(l.URI)
Expand All @@ -104,13 +104,13 @@ func meetingsPagination(linkHeader string, size, max int) *Meetings {
if size != 0 {
size = size + len(items.Items)
if size < max {
meetings := meetingsPagination(response.Header().Get("Link"), size, max)
meetings := s.meetingsPagination(response.Header().Get("Link"), size, max)
for _, meeting := range meetings.Items {
items.AddMeeting(meeting)
}
}
} else {
meetings := meetingsPagination(response.Header().Get("Link"), size, max)
meetings := s.meetingsPagination(response.Header().Get("Link"), size, max)
for _, meeting := range meetings.Items {
items.AddMeeting(meeting)
}
Expand All @@ -133,7 +133,7 @@ func (s *MeetingsService) CreateMeeting(meetingCreateRequest *MeetingCreateReque

path := "/meetings/"

response, err := RestyClient.R().
response, err := s.client.R().
SetBody(meetingCreateRequest).
SetResult(&Meeting{}).
SetError(&Error{}).
Expand All @@ -158,7 +158,7 @@ func (s *MeetingsService) DeleteMeeting(meetingID string) (*resty.Response, erro
path := "/meetings/{meetingId}"
path = strings.Replace(path, "{"+"meetingId"+"}", fmt.Sprintf("%v", meetingID), -1)

response, err := RestyClient.R().
response, err := s.client.R().
SetError(&Error{}).
Delete(path)

Expand All @@ -182,7 +182,7 @@ func (s *MeetingsService) GetMeeting(meetingID string) (*Meeting, *resty.Respons
path := "/meetings/{meetingId}"
path = strings.Replace(path, "{"+"meetingId"+"}", fmt.Sprintf("%v", meetingID), -1)

response, err := RestyClient.R().
response, err := s.client.R().
SetResult(&Meeting{}).
SetError(&Error{}).
Get(path)
Expand Down Expand Up @@ -225,7 +225,7 @@ func (s *MeetingsService) ListMeetings(queryParams *ListMeetingsQueryParams) (*M

queryParamsString, _ := query.Values(queryParams)

response, err := RestyClient.R().
response, err := s.client.R().
SetQueryString(queryParamsString.Encode()).
SetResult(&Meetings{}).
SetError(&Error{}).
Expand All @@ -236,14 +236,14 @@ func (s *MeetingsService) ListMeetings(queryParams *ListMeetingsQueryParams) (*M
}

result := response.Result().(*Meetings)
if queryParams.Paginate == true {
items := meetingsPagination(response.Header().Get("Link"), 0, 0)
if queryParams.Paginate {
items := s.meetingsPagination(response.Header().Get("Link"), 0, 0)
for _, meeting := range items.Items {
result.AddMeeting(meeting)
}
} else {
if len(result.Items) < queryParams.Max {
items := meetingsPagination(response.Header().Get("Link"), len(result.Items), queryParams.Max)
items := s.meetingsPagination(response.Header().Get("Link"), len(result.Items), queryParams.Max)
for _, meeting := range items.Items {
result.AddMeeting(meeting)
}
Expand Down
Loading

0 comments on commit 2f14ac0

Please sign in to comment.