appnet

package module
v0.0.0-...-4ac9010 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 30, 2014 License: GPL-3.0 Imports: 11 Imported by: 0

README

appnet

Go interface to the App.net API - forked from github.com/whee/adn

Now known as bitbucket.com/hybotics/go-appnet

Installation

The usual:

$ go get bitbucket.org/hybotics/appnet

Package documentation

/* godoc output courtesy of GoPkgDoc */

Example usage

getpost.go:

package main

import (
	"flag"
	"fmt"
	"bitbucket.org/hybotics/appnet"
	"log"
	"strconv"
)

var post = flag.Int("post", 1, "post id")

func main() {
	flag.Parse()

	app := &adn.Application{}
	post, err := app.GetPost("", strconv.Itoa(*post)) // unauthenticated request
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("%s [%v]\n%s\n", post.User.Username, post.CreatedAt, post.Text)
}

Try it out:

$ go run getpost.go -post=1
mthurman [2012-08-03 03:59:06 +0000 UTC]
join.app.net getting ready for the world w/ @dalton @berg @voidfiles @jhubball @aaronblyth @andrew @vinitlee @mark @mintz @barmstrong @laughingman @mikegreenspan @ben #joinus

Documentation

Overview

	Original Copyright and Attribution:

	Copyright (c) 2012 Brian Hetro <[email protected]>
		Use of this source code is governed by the ISC
		license that can be found in the LICENSE file.

	Original Copyright and Attribution:

	Copyright (c) 2012 Brian Hetro <[email protected]>
		Use of this source code is governed by the ISC
		license that can be found in the LICENSE file.

	Original Copyright and Attribution:

	Copyright (c) 2012 Brian Hetro <[email protected]>
		Use of this source code is governed by the ISC
		license that can be found in the LICENSE file.

	Original Copyright and Attribution:

	Copyright (c) 2012 Brian Hetro <[email protected]>
		Use of this source code is governed by the ISC
		license that can be found in the LICENSE file.

	Original Copyright and Attribution:

	Copyright (c) 2012 Brian Hetro <[email protected]>
		Use of this source code is governed by the ISC
		license that can be found in the LICENSE file.

	Original Copyright and Attribution:

	Copyright (c) 2012 Brian Hetro <[email protected]>
		Use of this source code is governed by the ISC
		license that can be found in the LICENSE file.

	Original Copyright and Attribution:

	Copyright (c) 2012 Brian Hetro <[email protected]>
		Use of this source code is governed by the ISC
		license that can be found in the LICENSE file.

    Original Copyright and Attribution:

    Copyright (c) 2012 Brian Hetro <[email protected]>
        Use of this source code is governed by the ISC
        license that can be found in the LICENSE file.

	Original Copyright and Attribution:

	Copyright (c) 2012 Brian Hetro <[email protected]>
		Use of this source code is governed by the ISC
		license that can be found in the LICENSE file.

Index

Constants

View Source
const (
	ScopeBasic     = "basic"
	ScopeStream    = "stream"
	ScopeEmail     = "email"
	ScopeWritePost = "write_post"
	ScopeFollow    = "follow"
	ScopeMessages  = "messages"
	ScopeExport    = "export"
)

Variables

