Skip to content

Add session so you can log in and get token #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
* [repositories](https://github.com/plouc/go-gitlab-client/tree/master/examples/repositories)
54 changes: 54 additions & 0 deletions session.go
Original file line number Diff line number Diff line change
@@ -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
}
32 changes: 32 additions & 0 deletions session_test.go
Original file line number Diff line number Diff line change
@@ -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()
}
20 changes: 20 additions & 0 deletions stubs/session/new.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"id": 1,
"username": "john_smith",
"email": "[email protected]",
"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
}