diff --git a/README.md b/README.md index 7f2b2f7..8c6e3dd 100644 --- a/README.md +++ b/README.md @@ -32,8 +32,11 @@ go-gitlab-client is a simple client written in golang to consume gitlab API. * list project deploy keys * add/get/rm project deploy key +* + ###Session [gitlab api doc](http://api.gitlab.org/session.html) + * create session - +Note: In most cases, OAuth2 is the preferred method of authentication, and you can find GitLab OAuth2 documentation [here](http://api.gitlab.org/oauth2.html). Access to the GitLab Session API is provided as a convenience when OAuth2 doesn't fit your use case. ##Installation @@ -69,4 +72,4 @@ Visit the docs at http://godoc.org/github.com/plouc/go-gitlab-client You can play with the examples located in the `examples` directory * [projects](https://github.com/plouc/go-gitlab-client/tree/master/examples/projects) -* [repositories](https://github.com/plouc/go-gitlab-client/tree/master/examples/repositories) \ No newline at end of file +* [repositories](https://github.com/plouc/go-gitlab-client/tree/master/examples/repositories) diff --git a/session.go b/session.go new file mode 100644 index 0000000..23bb3f1 --- /dev/null +++ b/session.go @@ -0,0 +1,54 @@ +package gogitlab + +import ( + "encoding/json" + "net/url" +) + +const ( + session_url = "/session" // Create a new session +) + +type Session struct { + Id int `json:"id,omitempty"` + Username string `json:"username,omitempty"` + Email string `json:"email,omitempty"` + Name string `json:"name,omitempty"` + PrivateToken string `json:"private_token,omitempty"` + Blocked bool `json:"blocked,omitempty"` + CreatedAt string `json:"created_at,omitempty"` + Bio string `json:"bio,omitempty"` + Skype string `json:"skype,omitempty"` + LinkedIn string `json:"linkedin,omitempty"` + Twitter string `json:"twitter,omitempty"` + WebsiteUrl string `json:"website_url,omitempty"` + DarkScheme bool `json:"dark_scheme,omitempty"` + ThemeId int `json:"theme_id,omitempty"` + IsAdmin bool `json:"is_admin,omitempty"` + CanCreateGroup bool `json:"can_create_group,omitempty"` + CanCreateTeam bool `json:"can_create_team,omitempty"` + CanCreateProject bool `json:"can_create_project,omitempty"` +} + +func (g *Gitlab) NewSession(login string, email string, password string) (*Session, error) { + path := g.ResourceUrl(session_url, nil) + + v := url.Values{} + if login != "" { + v.Set("login", login) + } + if email != "" { + v.Set("email", email) + } + v.Set("password", password) + + body := v.Encode() + + var session *Session + contents, err := g.buildAndExecRequest("POST", path, []byte(body)) + if err == nil { + err = json.Unmarshal(contents, &session) + } + + return session, err +} diff --git a/session_test.go b/session_test.go new file mode 100644 index 0000000..4f9c69a --- /dev/null +++ b/session_test.go @@ -0,0 +1,32 @@ +package gogitlab + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestNewSession(t *testing.T) { + ts, gitlab := Stub("stubs/session/new.json") + session, err := gitlab.NewSession("john_smith", "", "pw") + + assert.Equal(t, err, nil) + assert.IsType(t, new(Session), session) + assert.Equal(t, session.Id, 1) + assert.Equal(t, session.Username, "john_smith") + assert.Equal(t, session.Name, "John Smith") + assert.Equal(t, session.PrivateToken, "dd34asd13as") + assert.Equal(t, session.Blocked, false) + assert.Equal(t, session.CreatedAt, "2012-05-23T08:00:58Z") + assert.Equal(t, session.Bio, "") + assert.Equal(t, session.Skype, "") + assert.Equal(t, session.LinkedIn, "") + assert.Equal(t, session.Twitter, "") + assert.Equal(t, session.ThemeId, 1) + assert.Equal(t, session.DarkScheme, false) + assert.Equal(t, session.IsAdmin, false) + assert.Equal(t, session.CanCreateGroup, true) + assert.Equal(t, session.CanCreateTeam, true) + assert.Equal(t, session.CanCreateProject, true) + defer ts.Close() +} diff --git a/stubs/session/new.json b/stubs/session/new.json new file mode 100644 index 0000000..39bc123 --- /dev/null +++ b/stubs/session/new.json @@ -0,0 +1,20 @@ +{ + "id": 1, + "username": "john_smith", + "email": "john@example.com", + "name": "John Smith", + "private_token": "dd34asd13as", + "blocked": false, + "created_at": "2012-05-23T08:00:58Z", + "bio": null, + "skype": "", + "linkedin": "", + "twitter": "", + "website_url": "", + "dark_scheme": false, + "theme_id": 1, + "is_admin": false, + "can_create_group": true, + "can_create_team": true, + "can_create_project": true +}