~ubuntu-branches/ubuntu/saucy/juju-core/saucy-proposed

« back to all changes in this revision

Viewing changes to src/launchpad.net/lpad/person.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-07-11 17:18:27 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20130711171827-vjqkg40r0dlf7ys2
Tags: 1.11.2-0ubuntu1
* New upstream release.
* Make juju-core the default juju (LP: #1190634):
  - d/control: Add virtual package juju -> juju-core.
  - d/juju-core.postinst.in: Bump priority of alternatives over that of
    python juju packages.
* Enable for all architectures (LP: #1172505):
  - d/control: Version BD on golang-go to >= 2:1.1.1 to ensure CGO
    support for non-x86 archs, make juju-core Arch: any.
  - d/README.source: Dropped - no longer required.
* d/watch: Updated for new upstream tarball naming.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package lpad
 
2
 
 
3
import (
 
4
        "net/url"
 
5
)
 
6
 
 
7
// The Root type provides the entrance for the Launchpad API.
 
8
type Root struct {
 
9
        *Value
 
10
}
 
11
 
 
12
// Me returns the Person authenticated into Lauchpad in the current session.
 
13
func (root *Root) Me() (*Person, error) {
 
14
        me, err := root.Location("/people/+me").Get(nil)
 
15
        if err != nil {
 
16
                return nil, err
 
17
        }
 
18
        return &Person{me}, nil
 
19
}
 
20
 
 
21
// Member returns the Team or Person with the provided name or username.
 
22
func (root *Root) Member(name string) (Member, error) {
 
23
        v, err := root.Location("/~" + url.QueryEscape(name)).Get(nil)
 
24
        if err != nil {
 
25
                return nil, err
 
26
        }
 
27
        if v.BoolField("is_team") {
 
28
                return &Team{v}, nil
 
29
        }
 
30
        return &Person{v}, nil
 
31
}
 
32
 
 
33
// FindPeople returns a PersonList containing all Person accounts whose
 
34
// Name, DisplayName or email address match text.
 
35
func (root *Root) FindPeople(text string) (*PersonList, error) {
 
36
        v, err := root.Location("/people").Get(Params{"ws.op": "findPerson", "text": text})
 
37
        if err != nil {
 
38
                return nil, err
 
39
        }
 
40
        return &PersonList{v}, nil
 
41
}
 
42
 
 
43
// FindTeams returns a TeamList containing all Team accounts whose
 
44
// Name, DisplayName or email address match text.
 
45
func (root *Root) FindTeams(text string) (*TeamList, error) {
 
46
        v, err := root.Location("/people").Get(Params{"ws.op": "findTeam", "text": text})
 
47
        if err != nil {
 
48
                return nil, err
 
49
        }
 
50
        return &TeamList{v}, nil
 
51
}
 
52
 
 
53
// FindMembers returns a MemberList containing all Person or Team accounts
 
54
// whose Name, DisplayName or email address match text.
 
55
func (root *Root) FindMembers(text string) (*MemberList, error) {
 
56
        v, err := root.Location("/people").Get(Params{"ws.op": "find", "text": text})
 
57
        if err != nil {
 
58
                return nil, err
 
59
        }
 
60
        return &MemberList{v}, nil
 
61
}
 
62
 
 
63
// The MemberList type encapsulates a mixed list containing Person and Team
 
64
// elements for iteration.
 
65
type MemberList struct {
 
66
        *Value
 
67
}
 
68
 
 
69
// For iterates over the list of people and teams and calls f for each one.
 
70
// If f returns a non-nil error, iteration will stop and the error will be
 
71
// returned as the result of For.
 
72
func (list *MemberList) For(f func(v Member) error) error {
 
73
        return list.Value.For(func(v *Value) error {
 
74
                if v.BoolField("is_team") {
 
75
                        return f(&Team{v})
 
76
                }
 
77
                return f(&Person{v})
 
78
        })
 
79
}
 
80
 
 
81
// The PersonList type encapsulates a list of Person elements for iteration.
 
82
type PersonList struct {
 
83
        *Value
 
84
}
 
85
 
 
86
// For iterates over the list of people and calls f for each one.  If f
 
87
// returns a non-nil error, iteration will stop and the error will be
 
88
// returned as the result of For.
 
89
func (list *PersonList) For(f func(p *Person) error) error {
 
90
        return list.Value.For(func(v *Value) error {
 
91
                return f(&Person{v})
 
92
        })
 
93
}
 
94
 
 
95
// The TeamList type encapsulates a list of Team elements for iteration.
 
96
type TeamList struct {
 
97
        *Value
 
98
}
 
99
 
 
100
// For iterates over the list of teams and calls f for each one.  If f
 
101
// returns a non-nil error, iteration will stop and the error will be
 
102
// returned as the result of For.
 
103
func (list *TeamList) For(f func(t *Team) error) error {
 
104
        return list.Value.For(func(v *Value) error {
 
105
                return f(&Team{v})
 
106
        })
 
107
}
 
108
 
 
109
// Member is an interface implemented by both Person and Team.
 
110
type Member interface {
 
111
        AnyValue
 
112
 
 
113
        DisplayName() string
 
114
        SetDisplayName(name string)
 
115
        Name() string
 
116
        SetName(name string)
 
117
        WebPage() string
 
118
 
 
119
        // Member is a marker function for types satisfying the
 
120
        // the Member interface. This is necessary for now because 
 
121
        // the methods above are fairly common across several types,
 
122
        // but this will likely be dropped in the future.
 
123
        Member()
 
124
}
 
125
 
 
126
// The Person type represents a person in Launchpad.
 
127
type Person struct {
 
128
        *Value
 
129
}
 
130
 
 
131
var _ Member = (*Person)(nil)
 
132
 
 
133
// Member is a marker method so Person satisfies the Member interface.
 
134
func (person *Person) Member() {}
 
135
 
 
136
// DisplayName returns the person's name as it would be displayed
 
137
// throughout Launchpad.  Most people use their full name.
 
138
func (person *Person) DisplayName() string {
 
139
        return person.StringField("display_name")
 
140
}
 
141
 
 
142
// SetDisplayName changes the person's name as it would be displayed
 
143
// throughout Launchpad.  Most people use their full name.
 
144
// Patch must be called to commit all changes.
 
145
func (person *Person) SetDisplayName(name string) {
 
146
        person.SetField("display_name", name)
 
147
}
 
148
 
 
149
// Name returns the person's short unique name, beginning with a
 
150
// lower-case letter or number, and containing only letters, numbers,
 
151
// dots, hyphens, or plus signs.
 
152
func (person *Person) Name() string {
 
153
        return person.StringField("name")
 
154
}
 
155
 
 
156
// SetName changes the person's short unique name.
 
157
// The name must begin with a lower-case letter or number, and
 
158
// contain only letters, numbers, dots, hyphens, or plus signs.
 
159
func (person *Person) SetName(name string) {
 
160
        person.SetField("name", name)
 
161
}
 
162
 
 
163
// WebPage returns the URL for accessing this person's page in a browser.
 
164
func (person *Person) WebPage() string {
 
165
        return person.StringField("web_link")
 
166
}
 
167
 
 
168
// PreferredEmail returns the Person's preferred email. If the user
 
169
// disabled public access to email addresses, this method returns an
 
170
// *Error with StatusCode of 404.
 
171
func (person *Person) PreferredEmail() (string, error) {
 
172
        // WTF.. seriously!?
 
173
        e, err := person.Link("preferred_email_address_link").Get(nil)
 
174
        if err != nil {
 
175
                return "", err
 
176
        }
 
177
        return e.StringField("email"), nil
 
178
}
 
179
 
 
180
// IRCNicks returns a list of all IRC nicks for the person.
 
181
func (person *Person) IRCNicks() (nicks []*IRCNick, err error) {
 
182
        list, err := person.Link("irc_nicknames_collection_link").Get(nil)
 
183
        if err != nil {
 
184
                return nil, err
 
185
        }
 
186
        list.For(func(v *Value) error {
 
187
                nicks = append(nicks, &IRCNick{v})
 
188
                return nil
 
189
        })
 
190
        return
 
191
}
 
192
 
 
193
type IRCNick struct {
 
194
        *Value
 
195
}
 
196
 
 
197
// Nick returns the person's nick on an IRC network.
 
198
func (nick *IRCNick) Nick() string {
 
199
        return nick.StringField("nickname")
 
200
}
 
201
 
 
202
// SetNick changes the person's nick on an IRC network.
 
203
// Patch must be called to commit all changes.
 
204
func (nick *IRCNick) SetNick(n string) {
 
205
        nick.SetField("nickname", n)
 
206
}
 
207
 
 
208
// Network returns the IRC network this nick is associated to.
 
209
func (nick *IRCNick) Network() string {
 
210
        return nick.StringField("network")
 
211
}
 
212
 
 
213
// SetNetwork changes the IRC network this nick is associated to.
 
214
// Patch must be called to commit all changes.
 
215
func (nick *IRCNick) SetNetwork(n string) {
 
216
        nick.SetField("network", n)
 
217
}
 
218
 
 
219
// The Team type encapsulates access to details about a team in Launchpad.
 
220
type Team struct {
 
221
        *Value
 
222
}
 
223
 
 
224
var _ Member = (*Team)(nil)
 
225
 
 
226
// Member is a marker method so Team satisfies the Member interface.
 
227
func (team *Team) Member() {}
 
228
 
 
229
// Name returns the team's name.  This is a short unique name, beginning with a
 
230
// lower-case letter or number, and containing only letters, numbers, dots,
 
231
// hyphens, or plus signs.
 
232
func (team *Team) Name() string {
 
233
        return team.StringField("name")
 
234
}
 
235
 
 
236
// SetName changes the team's name.  This is a short unique name, beginning
 
237
// with a lower-case letter or number, and containing only letters, numbers,
 
238
// dots, hyphens, or plus signs.  Patch must be called to commit all changes.
 
239
func (team *Team) SetName(name string) {
 
240
        team.SetField("name", name)
 
241
}
 
242
 
 
243
// DisplayName returns the team's name as it would be displayed
 
244
// throughout Launchpad.
 
245
func (team *Team) DisplayName() string {
 
246
        return team.StringField("display_name")
 
247
}
 
248
 
 
249
// SetDisplayName changes the team's name as it would be displayed
 
250
// throughout Launchpad.  Patch must be called to commit all changes.
 
251
func (team *Team) SetDisplayName(name string) {
 
252
        team.SetField("display_name", name)
 
253
}
 
254
 
 
255
// WebPage returns the URL for accessing this team's page in a browser.
 
256
func (team *Team) WebPage() string {
 
257
        return team.StringField("web_link")
 
258
}