Skip to content

Commit

Permalink
Adding the me endpoints with its respective unit tests (#57)
Browse files Browse the repository at this point in the history
* Adding the me endpoints with its respective unit tests

* Fixing comment error

* Add newline

Co-authored-by: Shubham Pandey <[email protected]>
  • Loading branch information
fear-the-reaper and sp35 authored Sep 5, 2022
1 parent d15a363 commit 598ed60
Show file tree
Hide file tree
Showing 3 changed files with 394 additions and 0 deletions.
10 changes: 10 additions & 0 deletions gointelowl/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type IntelOwlClient struct {
JobService *JobService
AnalyzerService *AnalyzerService
ConnectorService *ConnectorService
UserService *UserService
Logger *IntelOwlLogger
}

Expand Down Expand Up @@ -139,15 +140,20 @@ func NewIntelOwlClient(options *IntelOwlClientOptions, httpClient *http.Client,
timeout = time.Duration(options.Timeout) * time.Second
}

// configuring the http.Client
if httpClient == nil {
httpClient = &http.Client{
Timeout: timeout,
}
}

// configuring the client
client := IntelOwlClient{
options: options,
client: httpClient,
}

// Adding the services
client.TagService = &TagService{
client: &client,
}
Expand All @@ -160,7 +166,11 @@ func NewIntelOwlClient(options *IntelOwlClientOptions, httpClient *http.Client,
client.ConnectorService = &ConnectorService{
client: &client,
}
client.UserService = &UserService{
client: &client,
}

// configuring the logger!
client.Logger = &IntelOwlLogger{}
client.Logger.Init(loggerParams)

Expand Down
214 changes: 214 additions & 0 deletions gointelowl/me.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
package gointelowl

import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"time"
)

type Details struct {
Username string `json:"username"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
FullName string `json:"full_name"`
Email string `json:"email"`
}

type AccessDetails struct {
TotalSubmissions int `json:"total_submissions"`
MonthSubmissions int `json:"month_submissions"`
}

type User struct {
User Details `json:"user"`
Access AccessDetails `json:"access"`
}

type UserService struct {
client *IntelOwlClient
}

type Owner struct {
Username string `json:"username"`
FullName string `json:"full_name"`
Joined time.Time `json:"joined"`
}

type Organization struct {
MembersCount int `json:"members_count"`
Owner Owner `json:"owner"`
IsUserOwner bool `json:"is_user_owner,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
Name string `json:"name"`
}

type OrganizationParams struct {
Name string `json:"name"`
}

type MemberParams struct {
Username string `json:"username"`
}

type Invite struct {
Id int `json:"id"`
CreatedAt time.Time `json:"created_at"`
Status string `json:"status"`
}

type Invitation struct {
Invite
Organization Organization `json:"organization"`
}

type InvitationParams struct {
Organization OrganizationParams `json:"organization"`
Status string `json:"status"`
}

// Access retrieves user details
//
// Endpoint: GET /api/me/access
//
// IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/me/operation/me_access_retrieve
func (userService *UserService) Access(ctx context.Context) (*User, error) {
requestUrl := fmt.Sprintf("%s/api/me/access", userService.client.options.Url)
contentType := "application/json"
method := "GET"
request, err := userService.client.buildRequest(ctx, method, contentType, nil, requestUrl)
if err != nil {
return nil, err
}
user := User{}
successResp, err := userService.client.newRequest(ctx, request)
if err != nil {
return nil, err
}
if unmarshalError := json.Unmarshal(successResp.Data, &user); unmarshalError != nil {
return nil, unmarshalError
}
return &user, nil
}

// Organization returns the organization's details.
//
// Endpoint: GET /api/me/organization
//
// IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/me/operation/me_organization_list
func (userService *UserService) Organization(ctx context.Context) (*Organization, error) {
requestUrl := fmt.Sprintf("%s/api/me/organization", userService.client.options.Url)
contentType := "application/json"
method := "GET"
request, err := userService.client.buildRequest(ctx, method, contentType, nil, requestUrl)
if err != nil {
return nil, err
}

org := Organization{}
successResp, err := userService.client.newRequest(ctx, request)
if err != nil {
return nil, err
}
if unmarshalError := json.Unmarshal(successResp.Data, &org); unmarshalError != nil {
return nil, unmarshalError
}
return &org, nil
}

// CreateOrganization allows you to create a super cool organization!
//
// Endpoint: POST /api/me/organization
//
// IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/me/operation/me_organization_create
func (userService *UserService) CreateOrganization(ctx context.Context, organizationParams *OrganizationParams) (*Organization, error) {
requestUrl := fmt.Sprintf("%s/api/me/organization", userService.client.options.Url)
// Getting the relevant JSON data
orgJson, err := json.Marshal(organizationParams)
if err != nil {
return nil, err
}
contentType := "application/json"
method := "POST"
body := bytes.NewBuffer(orgJson)
request, err := userService.client.buildRequest(ctx, method, contentType, body, requestUrl)
if err != nil {
return nil, err
}

org := Organization{}
successResp, err := userService.client.newRequest(ctx, request)
if err != nil {
return nil, err
}
if unmarshalError := json.Unmarshal(successResp.Data, &org); unmarshalError != nil {
return nil, unmarshalError
}
return &org, nil
}

// InviteToOrganization allows you to invite someone to your super cool organization!
// This is only accessible to the organization's owner.
//
// Endpoint: POST /api/me/organization/invite
//
// IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/me/operation/me_organization_invite_create
func (userService *UserService) InviteToOrganization(ctx context.Context, memberParams *MemberParams) (*Invite, error) {
requestUrl := fmt.Sprintf("%s/api/me/organization/invite", userService.client.options.Url)
// Getting the relevant JSON data
memberJson, err := json.Marshal(memberParams)
if err != nil {
return nil, err
}
contentType := "application/json"
method := "POST"
body := bytes.NewBuffer(memberJson)
request, err := userService.client.buildRequest(ctx, method, contentType, body, requestUrl)
if err != nil {
return nil, err
}

invite := Invite{}
successResp, err := userService.client.newRequest(ctx, request)
if err != nil {
return nil, err
}
if unmarshalError := json.Unmarshal(successResp.Data, &invite); unmarshalError != nil {
return nil, unmarshalError
}
return &invite, nil
}

// RemoveMemberFromOrganization lets you remove someone from your super cool organization! (you had your reasons)
// This is only accessible to the organization's owner.
//
// Endpoint: POST /api/me/organization/remove_member
//
// IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/me/operation/me_organization_create
func (userService *UserService) RemoveMemberFromOrganization(ctx context.Context, memberParams *MemberParams) (bool, error) {
requestUrl := fmt.Sprintf("%s/api/me/organization/remove_member", userService.client.options.Url)
// Getting the relevant JSON data
memberJson, err := json.Marshal(memberParams)
if err != nil {
return false, err
}
contentType := "application/json"
method := "POST"
body := bytes.NewBuffer(memberJson)
request, err := userService.client.buildRequest(ctx, method, contentType, body, requestUrl)
if err != nil {
return false, err
}

successResp, err := userService.client.newRequest(ctx, request)
if err != nil {
return false, err
}

if successResp.StatusCode == http.StatusNoContent {
return true, nil
}
return false, nil
}
Loading

0 comments on commit 598ed60

Please sign in to comment.