View Source
var ApiEndpoints = map[string]endpoint{

	"retrieve user":        {httpGet, apiHost + "/stream/0/users/{{.User}}", nil},
	"follow user":          {httpPost, apiHost + "/stream/0/users/{{.User}}/follow", nil},
	"unfollow user":        {httpDelete, apiHost + "/stream/0/users/{{.User}}/follow", nil},
	"list followed users":  {httpGet, apiHost + "/stream/0/users/{{.User}}/following", nil},
	"list following users": {httpGet, apiHost + "/stream/0/users/{{.User}}/followers", nil},
	"mute user":            {httpPost, apiHost + "/stream/0/users/{{.User}}/mute", nil},
	"unmute user":          {httpDelete, apiHost + "/stream/0/users/{{.User}}/mute", nil},
	"list muted users":     {httpGet, apiHost + "/stream/0/users/me/muted", nil},
	"search for users":     {httpGet, apiHost + "/stream/0/users/search", nil},
	"list reposters":       {httpGet, apiHost + "/stream/0/posts/{{.Post}}/reposters", nil},
	"list starrers":        {httpGet, apiHost + "/stream/0/posts/{{.Post}}/star", nil},

	"check current token": {httpGet, apiHost + "/stream/0/token", nil},

	"create post":                 {httpPost, apiHost + "/stream/0/posts", nil},
	"retrieve post":               {httpGet, apiHost + "/stream/0/posts/{{.Post}}", nil},
	"delete post":                 {httpDelete, apiHost + "/stream/0/posts/{{.Post}}", nil},
	"retrieve post replies":       {httpGet, apiHost + "/stream/0/posts/{{.Post}}/replies", nil},
	"retrieve user posts":         {httpGet, apiHost + "/stream/0/users/{{.User}}/posts", nil},
	"repost post":                 {httpPost, apiHost + "/stream/0/posts/{{.Post}}/repost", nil},
	"unrepost post":               {httpDelete, apiHost + "/stream/0/posts/{{.Post}}/repost", nil},
	"star post":                   {httpPost, apiHost + "/stream/0/posts/{{.Post}}/star", nil},
	"unstar post":                 {httpDelete, apiHost + "/stream/0/posts/{{.Post}}/star", nil},
	"retrieve user starred posts": {httpGet, apiHost + "/stream/0/users/{{.User}}/stars", nil},
	"retrieve tagged posts":       {httpGet, apiHost + "/stream/0/posts/tag/{{.Hashtag}}", nil},

	"retrieve global stream":                     {httpGet, apiHost + "/stream/0/posts/stream/global", nil},
	"retrieve public stream":                     {httpGet, apiHost + "/stream/0/streams/public", nil},
	"retrieve multiple user personalized stream": {httpGet, apiHost + "/stream/0/streams/app", nil},
	"retrieve user personalized stream":          {httpGet, apiHost + "/stream/0/posts/stream", nil},
	"retrieve user posts stream":                 {httpGet, apiHost + "/stream/0/users/{{.User}}/posts", nil},
	"retrieve user unified stream":               {httpGet, apiHost + "/stream/0/posts/stream/unified", nil},
	"retrieve user mentions stream":              {httpGet, apiHost + "/stream/0/users/{{.User}}/mentions", nil},
	"retrieve stream status":                     {httpGet, apiHost + "/stream/0/streams/{{.Stream}}", nil},
	"control stream":                             {httpPost, apiHost + "/stream/0/streams/{{.Stream}}", nil},

	"list subscriptions":       {httpGet, apiHost + "/stream/0/subscriptions", nil},
	"create subscription":      {httpPost, apiHost + "/stream/0/subscriptions", nil},
	"delete subscription":      {httpDelete, apiHost + "/stream/0/subscriptions/{{.Subscription}}", nil},
	"delete all subscriptions": {httpDelete, apiHost + "/stream/0/subscriptions", nil},

	"retrieve current user filters": {httpGet, apiHost + "/stream/0/filters", nil},
	"create filter":                 {httpPost, apiHost + "/stream/0/filters", nil},
	"retrieve filter":               {httpGet, apiHost + "/stream/0/filters/{{.Filter}}", nil},
	"delete filter":                 {httpGet, apiHost + "/stream/0/filters/{{.Filter}}", nil},

	"authentication url": {httpGet, apiAuthHost + "/oauth/authenticate?client_id={{urlquery .Id}}&response_type=code&redirect_uri={{urlquery .RedirectURI}}&scope={{urlquery .Scopes.Spaced}}{{if .State}}&state={{urlquery .State}}{{end}}", nil},
	"get access token":   {httpPost, apiAuthHost + "/oauth/access_token", &epOptions{ResponseEnvelope: false}},
}
View Source
var DefaultApplication = &Application{}

Functions

func Do

func Do(req *Request, name string, args EpArgs, v interface{}) (err error)

Do handles all API requests.

The Request contains the authentication token and optional body.

The name comes from ApiEndpoints, with template arguments

provided in args.

The response is unpacked into v.

In the future, you would not call this function directly, but

instead use this helper function for the specific action.

Types

type APIError

type APIError responseMeta

Represents errors returned by ADN as a result of an API call.

func (APIError) Error

func (e APIError) Error() string

type Application

type Application struct {
	Id             string
	Name           string
	Date           string
	Version        string
	Windowed       bool
	Secret         string
	RedirectURI    string
	Scopes         Scopes
	PasswordSecret string
	Token          string // Access token, Added, DAW, 06-Nov-2013
	UserName       string // User name, Added, DAW, 06-Nov-2013
	UserId         string // User ID, Added, DAW, 06-Nov-2013
}

func (*Application) AccessToken

func (c *Application) AccessToken(code string) (string, error)

During server-side flow, the user will be redirected back with a

code.

AccessToken() uses this code to request an access token for the

user, which is returned as a string.

func (*Application) AuthenticationURL

func (c *Application) AuthenticationURL(state string) (string, error)

Generate the authentication URL for the server-side flow.

func (*Application) GetPost

func (c *Application) GetPost(id string) (p Post, err error)

Retrieve the post specified by id using token as authentication.

func (*Application) GetUser

func (c *Application) GetUser(idname string) (u User, err error)

Retrieve the user specified by idname.

** Works **

func (*Application) PasswordFlowAuthenticator

func (c *Application) PasswordFlowAuthenticator(userName, userPassword string, appScope Scopes) (aToken string, user User, err error)

Authenticate a user via the Password Flow method

Returns: Access Token and User ID in app

func (*Application) PasswordToken

func (c *Application) PasswordToken(userName, userPassword string) (aToken string, err error)

PasswordToken is used to carry out the password flow. The function

submits the username and password to get an access token. This
token is returned as a string.

** Works **

type Counts

type Counts struct {
	Following int `json:"following"` // The number of users this user is following.
	Followers int `json:"followers"` // The number of users following this user.
	Posts     int `json:"posts"`     // The number of posts created by this user.
	Stars     int `json:"stars"`     // The number of posts starred by this user.
}

type Description

type Description struct {
	Text     string   `json:"text"` // Biographical information
	HTML     string   `json:"html"` // Server-generated annotated HTML version of Text.
	Entities Entities `json:"entities"`
}

type Entities

type Entities struct {
	Mentions []Mention
	Hashtags []Hashtag
	Links    []Link
}

type EpArgs

type EpArgs struct {
	User, Post, Hashtag, Stream, Subscription, Filter string
}

These fields are used to fill the endpoint template.

type Filter

type Filter struct {
	Type           string   `json:"type"`             // Either "show" or "block" for whether this filter should exclude everything except for what's shown or show everything except for what's blocked.
	Name           string   `json:"name"`             // A User assigned name for this filter.
	UserIds        []string `json:"user_ids"`         // A slice of user ids a Post must or must not be created by.
	Hashtags       []string `json:"hashtags"`         // A slice of hashtags a Post must or must not have.
	LinkDomains    []string `json:"link_domains"`     // A slice of domains a Post must or must not have a link to.
	MentionUserIds []string `json:"mention_user_ids"` // A slice of user ids a Post must or must not mention.
}

type Hashtag

type Hashtag struct {
	Name string `json:"name"` // The name of the hashtag (not including '@').
	Pos  int    `json:"pos"`  // The 0-based index where the entity includes Text (includes '#').
	Len  int    `json:"len"`  // The length of the substring in Text.
}

type Image

type Image struct {
	Height int    `json:"height"`
	Width  int    `json:"width"`
	Url    string `json:"url"`
}
type Link struct {
	Text string `json:"text"` // The anchor text to be linked.
	Url  string `json:"url"`  // The destination url.
	Pos  int    `json:"pos"`  // The 0-based index where the entity begins in Text.
	Len  int    `json:"len"`  // The length of the substring in Text that represents this link.
}

type Mention

type Mention struct {
	Name string `json:"name"` // The username being mentioned (not including '@').
	Id   string `json:"id"`   // The user id of the mentioned user.
	Pos  int    `json:"pos"`  // The 0-based index where the entity includes Text (includes '@').
	Len  int    `json:"len"`  // The length of the substring in Text.
}

type Post

type Post struct {
	Id   string `json:"id"`
	User User   `json:"user"` // Embedded version of the User object. May not exist if the account has since been deleted.
	//	CreatedAt    string    `json:"created_at"` // The time at which the post was created.
	CreatedAt    time.Time `json:"created_at"` // The time at which the post was created.
	Text         string    `json:"text"`       // User supplied text of the post.
	HTML         string    `json:"html"`       // Server-generated annotated HTML rendering of the post text.
	Source       Source    `json:"source"`
	ReplyTo      string    `json:"reply_to"`      // Id of the post this post is replying to (or "" if not a reply).
	ThreadId     string    `json:"thread_id"`     // Id of the post at the root of the thread this post is a part of.
	CanonicalURL string    `json:"canonical_url"` // The URL of the post's detail page on Alpha.

	NumReplies int `json:"num_replies"` // The number of posts created in reply to this post.
	NumStars   int `json:"num_stars"`   // The number of users who have starred this post.
	NumReposts int `json:"num_reposts"` // The number of users who have reposted this post.

	Annotations []interface{} `json:"annotations"` // TODO implement

	Entities Entities `json:"entities"`

	IsDeleted   bool `json:"is_deleted"`   // Has this post been deleted?
	MachineOnly bool `json:"machine_only"` // Is this post meant for machines or humans?

	YouStarred bool     `json:"you_starred"` // Has the current user starred this post?
	StarredBy  []string `json:"starred_by"`  // A partial list of users who have starred this post. (TODO verify)

	YouReposted bool     `json:"you_reposted"` // Has the current user reposted this post?
	Reposters   []string `json:"reposters"`    // A partial list of users who have reposted this post. (TODO verify)
	RepostOf    *Post    `json:"repost_of"`    // If this post is a repost, the original post. (TODO verify self-reference)
}

