Skip to content

Commit 598ed60

Browse files
Adding the me endpoints with its respective unit tests (#57)
* Adding the me endpoints with its respective unit tests * Fixing comment error * Add newline Co-authored-by: Shubham Pandey <[email protected]>
1 parent d15a363 commit 598ed60

File tree

3 files changed

+394
-0
lines changed

3 files changed

+394
-0
lines changed

gointelowl/client.go

+10
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ type IntelOwlClient struct {
6262
JobService *JobService
6363
AnalyzerService *AnalyzerService
6464
ConnectorService *ConnectorService
65+
UserService *UserService
6566
Logger *IntelOwlLogger
6667
}
6768

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

143+
// configuring the http.Client
142144
if httpClient == nil {
143145
httpClient = &http.Client{
144146
Timeout: timeout,
145147
}
146148
}
149+
150+
// configuring the client
147151
client := IntelOwlClient{
148152
options: options,
149153
client: httpClient,
150154
}
155+
156+
// Adding the services
151157
client.TagService = &TagService{
152158
client: &client,
153159
}
@@ -160,7 +166,11 @@ func NewIntelOwlClient(options *IntelOwlClientOptions, httpClient *http.Client,
160166
client.ConnectorService = &ConnectorService{
161167
client: &client,
162168
}
169+
client.UserService = &UserService{
170+
client: &client,
171+
}
163172

173+
// configuring the logger!
164174
client.Logger = &IntelOwlLogger{}
165175
client.Logger.Init(loggerParams)
166176

gointelowl/me.go

+214
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
package gointelowl
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"encoding/json"
7+
"fmt"
8+
"net/http"
9+
"time"
10+
)
11+
12+
type Details struct {
13+
Username string `json:"username"`
14+
FirstName string `json:"first_name"`
15+
LastName string `json:"last_name"`
16+
FullName string `json:"full_name"`
17+
Email string `json:"email"`
18+
}
19+
20+
type AccessDetails struct {
21+
TotalSubmissions int `json:"total_submissions"`
22+
MonthSubmissions int `json:"month_submissions"`
23+
}
24+
25+
type User struct {
26+
User Details `json:"user"`
27+
Access AccessDetails `json:"access"`
28+
}
29+
30+
type UserService struct {
31+
client *IntelOwlClient
32+
}
33+
34+
type Owner struct {
35+
Username string `json:"username"`
36+
FullName string `json:"full_name"`
37+
Joined time.Time `json:"joined"`
38+
}
39+
40+
type Organization struct {
41+
MembersCount int `json:"members_count"`
42+
Owner Owner `json:"owner"`
43+
IsUserOwner bool `json:"is_user_owner,omitempty"`
44+
CreatedAt *time.Time `json:"created_at,omitempty"`
45+
Name string `json:"name"`
46+
}
47+
48+
type OrganizationParams struct {
49+
Name string `json:"name"`
50+
}
51+
52+
type MemberParams struct {
53+
Username string `json:"username"`
54+
}
55+
56+
type Invite struct {
57+
Id int `json:"id"`
58+
CreatedAt time.Time `json:"created_at"`
59+
Status string `json:"status"`
60+
}
61+
62+
type Invitation struct {
63+
Invite
64+
Organization Organization `json:"organization"`
65+
}
66+
67+
type InvitationParams struct {
68+
Organization OrganizationParams `json:"organization"`
69+
Status string `json:"status"`
70+
}
71+
72+
// Access retrieves user details
73+
//
74+
// Endpoint: GET /api/me/access
75+
//
76+
// IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/me/operation/me_access_retrieve
77+
func (userService *UserService) Access(ctx context.Context) (*User, error) {
78+
requestUrl := fmt.Sprintf("%s/api/me/access", userService.client.options.Url)
79+
contentType := "application/json"
80+
method := "GET"
81+
request, err := userService.client.buildRequest(ctx, method, contentType, nil, requestUrl)
82+
if err != nil {
83+
return nil, err
84+
}
85+
user := User{}
86+
successResp, err := userService.client.newRequest(ctx, request)
87+
if err != nil {
88+
return nil, err
89+
}
90+
if unmarshalError := json.Unmarshal(successResp.Data, &user); unmarshalError != nil {
91+
return nil, unmarshalError
92+
}
93+
return &user, nil
94+
}
95+
96+
// Organization returns the organization's details.
97+
//
98+
// Endpoint: GET /api/me/organization
99+
//
100+
// IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/me/operation/me_organization_list
101+
func (userService *UserService) Organization(ctx context.Context) (*Organization, error) {
102+
requestUrl := fmt.Sprintf("%s/api/me/organization", userService.client.options.Url)
103+
contentType := "application/json"
104+
method := "GET"
105+
request, err := userService.client.buildRequest(ctx, method, contentType, nil, requestUrl)
106+
if err != nil {
107+
return nil, err
108+
}
109+
110+
org := Organization{}
111+
successResp, err := userService.client.newRequest(ctx, request)
112+
if err != nil {
113+
return nil, err
114+
}
115+
if unmarshalError := json.Unmarshal(successResp.Data, &org); unmarshalError != nil {
116+
return nil, unmarshalError
117+
}
118+
return &org, nil
119+
}
120+
121+
// CreateOrganization allows you to create a super cool organization!
122+
//
123+
// Endpoint: POST /api/me/organization
124+
//
125+
// IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/me/operation/me_organization_create
126+
func (userService *UserService) CreateOrganization(ctx context.Context, organizationParams *OrganizationParams) (*Organization, error) {
127+
requestUrl := fmt.Sprintf("%s/api/me/organization", userService.client.options.Url)
128+
// Getting the relevant JSON data
129+
orgJson, err := json.Marshal(organizationParams)
130+
if err != nil {
131+
return nil, err
132+
}
133+
contentType := "application/json"
134+
method := "POST"
135+
body := bytes.NewBuffer(orgJson)
136+
request, err := userService.client.buildRequest(ctx, method, contentType, body, requestUrl)
137+
if err != nil {
138+
return nil, err
139+
}
140+
141+
org := Organization{}
142+
successResp, err := userService.client.newRequest(ctx, request)
143+
if err != nil {
144+
return nil, err
145+
}
146+
if unmarshalError := json.Unmarshal(successResp.Data, &org); unmarshalError != nil {
147+
return nil, unmarshalError
148+
}
149+
return &org, nil
150+
}
151+
152+
// InviteToOrganization allows you to invite someone to your super cool organization!
153+
// This is only accessible to the organization's owner.
154+
//
155+
// Endpoint: POST /api/me/organization/invite
156+
//
157+
// IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/me/operation/me_organization_invite_create
158+
func (userService *UserService) InviteToOrganization(ctx context.Context, memberParams *MemberParams) (*Invite, error) {
159+
requestUrl := fmt.Sprintf("%s/api/me/organization/invite", userService.client.options.Url)
160+
// Getting the relevant JSON data
161+
memberJson, err := json.Marshal(memberParams)
162+
if err != nil {
163+
return nil, err
164+
}
165+
contentType := "application/json"
166+
method := "POST"
167+
body := bytes.NewBuffer(memberJson)
168+
request, err := userService.client.buildRequest(ctx, method, contentType, body, requestUrl)
169+
if err != nil {
170+
return nil, err
171+
}
172+
173+
invite := Invite{}
174+
successResp, err := userService.client.newRequest(ctx, request)
175+
if err != nil {
176+
return nil, err
177+
}
178+
if unmarshalError := json.Unmarshal(successResp.Data, &invite); unmarshalError != nil {
179+
return nil, unmarshalError
180+
}
181+
return &invite, nil
182+
}
183+
184+
// RemoveMemberFromOrganization lets you remove someone from your super cool organization! (you had your reasons)
185+
// This is only accessible to the organization's owner.
186+
//
187+
// Endpoint: POST /api/me/organization/remove_member
188+
//
189+
// IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/me/operation/me_organization_create
190+
func (userService *UserService) RemoveMemberFromOrganization(ctx context.Context, memberParams *MemberParams) (bool, error) {
191+
requestUrl := fmt.Sprintf("%s/api/me/organization/remove_member", userService.client.options.Url)
192+
// Getting the relevant JSON data
193+
memberJson, err := json.Marshal(memberParams)
194+
if err != nil {
195+
return false, err
196+
}
197+
contentType := "application/json"
198+
method := "POST"
199+
body := bytes.NewBuffer(memberJson)
200+
request, err := userService.client.buildRequest(ctx, method, contentType, body, requestUrl)
201+
if err != nil {
202+
return false, err
203+
}
204+
205+
successResp, err := userService.client.newRequest(ctx, request)
206+
if err != nil {
207+
return false, err
208+
}
209+
210+
if successResp.StatusCode == http.StatusNoContent {
211+
return true, nil
212+
}
213+
return false, nil
214+
}

0 commit comments

Comments
 (0)