Skip to content

Commit

Permalink
Merge branch '0.14' into better-error-handling-and-use
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobbednarz authored Feb 28, 2021
2 parents b4ca00a + d346eb5 commit c32c9c6
Show file tree
Hide file tree
Showing 119 changed files with 2,664 additions and 2,142 deletions.
74 changes: 38 additions & 36 deletions access_application.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package cloudflare

import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"time"
Expand Down Expand Up @@ -60,18 +62,18 @@ type AccessApplicationDetailResponse struct {
// AccessApplications returns all applications within an account.
//
// API reference: https://api.cloudflare.com/#access-applications-list-access-applications
func (api *API) AccessApplications(accountID string, pageOpts PaginationOptions) ([]AccessApplication, ResultInfo, error) {
return api.accessApplications(accountID, pageOpts, AccountRouteRoot)
func (api *API) AccessApplications(ctx context.Context, accountID string, pageOpts PaginationOptions) ([]AccessApplication, ResultInfo, error) {
return api.accessApplications(ctx, accountID, pageOpts, AccountRouteRoot)
}

// ZoneLevelAccessApplications returns all applications within a zone.
//
// API reference: https://api.cloudflare.com/#zone-level-access-applications-list-access-applications
func (api *API) ZoneLevelAccessApplications(zoneID string, pageOpts PaginationOptions) ([]AccessApplication, ResultInfo, error) {
return api.accessApplications(zoneID, pageOpts, ZoneRouteRoot)
func (api *API) ZoneLevelAccessApplications(ctx context.Context, zoneID string, pageOpts PaginationOptions) ([]AccessApplication, ResultInfo, error) {
return api.accessApplications(ctx, zoneID, pageOpts, ZoneRouteRoot)
}

func (api *API) accessApplications(id string, pageOpts PaginationOptions, routeRoot RouteRoot) ([]AccessApplication, ResultInfo, error) {
func (api *API) accessApplications(ctx context.Context, id string, pageOpts PaginationOptions, routeRoot RouteRoot) ([]AccessApplication, ResultInfo, error) {
v := url.Values{}
if pageOpts.PerPage > 0 {
v.Set("per_page", strconv.Itoa(pageOpts.PerPage))
Expand All @@ -85,7 +87,7 @@ func (api *API) accessApplications(id string, pageOpts PaginationOptions, routeR
uri = uri + "?" + v.Encode()
}

res, err := api.makeRequest("GET", uri, nil)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []AccessApplication{}, ResultInfo{}, err
}
Expand All @@ -103,27 +105,27 @@ func (api *API) accessApplications(id string, pageOpts PaginationOptions, routeR
// application ID.
//
// API reference: https://api.cloudflare.com/#access-applications-access-applications-details
func (api *API) AccessApplication(accountID, applicationID string) (AccessApplication, error) {
return api.accessApplication(accountID, applicationID, AccountRouteRoot)
func (api *API) AccessApplication(ctx context.Context, accountID, applicationID string) (AccessApplication, error) {
return api.accessApplication(ctx, accountID, applicationID, AccountRouteRoot)
}

// ZoneLevelAccessApplication returns a single zone level application based on the
// application ID.
//
// API reference: https://api.cloudflare.com/#zone-level-access-applications-access-applications-details
func (api *API) ZoneLevelAccessApplication(zoneID, applicationID string) (AccessApplication, error) {
return api.accessApplication(zoneID, applicationID, ZoneRouteRoot)
func (api *API) ZoneLevelAccessApplication(ctx context.Context, zoneID, applicationID string) (AccessApplication, error) {
return api.accessApplication(ctx, zoneID, applicationID, ZoneRouteRoot)
}

func (api *API) accessApplication(id, applicationID string, routeRoot RouteRoot) (AccessApplication, error) {
func (api *API) accessApplication(ctx context.Context, id, applicationID string, routeRoot RouteRoot) (AccessApplication, error) {
uri := fmt.Sprintf(
"/%s/%s/access/apps/%s",
routeRoot,
id,
applicationID,
)

res, err := api.makeRequest("GET", uri, nil)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return AccessApplication{}, err
}
Expand All @@ -140,21 +142,21 @@ func (api *API) accessApplication(id, applicationID string, routeRoot RouteRoot)
// CreateAccessApplication creates a new access application.
//
// API reference: https://api.cloudflare.com/#access-applications-create-access-application
func (api *API) CreateAccessApplication(accountID string, accessApplication AccessApplication) (AccessApplication, error) {
return api.createAccessApplication(accountID, accessApplication, AccountRouteRoot)
func (api *API) CreateAccessApplication(ctx context.Context, accountID string, accessApplication AccessApplication) (AccessApplication, error) {
return api.createAccessApplication(ctx, accountID, accessApplication, AccountRouteRoot)
}

// CreateZoneLevelAccessApplication creates a new zone level access application.
//
// API reference: https://api.cloudflare.com/#zone-level-access-applications-create-access-application
func (api *API) CreateZoneLevelAccessApplication(zoneID string, accessApplication AccessApplication) (AccessApplication, error) {
return api.createAccessApplication(zoneID, accessApplication, ZoneRouteRoot)
func (api *API) CreateZoneLevelAccessApplication(ctx context.Context, zoneID string, accessApplication AccessApplication) (AccessApplication, error) {
return api.createAccessApplication(ctx, zoneID, accessApplication, ZoneRouteRoot)
}

func (api *API) createAccessApplication(id string, accessApplication AccessApplication, routeRoot RouteRoot) (AccessApplication, error) {
func (api *API) createAccessApplication(ctx context.Context, id string, accessApplication AccessApplication, routeRoot RouteRoot) (AccessApplication, error) {
uri := fmt.Sprintf("/%s/%s/access/apps", routeRoot, id)

res, err := api.makeRequest("POST", uri, accessApplication)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, accessApplication)
if err != nil {
return AccessApplication{}, err
}
Expand All @@ -171,18 +173,18 @@ func (api *API) createAccessApplication(id string, accessApplication AccessAppli
// UpdateAccessApplication updates an existing access application.
//
// API reference: https://api.cloudflare.com/#access-applications-update-access-application
func (api *API) UpdateAccessApplication(accountID string, accessApplication AccessApplication) (AccessApplication, error) {
return api.updateAccessApplication(accountID, accessApplication, AccountRouteRoot)
func (api *API) UpdateAccessApplication(ctx context.Context, accountID string, accessApplication AccessApplication) (AccessApplication, error) {
return api.updateAccessApplication(ctx, accountID, accessApplication, AccountRouteRoot)
}

// UpdateZoneLevelAccessApplication updates an existing zone level access application.
//
// API reference: https://api.cloudflare.com/#zone-level-access-applications-update-access-application
func (api *API) UpdateZoneLevelAccessApplication(zoneID string, accessApplication AccessApplication) (AccessApplication, error) {
return api.updateAccessApplication(zoneID, accessApplication, ZoneRouteRoot)
func (api *API) UpdateZoneLevelAccessApplication(ctx context.Context, zoneID string, accessApplication AccessApplication) (AccessApplication, error) {
return api.updateAccessApplication(ctx, zoneID, accessApplication, ZoneRouteRoot)
}

func (api *API) updateAccessApplication(id string, accessApplication AccessApplication, routeRoot RouteRoot) (AccessApplication, error) {
func (api *API) updateAccessApplication(ctx context.Context, id string, accessApplication AccessApplication, routeRoot RouteRoot) (AccessApplication, error) {
if accessApplication.ID == "" {
return AccessApplication{}, errors.Errorf("access application ID cannot be empty")
}
Expand All @@ -194,7 +196,7 @@ func (api *API) updateAccessApplication(id string, accessApplication AccessAppli
accessApplication.ID,
)

res, err := api.makeRequest("PUT", uri, accessApplication)
res, err := api.makeRequestContext(ctx, http.MethodPut, uri, accessApplication)
if err != nil {
return AccessApplication{}, err
}
Expand All @@ -211,26 +213,26 @@ func (api *API) updateAccessApplication(id string, accessApplication AccessAppli
// DeleteAccessApplication deletes an access application.
//
// API reference: https://api.cloudflare.com/#access-applications-delete-access-application
func (api *API) DeleteAccessApplication(accountID, applicationID string) error {
return api.deleteAccessApplication(accountID, applicationID, AccountRouteRoot)
func (api *API) DeleteAccessApplication(ctx context.Context, accountID, applicationID string) error {
return api.deleteAccessApplication(ctx, accountID, applicationID, AccountRouteRoot)
}

// DeleteZoneLevelAccessApplication deletes a zone level access application.
//
// API reference: https://api.cloudflare.com/#zone-level-access-applications-delete-access-application
func (api *API) DeleteZoneLevelAccessApplication(zoneID, applicationID string) error {
return api.deleteAccessApplication(zoneID, applicationID, ZoneRouteRoot)
func (api *API) DeleteZoneLevelAccessApplication(ctx context.Context, zoneID, applicationID string) error {
return api.deleteAccessApplication(ctx, zoneID, applicationID, ZoneRouteRoot)
}

func (api *API) deleteAccessApplication(id, applicationID string, routeRoot RouteRoot) error {
func (api *API) deleteAccessApplication(ctx context.Context, id, applicationID string, routeRoot RouteRoot) error {
uri := fmt.Sprintf(
"/%s/%s/access/apps/%s",
routeRoot,
id,
applicationID,
)

_, err := api.makeRequest("DELETE", uri, nil)
_, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return err
}
Expand All @@ -242,27 +244,27 @@ func (api *API) deleteAccessApplication(id, applicationID string, routeRoot Rout
// access application.
//
// API reference: https://api.cloudflare.com/#access-applications-revoke-access-tokens
func (api *API) RevokeAccessApplicationTokens(accountID, applicationID string) error {
return api.revokeAccessApplicationTokens(accountID, applicationID, AccountRouteRoot)
func (api *API) RevokeAccessApplicationTokens(ctx context.Context, accountID, applicationID string) error {
return api.revokeAccessApplicationTokens(ctx, accountID, applicationID, AccountRouteRoot)
}

// RevokeZoneLevelAccessApplicationTokens revokes tokens associated with a zone level
// access application.
//
// API reference: https://api.cloudflare.com/#zone-level-access-applications-revoke-access-tokens
func (api *API) RevokeZoneLevelAccessApplicationTokens(zoneID, applicationID string) error {
return api.revokeAccessApplicationTokens(zoneID, applicationID, ZoneRouteRoot)
func (api *API) RevokeZoneLevelAccessApplicationTokens(ctx context.Context, zoneID, applicationID string) error {
return api.revokeAccessApplicationTokens(ctx, zoneID, applicationID, ZoneRouteRoot)
}

func (api *API) revokeAccessApplicationTokens(id string, applicationID string, routeRoot RouteRoot) error {
func (api *API) revokeAccessApplicationTokens(ctx context.Context, id string, applicationID string, routeRoot RouteRoot) error {
uri := fmt.Sprintf(
"/%s/%s/access/apps/%s/revoke-tokens",
routeRoot,
id,
applicationID,
)

_, err := api.makeRequest("POST", uri, nil)
_, err := api.makeRequestContext(ctx, http.MethodPost, uri, nil)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit c32c9c6

Please sign in to comment.