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

WIP: List reports for the given file ID #30

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
148 changes: 38 additions & 110 deletions appknox/reports.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,139 +4,67 @@ import (
"context"
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"strconv"
"time"
)

type ReportsService service

type Report struct {
URL string `json:"url"`
type HIPAAPreferences struct {
ShowHIPPA bool `json:"value"`
IsInherited bool `json:"is_inherited"`
}

type ReportResult struct {
ID int `json:"id"`
GeneratedOn string `json:"generated_on"`
Language string `json:"language"`
Progress int `json:"progress"`
Rating string `json:"rating"`
type PCIDSSPreferences struct {
ShowPCIDSS bool `json:"value"`
IsInherited bool `json:"is_inherited"`
}

// GenerateReport generates a report for the specified file.
func (s *ReportsService) GenerateReport(ctx context.Context, fileID int) (*ReportResult, error) {
url := fmt.Sprintf("api/v2/files/%d/reports", fileID)
req, err := s.client.NewRequest("POST", url, nil)
if err != nil {
return nil, err
}

var resp ReportResult

res, err := s.client.Do(ctx, req, &resp)
if err != nil {
if res.StatusCode == 400 {
return nil, errors.New("A report is already being generated or scan is in progress. Please wait.")
}
if res.StatusCode == 404 {
return nil, errors.New("File not found")
}
return nil, err
}

return &resp, nil
type ReportPreferences struct {
ShowAPIScan bool `json:"show_api_scan"`
ShowManualScan bool `json:"show_manual_scan"`
ShowStaticScan bool `json:"show_static_scan"`
ShowDynamicScan bool `json:"show_dynamic_scan"`
ShowIgnoredAnalyses bool `json:"show_ignored_analyses_scan"`
kirankumbhar marked this conversation as resolved.
Show resolved Hide resolved
PCIDSSPreferences PCIDSSPreferences `json:"show_hipaa"`
HIPAAPreferences HIPAAPreferences `json:"show_pcidss"`
}

// FetchReportResult it will fetch report result by result id.
func (s *ReportsService) FetchReportResult(ctx context.Context, reportID int) (*ReportResult, error) {
url := fmt.Sprintf("api/v2/reports/%d", reportID)
req, err := s.client.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}

var resp ReportResult

_, err = s.client.Do(ctx, req, &resp)
if err != nil {
return nil, err
}
type ReportResult struct {
ID int `json:"id"`
GeneratedOn *time.Time `json:"generated_on"`
Language string `json:"language"`
Progress int `json:"progress"`
Rating string `json:"rating"`
ReportPreferences ReportPreferences `json:"preferences"`
rajan262 marked this conversation as resolved.
Show resolved Hide resolved
}

return &resp, nil
type DRFResponseReport struct {
Count int `json:"count,omitempty"`
Next string `json:"next,omitempty"`
Previous string `json:"previous,omitempty"`
Results []*ReportResult `json:"results"`
}

// FetchLastReportResult it will return last report result, report list api is responding in decending order.
func (s *ReportsService) FetchLastReportResult(ctx context.Context, fileID int) (*ReportResult, error) {
func (s *ReportsService) List(ctx context.Context, fileID int) ([]*ReportResult, error) {
url := fmt.Sprintf("api/v2/files/%d/reports", fileID)
req, err := s.client.NewRequest("GET", url, nil)
request, err := s.client.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}

type ReportResultList struct {
Results []*ReportResult `json:"results"`
}

var resp ReportResultList

_, err = s.client.Do(ctx, req, &resp)
if err != nil {
return nil, err
}
var drfResponseReport DRFResponseReport

if len(resp.Results) > 0 {
return resp.Results[0], nil
}
return nil, errors.New("No report results found")
}

// GetReportURL returns the url of the report file to download.
func (s *ReportsService) GetReportURL(ctx context.Context, reportID int) (*Report, error) {
u := fmt.Sprintf("api/v2/reports/%d/pdf", reportID)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, err
resp, err := s.client.Do(ctx, request, &drfResponseReport)
if resp.StatusCode == 404 {
rajan262 marked this conversation as resolved.
Show resolved Hide resolved
id := strconv.Itoa(fileID)
return nil, errors.New("Reports for fileID " + id + " doesn't exist. Are you sure " + id + " is a fileID?")
}

var reportResponse Report

_, err = s.client.Do(ctx, req, &reportResponse)
if err != nil {
return nil, err
}

return &reportResponse, nil
}

// DownloadFile downloads the report file to the specified directory.
func (s *ReportsService) DownloadFile(ctx context.Context, url string, outputDir string) (string, error) {

// Generating filename from download url
filename := strings.Split(strings.Split(url, "?")[0], "/")[len(strings.Split(url, "/"))-1]

outputPath := filepath.Join(outputDir, filename)

// Creating output file from output path
out, err := os.Create(outputPath)
if err != nil {
return "", err
}
defer out.Close()

// Downloading file
resp, err := http.Get(url)
if err != nil || resp.StatusCode != 200 {
if resp.StatusCode != 200 {
err = errors.New(`resource not found`)
}
return "", err
}
defer resp.Body.Close()
return drfResponseReport.Results, nil

// Writing file to output file
_, err = io.Copy(out, resp.Body)
// return drfResponse.Results, &resp, nil

return outputPath, err
}
Loading