-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from doneill/feature/jdo-23-user-data
user command impl
- Loading branch information
Showing
8 changed files
with
281 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
// ---------------------------------------------- | ||
// stucts | ||
// ---------------------------------------------- | ||
|
||
type AuthResponse struct { | ||
AccessToken string `json:"access_token"` | ||
ExpiresIn int `json:"expires_in"` | ||
TokenType string `json:"token_type"` | ||
Scope string `json:"scope"` | ||
RefreshToken string `json:"refresh_token"` | ||
ErrorDescription string `json:"error_description"` | ||
} | ||
|
||
// ---------------------------------------------- | ||
// exported funtions | ||
// ---------------------------------------------- | ||
|
||
func Authenticate(sitename, username, password string) (*AuthResponse, error) { | ||
client := &http.Client{} | ||
authReq := getAuthRequest(sitename) | ||
authReq.Body = io.NopCloser(strings.NewReader(fmt.Sprintf("username=%s&password=%s&client_id=er_mobile_tracker&grant_type=password", username, password))) | ||
|
||
res, err := client.Do(&authReq) | ||
if err != nil { | ||
fmt.Println("Error making request:", err) | ||
} | ||
defer res.Body.Close() | ||
|
||
var responseData AuthResponse | ||
err = json.NewDecoder(res.Body).Decode(&responseData) | ||
if err != nil { | ||
fmt.Println("Error decoding response:", err) | ||
} | ||
|
||
if res.StatusCode == 200 { | ||
return &responseData, nil | ||
} | ||
|
||
fmt.Println("Error:", res.StatusCode) | ||
fmt.Println("Error Description:", responseData.ErrorDescription) | ||
|
||
return nil, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/doneill/er-cli-go/config" | ||
) | ||
|
||
// ---------------------------------------------- | ||
// stucts | ||
// ---------------------------------------------- | ||
|
||
type UserResponse struct { | ||
Data struct { | ||
Username string `json:"username"` | ||
Email string `json:"email"` | ||
FirstName string `json:"first_name"` | ||
LastName string `json:"last_name"` | ||
Role string `json:"role"` | ||
IsStaff bool `json:"is_staff"` | ||
IsSuperUser bool `json:"is_superuser"` | ||
DateJoined string `json:"date_joined"` | ||
ID string `json:"id"` | ||
IsActive bool `json:"is_active"` | ||
LastLogin string `json:"last_login"` | ||
Pin string `json:"pin"` | ||
Subject struct { | ||
ID string `json:"id"` | ||
} `json:"subject"` | ||
Permissions struct { | ||
Patrol []string `json:"patrol"` | ||
MobileTests []string `json:"mobile_tests"` | ||
} `json:"permissions"` | ||
} `json:"data"` | ||
ErrorDescription string `json:"error_description"` | ||
Status struct { | ||
Code int `json:"code"` | ||
Message string `json:"message"` | ||
} `json:"status"` | ||
} | ||
|
||
// ---------------------------------------------- | ||
// exported funtions | ||
// ---------------------------------------------- | ||
|
||
func User() (*UserResponse, error) { | ||
client := &http.Client{} | ||
clientReq := getClientRequest(config.Sitename(), API_USER_ME, config.Token()) | ||
|
||
res, err := client.Do(&clientReq) | ||
if err != nil { | ||
fmt.Println("Error making request:", err) | ||
} | ||
var responseData UserResponse | ||
err = json.NewDecoder(res.Body).Decode(&responseData) | ||
if err != nil { | ||
fmt.Println("Error decoding response:", err) | ||
} | ||
|
||
if res.StatusCode == 200 { | ||
return &responseData, nil | ||
} | ||
|
||
fmt.Println("Error:", res.StatusCode) | ||
fmt.Println("Error Description:", responseData.ErrorDescription) | ||
|
||
return nil, err | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package api | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
// ---------------------------------------------- | ||
// const endpoints | ||
// ---------------------------------------------- | ||
|
||
const DOMAIN = ".pamdas.org" | ||
|
||
const API_V1 = "/api/v1.0" | ||
|
||
const API_AUTH = "/oauth2/token" | ||
|
||
const API_USER = API_V1 + "/user" | ||
|
||
const API_USER_ME = API_USER + "/me" | ||
|
||
// ---------------------------------------------- | ||
// funtions | ||
// ---------------------------------------------- | ||
|
||
func getApiUrl(sitename string, endpoint string) string { | ||
return fmt.Sprintf("https://%s%s%s", sitename, DOMAIN, endpoint) | ||
} | ||
|
||
func getAuthRequest(sitename string) http.Request { | ||
req, err := http.NewRequest("POST", getApiUrl(sitename, API_AUTH), nil) | ||
if err != nil { | ||
fmt.Println("Error creating request:", err) | ||
} | ||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | ||
req.Header.Set("Accept", "application/json") | ||
|
||
return *req | ||
} | ||
|
||
func getClientRequest(sitename string, endpoint string, token string) http.Request { | ||
req, err := http.NewRequest("GET", getApiUrl(sitename, endpoint), nil) | ||
if err != nil { | ||
fmt.Println("Error creating request:", err) | ||
} | ||
req.Header.Set("Authorization", "Bearer "+token) | ||
req.Header.Set("Cache-control", "no-cache") | ||
|
||
return *req | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/doneill/er-cli-go/api" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// ---------------------------------------------- | ||
// user command | ||
// ---------------------------------------------- | ||
|
||
var userCmd = &cobra.Command{ | ||
Use: "user", | ||
Short: "Current authenticated user data", | ||
Long: `Return the currently authenticated er user data`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
user() | ||
}, | ||
} | ||
|
||
// ---------------------------------------------- | ||
// functions | ||
// ---------------------------------------------- | ||
|
||
func user() { | ||
userResponse, err := api.User() | ||
if err != nil { | ||
fmt.Println("Error authenticating:", err) | ||
os.Exit(1) | ||
} | ||
|
||
if userResponse != nil { | ||
formattedResponse := fmt.Sprintf("username: %s\nemail: %s\nfirst name: %s\nlast name: %s\nrole: %s\nis staff: %t\nis superuser: %t\ndate joined: %s\nid: %s\nisactive: %t\nlast login: %s\npin: %s\nsubject id: %s\npermissions:\n patrol: %v\nmobile tests: %v", | ||
userResponse.Data.Username, | ||
userResponse.Data.Email, | ||
userResponse.Data.FirstName, | ||
userResponse.Data.LastName, | ||
userResponse.Data.Role, | ||
userResponse.Data.IsStaff, | ||
userResponse.Data.IsSuperUser, | ||
userResponse.Data.DateJoined, | ||
userResponse.Data.ID, | ||
userResponse.Data.IsActive, | ||
userResponse.Data.LastLogin, | ||
userResponse.Data.Pin, | ||
userResponse.Data.Subject.ID, | ||
userResponse.Data.Permissions.Patrol, | ||
userResponse.Data.Permissions.MobileTests) | ||
fmt.Println(formattedResponse) | ||
} | ||
} | ||
|
||
// ---------------------------------------------- | ||
// initialize | ||
// ---------------------------------------------- | ||
|
||
func init() { | ||
rootCmd.AddCommand(userCmd) | ||
} |
Oops, something went wrong.