func GetPost

func GetPost(id string) (Post, error)

Calls GetPost on the DefaultApplication.

type Request

type Request struct {
	Token    string    // Authentication token for the user or ""
	Body     io.Reader // Data for the body
	BodyType string    // Value for the Content-Type header
}

type ResponseMeta

type ResponseMeta struct {
	Code         int    `json:"code"`
	ErrorId      string `json:"error_id"`
	ErrorMessage string `json:"error_message"`
	ErrorSlug    string `json:"error_slug"`

	MaxId string `json:"max_id"`
	MinId string `json:"min_id"`
	More  bool   `json:"more"`

	Marker StreamMarker `json:"marker"`
}

type Scopes

type Scopes []string

func (Scopes) Spaced

func (s Scopes) Spaced() string

func (Scopes) String

func (s Scopes) String() string

type Source

type Source struct {
	Name string `json:"name"` // Description of the API consumer that created this post.
	Link string `json:"link"` // Link provided by the API consumer that created this post.
}

type Stream

type Stream struct {
	PostCount int
	// contains filtered or unexported fields
}

func (*Stream) GetNextPost

func (s *Stream) GetNextPost() (post Post, nr int, err error)

Get the next post from the current block of posts. If there are

no more posts, try to read in another block of posts.

func (*Stream) Init

func (s *Stream) Init(app Application, streamName string)

func (*Stream) NextPost

func (s *Stream) NextPost() (post Post, err error)

func (*Stream) Read

func (s *Stream) Read() (n int, err error)

Read a set of posts from the stream

Sets: s.posts, s.postCount, s.index, s.more, s.min_id,

and s.max_id

Returns: number of posts read, or -1 if no new posts were read

func (*Stream) SetArgs

func (s *Stream) SetArgs(args EpArgs)

func (*Stream) SetUrlValues

func (s *Stream) SetUrlValues(values url.Values, init bool) (err error)

type StreamMarker

type StreamMarker struct {
	Name       string `json:"name"`
	UpdatedAt  string `json:"updated_at"`
	Version    string `json:"version"`
	LastReadId string `json:"last_read_id"`
	Percentage int    `json:"percentage"`
	Id         string `json:"id"`
}

type StreamPosts

type StreamPosts []Post

type StreamResponse

type StreamResponse struct {
	Data  StreamPosts  `json:"data"`
	Meta  ResponseMeta `json:"meta"`
	Error string
}

Stream response information

type User

type User struct {
	Id          string      `json:"id"`
	Username    string      `json:"username"`
	Name        string      `json:"name"` // The user-supplied Name may be a pseudonym.
	Description Description `json:"description"`
	Timezone    string      `json:"timezone"`     // The Timezone is in tzinfo format.
	Locale      string      `json:"locale"`       // The Locale is in ISO format.
	AvatarImage Image       `json:"avatar_image"` // The URL and original size of the user's avatar.
	CoverImage  Image       `json:"cover_image"`  // The URL and original size of the user's cover image.
	Type        string      `json:"type"`         // An account can be human, bot, corporate, or feed.
	CreatedAt   time.Time   `json:"created_at"`   // The time at which the User was created in ISO 8601 format.
	Counts      Counts      `json:"counts"`
	AppData     interface{} `json:"app_data"`    // TODO: Opaque information an application may store.
	FollowsYou  bool        `json:"follows_you"` // Does this user follow the user making the request? May be omitted if this is not an authenticated request.
	YouFollow   bool        `json:"you_follow"`  // Does this user making the request follow this user? May be omitted if this is not an authenticated request.
	YouMuted    bool        `json:"you_muted"`   // Has the user making the request blocked this user? May be omitted if this is not an authenticated request.
}

func GetUser

func GetUser(idname string) (User, error)

Calls GetUser on the DefaultApplication.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL