Commit 907e1703 authored by Morgan McMillian's avatar Morgan McMillian

minor refactoring and updated comments

parent cf9d91ac
......@@ -5,51 +5,54 @@ import (
"strings"
)
// App or client object, https://pnut.io/docs/api/resources/clients
type App struct {
Id string `json:"id"`
ID string `json:"id"`
Link string `json:"link"`
Name string `json:"name"`
}
// Token object, https://pnut.io/docs/api/resources/token
type Token struct {
App App `json:"app"`
Scopes []string `json:"scopes"`
User User `json:"user"`
ClientId string `json:"client_id"`
ClientID string `json:"client_id"`
}
// AccessTokenResult object
type AccessTokenResult struct {
AccessToken string `json:"access_token"`
Token Token `json:"token"`
UserId string `json:"user_id"`
UserID string `json:"user_id"`
Username string `json:"username"`
}
// Get AccessToken from authorization code
// AccessToken retrieves an access token using an authorization code
// https://pnut.io/docs/authentication/web-flows
func (c *Client) AccessToken(code string, redirectURI string) (result AccessTokenResult, err error) {
v := url.Values{}
v.Set("client_id", c.clientId)
v.Set("client_id", c.clientID)
v.Set("client_secret", c.clientSecret)
v.Set("code", code)
v.Set("redirect_uri", redirectURI)
v.Set("grant_type", "authorization_code")
response_ch := make(chan response)
c.queryQueue <- query{url: OAuthAccessTokenAPI, form: v, data: &result, method: "POST", response_ch: response_ch}
return result, (<-response_ch).err
responseCh := make(chan response)
c.queryQueue <- query{url: OAuthAccessTokenAPI, form: v, data: &result, method: "POST", responseCh: responseCh}
return result, (<-responseCh).err
}
// Get AccessToken from password
// AccessTokenFromPassword retrieves an access token from using a password
// https://pnut.io/docs/authentication/password-flow
func (c *Client) AccessTokenFromPassword(username string, password string, scope []string) (result AccessTokenResult, err error) {
v := url.Values{}
v.Set("client_id", c.clientId)
v.Set("client_id", c.clientID)
v.Set("password_grant_secret", c.passwordGrantSecret)
v.Set("username", username)
v.Set("password", password)
v.Set("grant_type", "password")
v.Set("scope", strings.Join(scope, ","))
response_ch := make(chan response)
c.queryQueue <- query{url: OAuthAccessTokenAPI, form: v, data: &result, method: "POST", response_ch: response_ch}
return result, (<-response_ch).err
responseCh := make(chan response)
c.queryQueue <- query{url: OAuthAccessTokenAPI, form: v, data: &result, method: "POST", responseCh: responseCh}
return result, (<-responseCh).err
}
......@@ -4,6 +4,7 @@ import (
"net/http"
)
// pnut.io v0 API endpoints, see https://pnut.io/docs/api/implementation/overview
const (
AuthenticateURL = "https://pnut.io/oauth/authenticate"
APIBasesURL = "https://api.pnut.io/v0/"
......@@ -27,6 +28,7 @@ const (
MarkerAPI = APIBasesURL + "markers"
)
// API definition
type API struct {
accessToken string
HTTPClient *http.Client
......
This diff is collapsed.
......@@ -11,30 +11,32 @@ import (
"strings"
)
// Client object definition
type Client struct {
clientId string
clientID string
clientSecret string
passwordGrantSecret string
queryQueue chan query
Api API
API API
}
func NewClient(clientId string, clientSecret string) *Client {
// NewClient returns a new client based on the specified ClientID and ClientSecret
func NewClient(clientID string, clientSecret string) *Client {
queue := make(chan query)
client := &Client{clientId: clientId, clientSecret: clientSecret, queryQueue: queue}
client := &Client{clientID: clientID, clientSecret: clientSecret, queryQueue: queue}
client.initialize()
go client.throttledQuery()
return client
}
type query struct {
url string
form url.Values
data interface{}
method string
response_ch chan response
json string
redirect bool
url string
form url.Values
data interface{}
method string
responseCh chan response
json string
redirect bool
}
type response struct {
......@@ -43,37 +45,39 @@ type response struct {
}
func (c *Client) initialize() {
c.Api = *&API{
c.API = *&API{
accessToken: "",
HTTPClient: http.DefaultClient,
}
}
// Generate authorization url
// AuthURL generates an authorization url
// https://pnut.io/docs/authentication/web-flows
func (c *Client) AuthURL(redirectURI string, scope []string, responseType string) string {
return AuthenticateURL + "?client_id=" + c.clientId + "&redirect_uri=" + redirectURI + "&scope=" + strings.Join(scope, "%20") + "&response_type=" + responseType
return AuthenticateURL + "?client_id=" + c.clientID + "&redirect_uri=" + redirectURI + "&scope=" + strings.Join(scope, "%20") + "&response_type=" + responseType
}
// Set password grant secret
// SetPasswordGrantSecret sets the password grant secret needed for password flow authentication
// https://pnut.io/docs/authentication/password-flow
func (c *Client) SetPasswordGrantSecret(passwordGrantSecret string) {
c.passwordGrantSecret = passwordGrantSecret
}
// Set access token
// SetAccessToken sets the access token for use by the client
// https://pnut.io/docs/authentication/web-flows
// https://pnut.io/docs/authentication/password-flow
func (c *Client) SetAccessToken(accessToken string) {
c.Api.accessToken = accessToken
c.API.accessToken = accessToken
}
// StreamMeta meta object definition
type StreamMeta struct {
More bool `json:"more"`
MaxId string `json:"max_id"`
MinId string `json:"min_id"`
MaxID string `json:"max_id"`
MinID string `json:"min_id"`
}
// Meta object definiton
type Meta struct {
*StreamMeta
Code int `json:"code"`
......@@ -81,6 +85,7 @@ type Meta struct {
ErrorMessage string `json:"error_message"`
}
// CommonResponse response object
type CommonResponse struct {
Meta Meta `json:"meta"`
}
......@@ -126,8 +131,8 @@ func (c *Client) execQuery(url string, form url.Values, data interface{}, method
if err != nil {
return err
}
if c.Api.accessToken != "" {
req.Header.Set("Authorization", "Bearer "+c.Api.accessToken)
if c.API.accessToken != "" {
req.Header.Set("Authorization", "Bearer "+c.API.accessToken)
}
if redirect {
res, err := http.DefaultTransport.RoundTrip(req)
......@@ -141,14 +146,12 @@ func (c *Client) execQuery(url string, form url.Values, data interface{}, method
// must fix with reflect
err = json.Unmarshal([]byte("{\"data\":\""+res.Header["Location"][0]+"\"}"), data)
return err
} else {
return fmt.Errorf("location is not found from header")
}
} else {
return fmt.Errorf(strconv.Itoa(res.StatusCode))
return fmt.Errorf("location is not found from header")
}
return fmt.Errorf(strconv.Itoa(res.StatusCode))
}
res, err := c.Api.HTTPClient.Do(req)
res, err := c.API.HTTPClient.Do(req)
if err != nil {
return err
}
......@@ -166,11 +169,11 @@ func (c *Client) throttledQuery() {
jsonStr := q.json
redirect := q.redirect
response_ch := q.response_ch
responseCh := q.responseCh
err := c.execQuery(url, form, data, method, jsonStr, redirect)
response_ch <- response{data, err}
responseCh <- response{data, err}
}
}
......
package woodstock
// ClientInfoResult object, https://pnut.io/docs/api/resources/clients
type ClientInfoResult struct {
*CommonResponse
Data ClientInfo `json:"data"`
}
// Get clinet
// GetClient retrieves client object based on the id
// https://pnut.io/docs/resources/clients#get-clients-id
func (c *Client) GetClient(id string) (result ClientInfosResult, err error) {
response_ch := make(chan response)
c.queryQueue <- query{url: ClientAPI + "/" + id, data: &result, method: "GET", response_ch: response_ch}
return result, (<-response_ch).err
responseCh := make(chan response)
c.queryQueue <- query{url: ClientAPI + "/" + id, data: &result, method: "GET", responseCh: responseCh}
return result, (<-responseCh).err
}
package woodstock
// Links object definition
type Links struct {
Link string `json:"link"`
Text string `json:"text"`
......@@ -9,8 +10,9 @@ type Links struct {
Description string `json:"description"`
}
// Mentions object definition
type Mentions struct {
Id string `json:"id"`
ID string `json:"id"`
Len int `json:"len"`
Pos int `json:"pos"`
Text string `json:"text"`
......@@ -18,12 +20,14 @@ type Mentions struct {
IsCopy bool `json:"is_copy"`
}
// Tags object definition
type Tags struct {
Len int `json:"len"`
Pos int `json:"pos"`
Text string `json:"text"`
}
// Entities object definition
type Entities struct {
Links []Links `json:"links"`
Mentions []Mentions `json:"mentions"`
......
package woodstock
// Image object definition
type Image struct {
Link string `json:"link"`
IsDefault bool `json:"is_default"`
......@@ -7,14 +8,16 @@ type Image struct {
Height int `json:"height"`
}
// ContentOfUser object definition
type ContentOfUser struct {
Text string `json:"text"`
Html string `json:"html"`
HTML string `json:"html"`
Entities Entities `json:"entities"`
AvatarImage Image `json:"avatar_image"`
CoverImage Image `json:"cover_image"`
}
// CountsOfUser object definition
type CountsOfUser struct {
Bookmarks int `json:"bookmarks"`
Clients int `json:"clients"`
......@@ -24,15 +27,17 @@ type CountsOfUser struct {
Users int `json:"users"`
}
// Verified object definition
type Verified struct {
Domain string `json:"domain"`
Link string `json:"link"`
}
// User object definition
type User struct {
CreatedAt string `json:"created_at"`
Guid string `json:"guid"`
Id string `json:"id"`
GUID string `json:"guid"`
ID string `json:"id"`
Locale string `json:"locale"`
Timezone string `json:"timezon"`
Type string `json:"type"`
......@@ -48,12 +53,14 @@ type User struct {
Verified Verified `json:"verified"`
}
// Source object definition
type Source struct {
Name string `json:"name"`
Link string `json:"link"`
Id string `json:"id"`
ID string `json:"id"`
}
// CountsOfPost object definition
type CountsOfPost struct {
Bookmarks int `json:"bookmarks"`
Replies int `json:"replies"`
......@@ -61,21 +68,23 @@ type CountsOfPost struct {
Threads int `json:"threads"`
}
// ContentOfPost object definition
type ContentOfPost struct {
Text string `json:"text"`
Html string `json:"html"`
HTML string `json:"html"`
Entities Entities `json:"entities"`
LinksNotParsed bool `json:"links_not_parsed"`
}
// Post object definition
type Post struct {
CreatedAt string `json:"created_at"`
Guid string `json:"guid"`
Id string `json:"id"`
GUID string `json:"guid"`
ID string `json:"id"`
IsDeleted bool `json:"is_deleted"`
Source Source `json:"source"`
User User `json:"user"`
ThreadId string `json:"thread_id"`
ThreadID string `json:"thread_id"`
IsRevised bool `json:"is_revised"`
Revision string `json:"revision"`
ReplyTo string `json:"reply_to"`
......@@ -84,103 +93,117 @@ type Post struct {
Content ContentOfPost `json:"content"`
YouBookmarked bool `json:"you_bookmarked"`
YouReposted bool `json:"you_reposted"`
PaginationId string `json:"pagination_id"`
PaginationID string `json:"pagination_id"`
}
// Action object definition
type Action struct {
PaginationId string `json:"pagination_id"`
PaginationID string `json:"pagination_id"`
EventDate string `json:"event_date"`
Action string `json:"action"`
Users []User `json:"users"`
Objects []Post `json:"objects"`
}
// Presence object definition
type Presence struct {
Id string `json:"id"`
ID string `json:"id"`
LastSeenAt string `json:"last_seen_at"`
Presence string `json:"presence"`
}
// Full object definition
type Full struct {
Immutable bool `json:"immutable"`
You bool `json:"you"`
UserIds []string `json:"user_ids"`
}
// Write object definition
type Write struct {
*Full
AnyUser bool `json:"any_user"`
}
// Read object definition
type Read struct {
*Write
Public bool `json:"publicj"`
}
type Acl struct {
// ACL object definition
type ACL struct {
Full Full `json:"full"`
Write Write `json:"write"`
Read Read `json:"read"`
}
// CountsOfChannel object definition
type CountsOfChannel struct {
Messages int `json:"messages"`
Subscribers int `json:"subscribers"`
}
// Channel object definition
type Channel struct {
CreatedAt string `json:"created_at"`
Id string `json:"id"`
ID string `json:"id"`
Type string `json:"type"`
Owner User `json:"owner"`
Acl Acl `json:"acl"`
ACL ACL `json:"acl"`
Counts CountsOfChannel `json:"counts"`
YouSubscribed bool `json:"you_subscribed"`
YouMuted bool `json:"you_muted"`
HasUnread bool `json:"has_unread"`
PaginationId string `json:"pagination_id"`
PaginationID string `json:"pagination_id"`
}
// CountsOfMessage object definition
type CountsOfMessage struct {
Replies int `json:"replies"`
}
// ContentOfMessage object definition
type ContentOfMessage struct {
Html string `json:"html"`
HTML string `json:"html"`
Text string `json:"text"`
Entities Entities `json:"entities"`
}
// Message object defiition
type Message struct {
Id string `json:"id"`
ChannelId string `json:"channel_id"`
ID string `json:"id"`
ChannelID string `json:"channel_id"`
CreatedAt string `json:"created_at"`
Source Source `json:"source"`
IsDeleted bool `json:"is_deleted"`
ThreadId string `json:"thread_id"`
ThreadID string `json:"thread_id"`
User User `json:"user"`
Counts CountsOfMessage `json:"counts"`
Content ContentOfMessage `json:"content"`
PaginationId string `json:"pagination_id"`
PaginationID string `json:"pagination_id"`
}
// ContentOfClient object definition
type ContentOfClient struct {
*ContentOfMessage
}
// ClientInfo object definition
type ClientInfo struct {
CreatedAt string `json:"created_at"`
CreatedBy User `json:"created_by"`
Id string `json:"id"`
ID string `json:"id"`
Link string `json:"link"`
Name string `json:"name"`
Posts int `json:"posts"`
Content ContentOfClient `json:"content"`
}
// Marker object definition
type Marker struct {
Id string `json:"id"`
LastReadId string `json:"last_read_id"`
ID string `json:"id"`
LastReadID string `json:"last_read_id"`
Percentage int `json:"percentage"`
UpdatedAt string `json:"updated_at"`
Version string `json:"version"`
......
This diff is collapsed.
package woodstock
// PresencesResult response object
type PresencesResult struct {
*CommonResponse
Data []Presence `json:"data"`
}
// Get presences
// GetPresences retrieves precense
// https://pnut.io/docs/resources/users/presence#get-presence
func (c *Client) GetPresences() (result PresencesResult, err error) {
response_ch := make(chan response)
c.queryQueue <- query{url: PresenceAPI, data: &result, method: "GET", response_ch: response_ch}
return result, (<-response_ch).err
responseCh := make(chan response)
c.queryQueue <- query{url: PresenceAPI, data: &result, method: "GET", responseCh: responseCh}
return result, (<-responseCh).err
}
package woodstock
// MarkersResult response object
type MarkersResult struct {
*CommonResponse
Data []Marker `json:"data"`
}
// Set marker
// SetMarker sets the current read marker
// this func will be updated
// https://pnut.io/docs/resources/stream-marker
// https://pnut.io/docs/resources/stream-marker#post-markers
func (c *Client) SetMarker(json string) (result MarkersResult, err error) {
response_ch := make(chan response)
c.queryQueue <- query{url: MarkerAPI, data: &result, method: "PUT", response_ch: response_ch, json: json}
return result, (<-response_ch).err
responseCh := make(chan response)
c.queryQueue <- query{url: MarkerAPI, data: &result, method: "PUT", responseCh: responseCh, json: json}
return result, (<-responseCh).err
}
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment