Skip to content

Commit e5f196e

Browse files
author
Debashish Kundu
authored
removed package name from content names, updated deprecated ioutil code (#64)
1 parent 1afd225 commit e5f196e

File tree

18 files changed

+135
-130
lines changed

18 files changed

+135
-130
lines changed

constants/constants.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,15 @@ const (
3939
)
4040

4141
// These represent me endpoints URL
42-
4342
const (
4443
BASE_ME_URL = "/api/me"
4544
USER_DETAILS_URL = BASE_ME_URL + "/access"
4645
ORGANIZATION_URL = BASE_ME_URL + "/organization"
4746
INVITE_TO_ORGANIZATION_URL = ORGANIZATION_URL + "/invite"
4847
REMOVE_MEMBER_FROM_ORGANIZATION_URL = ORGANIZATION_URL + "/remove_member"
4948
)
49+
50+
// These represent REST API related constants
51+
const (
52+
ContentTypeJSON = "application/json"
53+
)

examples/basicExample/example.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111

1212
func main() {
1313

14-
// Configuring the IntelOwlClient!
15-
clientOptions := gointelowl.IntelOwlClientOptions{
14+
// Configuring the Client!
15+
clientOptions := gointelowl.ClientOptions{
1616
Url: "PUT-YOUR-INTELOWL-INSTANCE-URL-HERE",
1717
Token: "PUT-YOUR-TOKEN-HERE",
1818
Certificate: "",
@@ -26,7 +26,7 @@ func main() {
2626
}
2727

2828
// Making the client!
29-
client := gointelowl.NewIntelOwlClient(
29+
client := gointelowl.NewClient(
3030
&clientOptions,
3131
nil,
3232
loggerParams,

examples/client/client.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ import (
99

1010
func main() {
1111
/*
12-
Making a new client through NewIntelOwlClient:
12+
Making a new client through NewClient:
1313
This takes the following parameters:
14-
1. IntelOwlClientOptions
14+
1. ClientOptions
1515
2. A *http.Client (if you do not provide one. One will be made by default)
1616
3. LoggerParams
17-
These are parameters that allow you to easily configure your IntelOwlClient to your liking.
17+
These are parameters that allow you to easily configure your Client to your liking.
1818
For a better understanding you can read it in the documentation: https://github.com/intelowlproject/go-intelowl/tree/main/examples/optionalParams
1919
*/
2020

21-
// Configuring the IntelOwlClient!
22-
clientOptions := gointelowl.IntelOwlClientOptions{
21+
// Configuring the Client!
22+
clientOptions := gointelowl.ClientOptions{
2323
Url: "PUT-YOUR-INTELOWL-INSTANCE-URL-HERE",
2424
Token: "PUT-YOUR-TOKEN-HERE",
2525
Certificate: "",
@@ -34,7 +34,7 @@ func main() {
3434
}
3535

3636
// Making the client!
37-
client := gointelowl.NewIntelOwlClient(
37+
client := gointelowl.NewClient(
3838
&clientOptions,
3939
nil,
4040
loggerParams,

examples/endpoints/endpoints.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111

1212
func main() {
1313

14-
// Configuring the IntelOwlClient!
15-
clientOptions := gointelowl.IntelOwlClientOptions{
14+
// Configuring the Client!
15+
clientOptions := gointelowl.ClientOptions{
1616
Url: "PUT-YOUR-INTELOWL-INSTANCE-URL-HERE",
1717
Token: "PUT-YOUR-TOKEN-HERE",
1818
Certificate: "",
@@ -25,7 +25,7 @@ func main() {
2525
}
2626

2727
// Making the client!
28-
client := gointelowl.NewIntelOwlClient(
28+
client := gointelowl.NewClient(
2929
&clientOptions,
3030
nil,
3131
loggerParams,
@@ -42,7 +42,7 @@ func main() {
4242

4343
// Getting the tag list!
4444
tagList, err := client.TagService.List(ctx)
45-
// checking for any pesky errors if there's any error it'll return an IntelOwlError
45+
// checking for any pesky errors if there's any error it'll return an Error
4646
if err != nil {
4747
fmt.Println(err)
4848
} else {

examples/optionalParams/optionalParams.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ For this example I'll be using the tag params!
1414
*/
1515
func main() {
1616

17-
// Configuring the IntelOwlClient!
18-
clientOptions := gointelowl.IntelOwlClientOptions{
17+
// Configuring the Client!
18+
clientOptions := gointelowl.ClientOptions{
1919
Url: "PUT-YOUR-INTELOWL-INSTANCE-URL-HERE",
2020
Token: "PUT-YOUR-TOKEN-HERE",
2121
Certificate: "",
@@ -28,7 +28,7 @@ func main() {
2828
}
2929

3030
// Making the client!
31-
client := gointelowl.NewIntelOwlClient(
31+
client := gointelowl.NewClient(
3232
&clientOptions,
3333
nil,
3434
loggerParams,

gointelowl/analysis.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ type MultipleAnalysisResponse struct {
6767
// Endpoint: POST /api/analyze_observable
6868
//
6969
// IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/analyze_observable
70-
func (client *IntelOwlClient) CreateObservableAnalysis(ctx context.Context, params *ObservableAnalysisParams) (*AnalysisResponse, error) {
70+
func (client *Client) CreateObservableAnalysis(ctx context.Context, params *ObservableAnalysisParams) (*AnalysisResponse, error) {
7171
requestUrl := client.options.Url + constants.ANALYZE_OBSERVABLE_URL
7272
method := "POST"
7373
contentType := "application/json"
@@ -96,7 +96,7 @@ func (client *IntelOwlClient) CreateObservableAnalysis(ctx context.Context, para
9696
// Endpoint: POST /api/analyze_multiple_observables
9797
//
9898
// IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/analyze_multiple_observables
99-
func (client *IntelOwlClient) CreateMultipleObservableAnalysis(ctx context.Context, params *MultipleObservableAnalysisParams) (*MultipleAnalysisResponse, error) {
99+
func (client *Client) CreateMultipleObservableAnalysis(ctx context.Context, params *MultipleObservableAnalysisParams) (*MultipleAnalysisResponse, error) {
100100
requestUrl := client.options.Url + constants.ANALYZE_MULTIPLE_OBSERVABLES_URL
101101
method := "POST"
102102
contentType := "application/json"
@@ -124,7 +124,7 @@ func (client *IntelOwlClient) CreateMultipleObservableAnalysis(ctx context.Conte
124124
// Endpoint: POST /api/analyze_file
125125
//
126126
// IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/analyze_file
127-
func (client *IntelOwlClient) CreateFileAnalysis(ctx context.Context, fileAnalysisParams *FileAnalysisParams) (*AnalysisResponse, error) {
127+
func (client *Client) CreateFileAnalysis(ctx context.Context, fileAnalysisParams *FileAnalysisParams) (*AnalysisResponse, error) {
128128
requestUrl := client.options.Url + constants.ANALYZE_FILE_URL
129129
// * Making the multiform data
130130
body := &bytes.Buffer{}
@@ -202,7 +202,7 @@ func (client *IntelOwlClient) CreateFileAnalysis(ctx context.Context, fileAnalys
202202
// Endpoint: POST /api/analyze_mutliple_files
203203
//
204204
// IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/analyze_multiple_files
205-
func (client *IntelOwlClient) CreateMultipleFileAnalysis(ctx context.Context, fileAnalysisParams *MultipleFileAnalysisParams) (*MultipleAnalysisResponse, error) {
205+
func (client *Client) CreateMultipleFileAnalysis(ctx context.Context, fileAnalysisParams *MultipleFileAnalysisParams) (*MultipleAnalysisResponse, error) {
206206
requestUrl := client.options.Url + constants.ANALYZE_MULTIPLE_FILES_URL
207207
// * Making the multiform data
208208
body := &bytes.Buffer{}

gointelowl/analyzer.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"net/http"
78
"sort"
89

910
"github.com/intelowlproject/go-intelowl/constants"
@@ -29,7 +30,7 @@ type AnalyzerConfig struct {
2930
//
3031
// IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/analyzer
3132
type AnalyzerService struct {
32-
client *IntelOwlClient
33+
client *Client
3334
}
3435

3536
// GetConfigs lists down every analyzer configuration in your IntelOwl instance.
@@ -39,8 +40,8 @@ type AnalyzerService struct {
3940
// IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/get_analyzer_configs
4041
func (analyzerService *AnalyzerService) GetConfigs(ctx context.Context) (*[]AnalyzerConfig, error) {
4142
requestUrl := analyzerService.client.options.Url + constants.ANALYZER_CONFIG_URL
42-
contentType := "application/json"
43-
method := "GET"
43+
contentType := constants.ContentTypeJSON
44+
method := http.MethodGet
4445
request, err := analyzerService.client.buildRequest(ctx, method, contentType, nil, requestUrl)
4546
if err != nil {
4647
return nil, err
@@ -62,7 +63,7 @@ func (analyzerService *AnalyzerService) GetConfigs(ctx context.Context) (*[]Anal
6263
}
6364
// * sorting them alphabetically
6465
sort.Strings(analyzerNames)
65-
analyzerConfigurationList := []AnalyzerConfig{}
66+
var analyzerConfigurationList []AnalyzerConfig
6667
for _, analyzerName := range analyzerNames {
6768
analyzerConfig := analyzerConfigurationResponse[analyzerName]
6869
analyzerConfigurationList = append(analyzerConfigurationList, analyzerConfig)
@@ -78,8 +79,8 @@ func (analyzerService *AnalyzerService) GetConfigs(ctx context.Context) (*[]Anal
7879
func (analyzerService *AnalyzerService) HealthCheck(ctx context.Context, analyzerName string) (bool, error) {
7980
route := analyzerService.client.options.Url + constants.ANALYZER_HEALTHCHECK_URL
8081
requestUrl := fmt.Sprintf(route, analyzerName)
81-
contentType := "application/json"
82-
method := "GET"
82+
contentType := constants.ContentTypeJSON
83+
method := http.MethodGet
8384
request, err := analyzerService.client.buildRequest(ctx, method, contentType, nil, requestUrl)
8485
if err != nil {
8586
return false, err

gointelowl/client.go

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,29 @@ import (
99
"encoding/json"
1010
"fmt"
1111
"io"
12-
"io/ioutil"
1312
"net/http"
1413
"os"
1514
"strings"
1615
"time"
1716
)
1817

19-
// IntelOwlError represents an error that has occurred when communicating with IntelOwl.
20-
type IntelOwlError struct {
18+
// Error represents an error that has occurred when communicating with IntelOwl.
19+
type Error struct {
2120
StatusCode int
2221
Message string
2322
Response *http.Response
2423
}
2524

2625
// Error lets you implement the error interface.
2726
// This is used for making custom go errors.
28-
func (intelOwlError *IntelOwlError) Error() string {
27+
func (intelOwlError *Error) Error() string {
2928
errorMessage := fmt.Sprintf("Status Code: %d \n Error: %s", intelOwlError.StatusCode, intelOwlError.Message)
3029
return errorMessage
3130
}
3231

33-
// newIntelOwlError lets you easily create new IntelOwlErrors.
34-
func newIntelOwlError(statusCode int, message string, response *http.Response) *IntelOwlError {
35-
return &IntelOwlError{
32+
// newError lets you easily create new Errors.
33+
func newError(statusCode int, message string, response *http.Response) *Error {
34+
return &Error{
3635
StatusCode: statusCode,
3736
Message: message,
3837
Response: response,
@@ -44,8 +43,8 @@ type successResponse struct {
4443
Data []byte
4544
}
4645

47-
// IntelOwlClientOptions represents the fields needed to configure and use the IntelOwlClient
48-
type IntelOwlClientOptions struct {
46+
// ClientOptions represents the fields needed to configure and use the Client
47+
type ClientOptions struct {
4948
Url string `json:"url"`
5049
Token string `json:"token"`
5150
// Certificate represents your SSL cert: path to the cert file!
@@ -54,16 +53,16 @@ type IntelOwlClientOptions struct {
5453
Timeout uint64 `json:"timeout"`
5554
}
5655

57-
// IntelOwlClient handles all the communication with your IntelOwl instance.
58-
type IntelOwlClient struct {
59-
options *IntelOwlClientOptions
56+
// Client handles all the communication with your IntelOwl instance.
57+
type Client struct {
58+
options *ClientOptions
6059
client *http.Client
6160
TagService *TagService
6261
JobService *JobService
6362
AnalyzerService *AnalyzerService
6463
ConnectorService *ConnectorService
6564
UserService *UserService
66-
Logger *IntelOwlLogger
65+
Logger *Logger
6766
}
6867

6968
// TLP represents an enum for the TLP attribute used in IntelOwl's REST API.
@@ -113,7 +112,7 @@ func ParseTLP(s string) TLP {
113112
}
114113

115114
// Implementing the MarshalJSON interface to make our custom Marshal for the enum
116-
func (tlp TLP) MarshalJSON() ([]byte, error) {
115+
func (tlp *TLP) MarshalJSON() ([]byte, error) {
117116
return json.Marshal(tlp.String())
118117
}
119118

@@ -129,8 +128,8 @@ func (tlp *TLP) UnmarshalJSON(data []byte) (err error) {
129128
return nil
130129
}
131130

132-
// NewIntelOwlClient lets you easily create a new IntelOwlClient by providing IntelOwlClientOptions, http.Clients, and LoggerParams.
133-
func NewIntelOwlClient(options *IntelOwlClientOptions, httpClient *http.Client, loggerParams *LoggerParams) IntelOwlClient {
131+
// NewClient lets you easily create a new Client by providing ClientOptions, http.Clients, and LoggerParams.
132+
func NewClient(options *ClientOptions, httpClient *http.Client, loggerParams *LoggerParams) Client {
134133

135134
var timeout time.Duration
136135

@@ -148,7 +147,7 @@ func NewIntelOwlClient(options *IntelOwlClientOptions, httpClient *http.Client,
148147
}
149148

150149
// configuring the client
151-
client := IntelOwlClient{
150+
client := Client{
152151
options: options,
153152
client: httpClient,
154153
}
@@ -171,33 +170,33 @@ func NewIntelOwlClient(options *IntelOwlClientOptions, httpClient *http.Client,
171170
}
172171

173172
// configuring the logger!
174-
client.Logger = &IntelOwlLogger{}
173+
client.Logger = &Logger{}
175174
client.Logger.Init(loggerParams)
176175

177176
return client
178177
}
179178

180-
// NewIntelOwlClientThroughJsonFile lets you create a new IntelOwlClient through a JSON file that contains your IntelOwlClientOptions
181-
func NewIntelOwlClientThroughJsonFile(filePath string, httpClient *http.Client, loggerParams *LoggerParams) (*IntelOwlClient, error) {
179+
// NewClientFromJsonFile lets you create a new Client through a JSON file that contains your ClientOptions
180+
func NewClientFromJsonFile(filePath string, httpClient *http.Client, loggerParams *LoggerParams) (*Client, error) {
182181
optionsBytes, err := os.ReadFile(filePath)
183182
if err != nil {
184183
errorMessage := fmt.Sprintf("Could not read %s", filePath)
185-
intelOwlError := newIntelOwlError(400, errorMessage, nil)
184+
intelOwlError := newError(400, errorMessage, nil)
186185
return nil, intelOwlError
187186
}
188187

189-
intelOwlClientOptions := &IntelOwlClientOptions{}
188+
intelOwlClientOptions := &ClientOptions{}
190189
if unmarshalError := json.Unmarshal(optionsBytes, &intelOwlClientOptions); unmarshalError != nil {
191190
return nil, unmarshalError
192191
}
193192

194-
intelOwlClient := NewIntelOwlClient(intelOwlClientOptions, httpClient, loggerParams)
193+
intelOwlClient := NewClient(intelOwlClientOptions, httpClient, loggerParams)
195194

196195
return &intelOwlClient, nil
197196
}
198197

199198
// buildRequest is used for building requests.
200-
func (client *IntelOwlClient) buildRequest(ctx context.Context, method string, contentType string, body io.Reader, url string) (*http.Request, error) {
199+
func (client *Client) buildRequest(ctx context.Context, method string, contentType string, body io.Reader, url string) (*http.Request, error) {
201200
request, err := http.NewRequestWithContext(ctx, method, url, body)
202201
if err != nil {
203202
return nil, err
@@ -211,7 +210,7 @@ func (client *IntelOwlClient) buildRequest(ctx context.Context, method string, c
211210
}
212211

213212
// newRequest is used for making requests.
214-
func (client *IntelOwlClient) newRequest(ctx context.Context, request *http.Request) (*successResponse, error) {
213+
func (client *Client) newRequest(ctx context.Context, request *http.Request) (*successResponse, error) {
215214
response, err := client.client.Do(request)
216215

217216
// Checking for context errors such as reaching the deadline and/or Timeout
@@ -226,17 +225,17 @@ func (client *IntelOwlClient) newRequest(ctx context.Context, request *http.Requ
226225

227226
defer response.Body.Close()
228227

229-
msgBytes, err := ioutil.ReadAll(response.Body)
228+
msgBytes, err := io.ReadAll(response.Body)
230229
statusCode := response.StatusCode
231230
if err != nil {
232231
errorMessage := fmt.Sprintf("Could not convert JSON response. Status code: %d", statusCode)
233-
intelOwlError := newIntelOwlError(statusCode, errorMessage, response)
232+
intelOwlError := newError(statusCode, errorMessage, response)
234233
return nil, intelOwlError
235234
}
236235

237236
if statusCode < http.StatusOK || statusCode >= http.StatusBadRequest {
238237
errorMessage := string(msgBytes)
239-
intelOwlError := newIntelOwlError(statusCode, errorMessage, response)
238+
intelOwlError := newError(statusCode, errorMessage, response)
240239
return nil, intelOwlError
241240
}
242241

0 commit comments

Comments
 (0)