Skip to content
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

Implemented the builder method and integrated it with the service objects #40

Merged
merged 1 commit into from
Jul 21, 2022
Merged
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
9 changes: 6 additions & 3 deletions gointelowl/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
"net/http"
"sort"
)

Expand All @@ -31,7 +30,9 @@ type AnalyzerService struct {
*/
func (analyzerService *AnalyzerService) GetConfigs(ctx context.Context) (*[]AnalyzerConfig, error) {
requestUrl := fmt.Sprintf("%s/api/get_analyzer_configs", analyzerService.client.options.Url)
request, err := http.NewRequest("GET", requestUrl, nil)
contentType := "application/json"
method := "GET"
request, err := analyzerService.client.buildRequest(ctx, method, contentType, nil, requestUrl)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -66,7 +67,9 @@ func (analyzerService *AnalyzerService) GetConfigs(ctx context.Context) (*[]Anal
*/
func (analyzerService *AnalyzerService) HealthCheck(ctx context.Context, analyzerName string) (bool, error) {
requestUrl := fmt.Sprintf("%s/api/analyzer/%s/healthcheck", analyzerService.client.options.Url, analyzerName)
request, err := http.NewRequest("GET", requestUrl, nil)
contentType := "application/json"
method := "GET"
request, err := analyzerService.client.buildRequest(ctx, method, contentType, nil, requestUrl)
if err != nil {
return false, err
}
Expand Down
25 changes: 16 additions & 9 deletions gointelowl/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
Expand Down Expand Up @@ -44,10 +45,10 @@ type IntelOwlClientOptions struct {
}

type IntelOwlClient struct {
options *IntelOwlClientOptions
client *http.Client
TagService *TagService
JobService *JobService
options *IntelOwlClientOptions
client *http.Client
TagService *TagService
JobService *JobService
AnalyzerService *AnalyzerService
ConnectorService *ConnectorService
}
Expand Down Expand Up @@ -132,7 +133,7 @@ func NewIntelOwlClient(options *IntelOwlClientOptions, httpClient *http.Client)
client: &client,
}
client.JobService = &JobService{
client: &client,
client: &client,
}
client.AnalyzerService = &AnalyzerService{
client: &client,
Expand All @@ -143,14 +144,20 @@ func NewIntelOwlClient(options *IntelOwlClientOptions, httpClient *http.Client)
return client
}

func (client *IntelOwlClient) newRequest(ctx context.Context, request *http.Request) (*successResponse, error) {
request = request.WithContext(ctx)

request.Header.Set("Content-Type", "application/json")
func (client *IntelOwlClient) buildRequest(ctx context.Context, method string, contentType string, body io.Reader, url string) (*http.Request, error) {
request, err := http.NewRequestWithContext(ctx, method, url, body)
if err != nil {
return nil, err
}
request.Header.Set("Content-Type", contentType)

tokenString := fmt.Sprintf("token %s", client.options.Token)

request.Header.Set("Authorization", tokenString)
return request, nil
}

func (client *IntelOwlClient) newRequest(ctx context.Context, request *http.Request) (*successResponse, error) {
response, err := client.client.Do(request)

// * Checking for context errors such as reaching the deadline and/or Timeout
Expand Down
17 changes: 14 additions & 3 deletions gointelowl/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
"net/http"
"sort"
)

Expand All @@ -17,9 +16,15 @@ type ConnectorService struct {
client *IntelOwlClient
}

/*
* Desc: get the connector configurations of your intelowl instance
* Endpoint: GET /api/get_connector_configs
*/
func (connectorService *ConnectorService) GetConfigs(ctx context.Context) (*[]ConnectorConfig, error) {
requestUrl := fmt.Sprintf("%s/api/get_connector_configs", connectorService.client.options.Url)
request, err := http.NewRequest("GET", requestUrl, nil)
contentType := "application/json"
method := "GET"
request, err := connectorService.client.buildRequest(ctx, method, contentType, nil, requestUrl)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -48,9 +53,15 @@ func (connectorService *ConnectorService) GetConfigs(ctx context.Context) (*[]Co
return &connectorConfigurationList, nil
}

/*
* Desc: Checking if your connector is up and running!
* Endpoint: GET /api/connector/{NameOfConnector}/healthcheck
*/
func (connectorService *ConnectorService) HealthCheck(ctx context.Context, connectorName string) (bool, error) {
requestUrl := fmt.Sprintf("%s/api/connector/%s/healthcheck", connectorService.client.options.Url, connectorName)
request, err := http.NewRequest("GET", requestUrl, nil)
contentType := "application/json"
method := "GET"
request, err := connectorService.client.buildRequest(ctx, method, contentType, nil, requestUrl)
if err != nil {
return false, err
}
Expand Down
36 changes: 27 additions & 9 deletions gointelowl/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ type JobService struct {
*/
func (jobService *JobService) List(ctx context.Context) (*JobListResponse, error) {
requestUrl := fmt.Sprintf("%s/api/jobs", jobService.client.options.Url)
request, err := http.NewRequest("GET", requestUrl, nil)
contentType := "application/json"
method := "GET"
request, err := jobService.client.buildRequest(ctx, method, contentType, nil, requestUrl)
if err != nil {
return nil, err
}
Expand All @@ -97,7 +99,9 @@ func (jobService *JobService) List(ctx context.Context) (*JobListResponse, error
*/
func (jobService *JobService) Get(ctx context.Context, jobId uint64) (*Job, error) {
requestUrl := fmt.Sprintf("%s/api/jobs/%d", jobService.client.options.Url, jobId)
request, err := http.NewRequest("GET", requestUrl, nil)
contentType := "application/json"
method := "GET"
request, err := jobService.client.buildRequest(ctx, method, contentType, nil, requestUrl)
if err != nil {
return nil, err
}
Expand All @@ -119,7 +123,9 @@ func (jobService *JobService) Get(ctx context.Context, jobId uint64) (*Job, erro
*/
func (jobService *JobService) DownloadSample(ctx context.Context, jobId uint64) ([]byte, error) {
requestUrl := fmt.Sprintf("%s/api/jobs/%d/download_sample", jobService.client.options.Url, jobId)
request, err := http.NewRequest("GET", requestUrl, nil)
contentType := "application/json"
method := "GET"
request, err := jobService.client.buildRequest(ctx, method, contentType, nil, requestUrl)
if err != nil {
return nil, err
}
Expand All @@ -136,7 +142,9 @@ func (jobService *JobService) DownloadSample(ctx context.Context, jobId uint64)
*/
func (jobService *JobService) Delete(ctx context.Context, jobId uint64) (bool, error) {
requestUrl := fmt.Sprintf("%s/api/jobs/%d", jobService.client.options.Url, jobId)
request, err := http.NewRequest("DELETE", requestUrl, nil)
contentType := "application/json"
method := "DELETE"
request, err := jobService.client.buildRequest(ctx, method, contentType, nil, requestUrl)
if err != nil {
return false, err
}
Expand All @@ -156,7 +164,9 @@ func (jobService *JobService) Delete(ctx context.Context, jobId uint64) (bool, e
*/
func (jobService *JobService) Kill(ctx context.Context, jobId uint64) (bool, error) {
requestUrl := fmt.Sprintf("%s/api/jobs/%d/kill", jobService.client.options.Url, jobId)
request, err := http.NewRequest("PATCH", requestUrl, nil)
contentType := "application/json"
method := "PATCH"
request, err := jobService.client.buildRequest(ctx, method, contentType, nil, requestUrl)
if err != nil {
return false, err
}
Expand All @@ -176,7 +186,9 @@ func (jobService *JobService) Kill(ctx context.Context, jobId uint64) (bool, err
*/
func (jobService *JobService) KillAnalyzer(ctx context.Context, jobId uint64, analyzerName string) (bool, error) {
requestUrl := fmt.Sprintf("%s/api/jobs/%d/analyzer/%s/kill", jobService.client.options.Url, jobId, analyzerName)
request, err := http.NewRequest("PATCH", requestUrl, nil)
contentType := "application/json"
method := "PATCH"
request, err := jobService.client.buildRequest(ctx, method, contentType, nil, requestUrl)
if err != nil {
return false, err
}
Expand All @@ -196,7 +208,9 @@ func (jobService *JobService) KillAnalyzer(ctx context.Context, jobId uint64, an
*/
func (jobService *JobService) RetryAnalyzer(ctx context.Context, jobId uint64, analyzerName string) (bool, error) {
requestUrl := fmt.Sprintf("%s/api/jobs/%d/analyzer/%s/retry", jobService.client.options.Url, jobId, analyzerName)
request, err := http.NewRequest("PATCH", requestUrl, nil)
contentType := "application/json"
method := "PATCH"
request, err := jobService.client.buildRequest(ctx, method, contentType, nil, requestUrl)
if err != nil {
return false, err
}
Expand All @@ -216,7 +230,9 @@ func (jobService *JobService) RetryAnalyzer(ctx context.Context, jobId uint64, a
*/
func (jobService *JobService) KillConnector(ctx context.Context, jobId uint64, connectorName string) (bool, error) {
requestUrl := fmt.Sprintf("%s/api/jobs/%d/connector/%s/kill", jobService.client.options.Url, jobId, connectorName)
request, err := http.NewRequest("PATCH", requestUrl, nil)
contentType := "application/json"
method := "PATCH"
request, err := jobService.client.buildRequest(ctx, method, contentType, nil, requestUrl)
if err != nil {
return false, err
}
Expand All @@ -236,7 +252,9 @@ func (jobService *JobService) KillConnector(ctx context.Context, jobId uint64, c
*/
func (jobService *JobService) RetryConnector(ctx context.Context, jobId uint64, connectorName string) (bool, error) {
requestUrl := fmt.Sprintf("%s/api/jobs/%d/connector/%s/retry", jobService.client.options.Url, jobId, connectorName)
request, err := http.NewRequest("PATCH", requestUrl, nil)
contentType := "application/json"
method := "PATCH"
request, err := jobService.client.buildRequest(ctx, method, contentType, nil, requestUrl)
if err != nil {
return false, err
}
Expand Down
30 changes: 17 additions & 13 deletions gointelowl/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ func checkTagID(id uint64) error {
*/
func (tagService *TagService) List(ctx context.Context) (*[]Tag, error) {
requestUrl := fmt.Sprintf("%s/api/tags", tagService.client.options.Url)
request, err := http.NewRequest("GET", requestUrl, nil)
contentType := "application/json"
method := "GET"
request, err := tagService.client.buildRequest(ctx, method, contentType, nil, requestUrl)
if err != nil {
return nil, err
}
Expand All @@ -65,7 +67,9 @@ func (tagService *TagService) Get(ctx context.Context, tagId uint64) (*Tag, erro
return nil, err
}
requestUrl := fmt.Sprintf("%s/api/tags/%d", tagService.client.options.Url, tagId)
request, err := http.NewRequest("GET", requestUrl, nil)
contentType := "application/json"
method := "GET"
request, err := tagService.client.buildRequest(ctx, method, contentType, nil, requestUrl)
if err != nil {
return nil, err
}
Expand All @@ -91,7 +95,10 @@ func (tagService *TagService) Create(ctx context.Context, tagParams *TagParams)
if err != nil {
return nil, err
}
request, err := http.NewRequest("POST", requestUrl, bytes.NewBuffer(tagJson))
contentType := "application/json"
method := "POST"
body := bytes.NewBuffer(tagJson)
request, err := tagService.client.buildRequest(ctx, method, contentType, body, requestUrl)
if err != nil {
return nil, err
}
Expand All @@ -118,7 +125,10 @@ func (tagService *TagService) Update(ctx context.Context, tagId uint64, tagParam
if err != nil {
return nil, err
}
request, err := http.NewRequest("PUT", requestUrl, bytes.NewBuffer(tagJson))
contentType := "application/json"
method := "PUT"
body := bytes.NewBuffer(tagJson)
request, err := tagService.client.buildRequest(ctx, method, contentType, body, requestUrl)
if err != nil {
return nil, err
}
Expand All @@ -144,9 +154,9 @@ func (tagService *TagService) Delete(ctx context.Context, tagId uint64) (bool, e
return false, err
}
requestUrl := fmt.Sprintf("%s/api/tags/%d", tagService.client.options.Url, tagId)
// printing the request
fmt.Println("Url: " + requestUrl)
request, err := http.NewRequest("DELETE", requestUrl, nil)
contentType := "application/json"
method := "DELETE"
request, err := tagService.client.buildRequest(ctx, method, contentType, nil, requestUrl)
if err != nil {
return false, err
}
Expand All @@ -159,9 +169,3 @@ func (tagService *TagService) Delete(ctx context.Context, tagId uint64) (bool, e
}
return false, nil
}

//* Pretty printing the tag
func (tag *Tag) Display() {
display := fmt.Sprintf("ID:%d\nLabel:%s\nColor:%s", tag.ID, tag.Label, tag.Color)
fmt.Println(display)
}