-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add [POST] /v1/oauth/token endpoint (#35)
* Add [POST] /v1/oauth/token endpoint * Add timeout tests... * Add docs for OAuth * Rename sample board to prevent race condition
- Loading branch information
1 parent
a0f8353
commit 5220f3e
Showing
6 changed files
with
128 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package controllers | ||
|
||
import ( | ||
"github.com/BrandonRomano/wrecker" | ||
) | ||
|
||
// OAuthController is the controller that is responsible | ||
// for all /v1/oauth endpoints in the Pinterest API. | ||
type OAuthController struct { | ||
wreckerClient *wrecker.Wrecker | ||
Token *OAuthTokenController | ||
} | ||
|
||
// NewOAuthController instantiates a new OAuthController | ||
func NewOAuthController(wc *wrecker.Wrecker) *OAuthController { | ||
return &OAuthController{ | ||
wreckerClient: wc, | ||
Token: newOAuthTokenController(wc), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package controllers | ||
|
||
import ( | ||
"github.com/BrandonRomano/wrecker" | ||
"github.com/carrot/go-pinterest/models" | ||
) | ||
|
||
// OAuthTokenController is the controller that is responsible | ||
// for all /v1/oauth/token endpoints in the Pinterest API. | ||
type OAuthTokenController struct { | ||
wreckerClient *wrecker.Wrecker | ||
} | ||
|
||
// newOAuthTokenController instantiates a new OAuthTokenController | ||
func newOAuthTokenController(wc *wrecker.Wrecker) *OAuthTokenController { | ||
return &OAuthTokenController{ | ||
wreckerClient: wc, | ||
} | ||
} | ||
|
||
// Create generates an access token | ||
// Endpoint: [POST] /v1/oauth/token | ||
func (otc *OAuthTokenController) Create(clientId, clientSecret, accessCode string) (*models.AccessToken, error) { | ||
// Build + execute request | ||
accessToken := new(models.AccessToken) | ||
httpResp, err := otc.wreckerClient.Post("/oauth/token"). | ||
URLParam("grant_type", "authorization_code"). | ||
URLParam("client_id", clientId). | ||
URLParam("client_secret", clientSecret). | ||
URLParam("code", accessCode). | ||
Into(accessToken). | ||
Execute() | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if !(httpResp.StatusCode >= 200 && httpResp.StatusCode < 300) { | ||
return nil, &models.PinterestError{ | ||
StatusCode: httpResp.StatusCode, | ||
Message: accessToken.Error, | ||
} | ||
} | ||
|
||
// OK | ||
return accessToken, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package models | ||
|
||
// AccessToken is a struct that represents a Access Token | ||
// response from the Pinterest API. | ||
type AccessToken struct { | ||
AccessToken string `json:"access_token"` | ||
TokenType string `json:"token_type"` | ||
Scope []string `json:"scope"` | ||
ErrorDescription string `json:"error_description"` | ||
Error string `json:"error"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters