Address comments from review

This commit is contained in:
Dennis Oehme 2021-02-26 11:24:22 +01:00
parent af51cfebfc
commit 9029d1aa19
3 changed files with 35 additions and 43 deletions

View File

@ -9,7 +9,6 @@ import (
"net" "net"
"net/http" "net/http"
"os" "os"
"os/user"
"strconv" "strconv"
) )
@ -43,13 +42,6 @@ func NewSocketAdapter(host string, useTLS bool) *SocketAdapter {
//DoRequest performs the given IPP request to the given URL, returning the IPP response or an error if one occurred //DoRequest performs the given IPP request to the given URL, returning the IPP response or an error if one occurred
func (h *SocketAdapter) SendRequest(url string, r *Request, _ io.Writer) (*Response, error) { func (h *SocketAdapter) SendRequest(url string, r *Request, _ io.Writer) (*Response, error) {
// set user field
user, err := user.Current()
if err != nil {
return nil, fmt.Errorf("unable to lookup current user: %v", err)
}
r.OperationAttributes[AttributeRequestingUserName] = user.Username
for i := 0; i < h.requestRetryLimit; i++ { for i := 0; i < h.requestRetryLimit; i++ {
// encode request // encode request
payload, err := r.Encode() payload, err := r.Encode()

View File

@ -26,7 +26,7 @@ func NewCUPSClientWithAdapter(username string, adapter Adapter) *CUPSClient {
func (c *CUPSClient) GetDevices() (map[string]Attributes, error) { func (c *CUPSClient) GetDevices() (map[string]Attributes, error) {
req := NewRequest(OperationCupsGetDevices, 1) req := NewRequest(OperationCupsGetDevices, 1)
resp, err := c.SendRequest(c.getHttpUri("", nil), req, nil) resp, err := c.SendRequest(c.adapter.GetHttpUri("", nil), req, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -46,7 +46,7 @@ func (c *CUPSClient) MoveJob(jobID int, destPrinter string) error {
req.OperationAttributes[AttributeJobURI] = c.getJobUri(jobID) req.OperationAttributes[AttributeJobURI] = c.getJobUri(jobID)
req.PrinterAttributes[AttributeJobPrinterURI] = c.getPrinterUri(destPrinter) req.PrinterAttributes[AttributeJobPrinterURI] = c.getPrinterUri(destPrinter)
_, err := c.SendRequest(c.getHttpUri("jobs", ""), req, nil) _, err := c.SendRequest(c.adapter.GetHttpUri("jobs", ""), req, nil)
return err return err
} }
@ -56,7 +56,7 @@ func (c *CUPSClient) MoveAllJob(srcPrinter, destPrinter string) error {
req.OperationAttributes[AttributePrinterURI] = c.getPrinterUri(srcPrinter) req.OperationAttributes[AttributePrinterURI] = c.getPrinterUri(srcPrinter)
req.PrinterAttributes[AttributeJobPrinterURI] = c.getPrinterUri(destPrinter) req.PrinterAttributes[AttributeJobPrinterURI] = c.getPrinterUri(destPrinter)
_, err := c.SendRequest(c.getHttpUri("jobs", ""), req, nil) _, err := c.SendRequest(c.adapter.GetHttpUri("jobs", ""), req, nil)
return err return err
} }
@ -64,7 +64,7 @@ func (c *CUPSClient) MoveAllJob(srcPrinter, destPrinter string) error {
func (c *CUPSClient) GetPPDs() (map[string]Attributes, error) { func (c *CUPSClient) GetPPDs() (map[string]Attributes, error) {
req := NewRequest(OperationCupsGetPPDs, 1) req := NewRequest(OperationCupsGetPPDs, 1)
resp, err := c.SendRequest(c.getHttpUri("", nil), req, nil) resp, err := c.SendRequest(c.adapter.GetHttpUri("", nil), req, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -83,7 +83,7 @@ func (c *CUPSClient) AcceptJobs(printer string) error {
req := NewRequest(OperationCupsAcceptJobs, 1) req := NewRequest(OperationCupsAcceptJobs, 1)
req.OperationAttributes[AttributePrinterURI] = c.getPrinterUri(printer) req.OperationAttributes[AttributePrinterURI] = c.getPrinterUri(printer)
_, err := c.SendRequest(c.getHttpUri("admin", ""), req, nil) _, err := c.SendRequest(c.adapter.GetHttpUri("admin", ""), req, nil)
return err return err
} }
@ -92,7 +92,7 @@ func (c *CUPSClient) RejectJobs(printer string) error {
req := NewRequest(OperationCupsRejectJobs, 1) req := NewRequest(OperationCupsRejectJobs, 1)
req.OperationAttributes[AttributePrinterURI] = c.getPrinterUri(printer) req.OperationAttributes[AttributePrinterURI] = c.getPrinterUri(printer)
_, err := c.SendRequest(c.getHttpUri("admin", ""), req, nil) _, err := c.SendRequest(c.adapter.GetHttpUri("admin", ""), req, nil)
return err return err
} }
@ -124,7 +124,7 @@ func (c *CUPSClient) AddPrinterToClass(class, printer string) error {
req.OperationAttributes[AttributePrinterURI] = c.getClassUri(class) req.OperationAttributes[AttributePrinterURI] = c.getClassUri(class)
req.PrinterAttributes[AttributeMemberURIs] = memberURIList req.PrinterAttributes[AttributeMemberURIs] = memberURIList
_, err = c.SendRequest(c.getHttpUri("admin", ""), req, nil) _, err = c.SendRequest(c.adapter.GetHttpUri("admin", ""), req, nil)
return err return err
} }
@ -154,7 +154,7 @@ func (c *CUPSClient) DeletePrinterFromClass(class, printer string) error {
req.OperationAttributes[AttributePrinterURI] = c.getClassUri(class) req.OperationAttributes[AttributePrinterURI] = c.getClassUri(class)
req.PrinterAttributes[AttributeMemberURIs] = memberURIList req.PrinterAttributes[AttributeMemberURIs] = memberURIList
_, err = c.SendRequest(c.getHttpUri("admin", ""), req, nil) _, err = c.SendRequest(c.adapter.GetHttpUri("admin", ""), req, nil)
return err return err
} }
@ -163,7 +163,7 @@ func (c *CUPSClient) DeleteClass(class string) error {
req := NewRequest(OperationCupsDeleteClass, 1) req := NewRequest(OperationCupsDeleteClass, 1)
req.OperationAttributes[AttributePrinterURI] = c.getClassUri(class) req.OperationAttributes[AttributePrinterURI] = c.getClassUri(class)
_, err := c.SendRequest(c.getHttpUri("admin", ""), req, nil) _, err := c.SendRequest(c.adapter.GetHttpUri("admin", ""), req, nil)
return err return err
} }
@ -179,7 +179,7 @@ func (c *CUPSClient) CreatePrinter(name, deviceURI, ppd string, shared bool, err
req.PrinterAttributes[AttributePrinterLocation] = location req.PrinterAttributes[AttributePrinterLocation] = location
req.PrinterAttributes[AttributePrinterErrorPolicy] = string(errorPolicy) req.PrinterAttributes[AttributePrinterErrorPolicy] = string(errorPolicy)
_, err := c.SendRequest(c.getHttpUri("admin", ""), req, nil) _, err := c.SendRequest(c.adapter.GetHttpUri("admin", ""), req, nil)
return err return err
} }
@ -189,7 +189,7 @@ func (c *CUPSClient) SetPrinterPPD(printer, ppd string) error {
req.OperationAttributes[AttributePrinterURI] = c.getPrinterUri(printer) req.OperationAttributes[AttributePrinterURI] = c.getPrinterUri(printer)
req.OperationAttributes[AttributePPDName] = ppd req.OperationAttributes[AttributePPDName] = ppd
_, err := c.SendRequest(c.getHttpUri("admin", ""), req, nil) _, err := c.SendRequest(c.adapter.GetHttpUri("admin", ""), req, nil)
return err return err
} }
@ -199,7 +199,7 @@ func (c *CUPSClient) SetPrinterDeviceURI(printer, deviceURI string) error {
req.OperationAttributes[AttributePrinterURI] = c.getPrinterUri(printer) req.OperationAttributes[AttributePrinterURI] = c.getPrinterUri(printer)
req.PrinterAttributes[AttributeDeviceURI] = deviceURI req.PrinterAttributes[AttributeDeviceURI] = deviceURI
_, err := c.SendRequest(c.getHttpUri("admin", ""), req, nil) _, err := c.SendRequest(c.adapter.GetHttpUri("admin", ""), req, nil)
return err return err
} }
@ -209,7 +209,7 @@ func (c *CUPSClient) SetPrinterIsShared(printer string, shared bool) error {
req.OperationAttributes[AttributePrinterURI] = c.getPrinterUri(printer) req.OperationAttributes[AttributePrinterURI] = c.getPrinterUri(printer)
req.OperationAttributes[AttributePrinterIsShared] = shared req.OperationAttributes[AttributePrinterIsShared] = shared
_, err := c.SendRequest(c.getHttpUri("admin", ""), req, nil) _, err := c.SendRequest(c.adapter.GetHttpUri("admin", ""), req, nil)
return err return err
} }
@ -219,7 +219,7 @@ func (c *CUPSClient) SetPrinterErrorPolicy(printer string, errorPolicy string) e
req.OperationAttributes[AttributePrinterURI] = c.getPrinterUri(printer) req.OperationAttributes[AttributePrinterURI] = c.getPrinterUri(printer)
req.PrinterAttributes[AttributePrinterErrorPolicy] = string(errorPolicy) req.PrinterAttributes[AttributePrinterErrorPolicy] = string(errorPolicy)
_, err := c.SendRequest(c.getHttpUri("admin", ""), req, nil) _, err := c.SendRequest(c.adapter.GetHttpUri("admin", ""), req, nil)
return err return err
} }
@ -229,7 +229,7 @@ func (c *CUPSClient) SetPrinterInformation(printer, information string) error {
req.OperationAttributes[AttributePrinterURI] = c.getPrinterUri(printer) req.OperationAttributes[AttributePrinterURI] = c.getPrinterUri(printer)
req.PrinterAttributes[AttributePrinterInfo] = information req.PrinterAttributes[AttributePrinterInfo] = information
_, err := c.SendRequest(c.getHttpUri("admin", ""), req, nil) _, err := c.SendRequest(c.adapter.GetHttpUri("admin", ""), req, nil)
return err return err
} }
@ -239,7 +239,7 @@ func (c *CUPSClient) SetPrinterLocation(printer, location string) error {
req.OperationAttributes[AttributePrinterURI] = c.getPrinterUri(printer) req.OperationAttributes[AttributePrinterURI] = c.getPrinterUri(printer)
req.PrinterAttributes[AttributePrinterLocation] = location req.PrinterAttributes[AttributePrinterLocation] = location
_, err := c.SendRequest(c.getHttpUri("admin", ""), req, nil) _, err := c.SendRequest(c.adapter.GetHttpUri("admin", ""), req, nil)
return err return err
} }
@ -248,7 +248,7 @@ func (c *CUPSClient) DeletePrinter(printer string) error {
req := NewRequest(OperationCupsDeletePrinter, 1) req := NewRequest(OperationCupsDeletePrinter, 1)
req.OperationAttributes[AttributePrinterURI] = c.getPrinterUri(printer) req.OperationAttributes[AttributePrinterURI] = c.getPrinterUri(printer)
_, err := c.SendRequest(c.getHttpUri("admin", ""), req, nil) _, err := c.SendRequest(c.adapter.GetHttpUri("admin", ""), req, nil)
return err return err
} }
@ -262,7 +262,7 @@ func (c *CUPSClient) GetPrinters(attributes []string) (map[string]Attributes, er
req.OperationAttributes[AttributeRequestedAttributes] = append(attributes, AttributePrinterName) req.OperationAttributes[AttributeRequestedAttributes] = append(attributes, AttributePrinterName)
} }
resp, err := c.SendRequest(c.getHttpUri("", nil), req, nil) resp, err := c.SendRequest(c.adapter.GetHttpUri("", nil), req, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -286,7 +286,7 @@ func (c *CUPSClient) GetClasses(attributes []string) (map[string]Attributes, err
req.OperationAttributes[AttributeRequestedAttributes] = append(attributes, AttributePrinterName) req.OperationAttributes[AttributeRequestedAttributes] = append(attributes, AttributePrinterName)
} }
resp, err := c.SendRequest(c.getHttpUri("", nil), req, nil) resp, err := c.SendRequest(c.adapter.GetHttpUri("", nil), req, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -40,10 +40,6 @@ func NewIPPClientWithAdapter(username string, adapter Adapter) *IPPClient {
} }
} }
func (c *IPPClient) getHttpUri(namespace string, object interface{}) string {
return c.adapter.GetHttpUri(namespace, object)
}
func (c *IPPClient) getPrinterUri(printer string) string { func (c *IPPClient) getPrinterUri(printer string) string {
return fmt.Sprintf("ipp://localhost/printers/%s", printer) return fmt.Sprintf("ipp://localhost/printers/%s", printer)
} }
@ -58,6 +54,10 @@ func (c *IPPClient) getClassUri(printer string) string {
// SendRequest sends a request to a remote uri end returns the response // SendRequest sends a request to a remote uri end returns the response
func (c *IPPClient) SendRequest(url string, req *Request, additionalResponseData io.Writer) (*Response, error) { func (c *IPPClient) SendRequest(url string, req *Request, additionalResponseData io.Writer) (*Response, error) {
if _, ok := req.OperationAttributes[AttributeRequestingUserName]; ok == false {
req.OperationAttributes[AttributeRequestingUserName] = c.username
}
return c.adapter.SendRequest(url, req, additionalResponseData) return c.adapter.SendRequest(url, req, additionalResponseData)
} }
@ -78,7 +78,7 @@ func (c *IPPClient) PrintDocuments(docs []Document, printer string, jobAttribute
req.JobAttributes[key] = value req.JobAttributes[key] = value
} }
resp, err := c.SendRequest(c.getHttpUri("printers", printer), req, nil) resp, err := c.SendRequest(c.adapter.GetHttpUri("printers", printer), req, nil)
if err != nil { if err != nil {
return -1, err return -1, err
} }
@ -102,7 +102,7 @@ func (c *IPPClient) PrintDocuments(docs []Document, printer string, jobAttribute
req.File = doc.Document req.File = doc.Document
req.FileSize = doc.Size req.FileSize = doc.Size
_, err = c.SendRequest(c.getHttpUri("printers", printer), req, nil) _, err = c.SendRequest(c.adapter.GetHttpUri("printers", printer), req, nil)
if err != nil { if err != nil {
return -1, err return -1, err
} }
@ -132,7 +132,7 @@ func (c *IPPClient) PrintJob(doc Document, printer string, jobAttributes map[str
req.File = doc.Document req.File = doc.Document
req.FileSize = doc.Size req.FileSize = doc.Size
resp, err := c.SendRequest(c.getHttpUri("printers", printer), req, nil) resp, err := c.SendRequest(c.adapter.GetHttpUri("printers", printer), req, nil)
if err != nil { if err != nil {
return -1, err return -1, err
} }
@ -185,7 +185,7 @@ func (c *IPPClient) GetPrinterAttributes(printer string, attributes []string) (A
req.OperationAttributes[AttributeRequestedAttributes] = attributes req.OperationAttributes[AttributeRequestedAttributes] = attributes
} }
resp, err := c.SendRequest(c.getHttpUri("printers", printer), req, nil) resp, err := c.SendRequest(c.adapter.GetHttpUri("printers", printer), req, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -202,7 +202,7 @@ func (c *IPPClient) ResumePrinter(printer string) error {
req := NewRequest(OperationResumePrinter, 1) req := NewRequest(OperationResumePrinter, 1)
req.OperationAttributes[AttributePrinterURI] = c.getPrinterUri(printer) req.OperationAttributes[AttributePrinterURI] = c.getPrinterUri(printer)
_, err := c.SendRequest(c.getHttpUri("admin", ""), req, nil) _, err := c.SendRequest(c.adapter.GetHttpUri("admin", ""), req, nil)
return err return err
} }
@ -211,7 +211,7 @@ func (c *IPPClient) PausePrinter(printer string) error {
req := NewRequest(OperationPausePrinter, 1) req := NewRequest(OperationPausePrinter, 1)
req.OperationAttributes[AttributePrinterURI] = c.getPrinterUri(printer) req.OperationAttributes[AttributePrinterURI] = c.getPrinterUri(printer)
_, err := c.SendRequest(c.getHttpUri("admin", ""), req, nil) _, err := c.SendRequest(c.adapter.GetHttpUri("admin", ""), req, nil)
return err return err
} }
@ -226,7 +226,7 @@ func (c *IPPClient) GetJobAttributes(jobID int, attributes []string) (Attributes
req.OperationAttributes[AttributeRequestedAttributes] = attributes req.OperationAttributes[AttributeRequestedAttributes] = attributes
} }
resp, err := c.SendRequest(c.getHttpUri("jobs", jobID), req, nil) resp, err := c.SendRequest(c.adapter.GetHttpUri("jobs", jobID), req, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -270,7 +270,7 @@ func (c *IPPClient) GetJobs(printer, class string, whichJobs string, myJobs bool
req.OperationAttributes[AttributeRequestedAttributes] = append(attributes, AttributeJobID) req.OperationAttributes[AttributeRequestedAttributes] = append(attributes, AttributeJobID)
} }
resp, err := c.SendRequest(c.getHttpUri("", nil), req, nil) resp, err := c.SendRequest(c.adapter.GetHttpUri("", nil), req, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -290,7 +290,7 @@ func (c *IPPClient) CancelJob(jobID int, purge bool) error {
req.OperationAttributes[AttributeJobURI] = c.getJobUri(jobID) req.OperationAttributes[AttributeJobURI] = c.getJobUri(jobID)
req.OperationAttributes[AttributePurgeJobs] = purge req.OperationAttributes[AttributePurgeJobs] = purge
_, err := c.SendRequest(c.getHttpUri("jobs", ""), req, nil) _, err := c.SendRequest(c.adapter.GetHttpUri("jobs", ""), req, nil)
return err return err
} }
@ -300,7 +300,7 @@ func (c *IPPClient) CancelAllJob(printer string, purge bool) error {
req.OperationAttributes[AttributePrinterURI] = c.getPrinterUri(printer) req.OperationAttributes[AttributePrinterURI] = c.getPrinterUri(printer)
req.OperationAttributes[AttributePurgeJobs] = purge req.OperationAttributes[AttributePurgeJobs] = purge
_, err := c.SendRequest(c.getHttpUri("admin", ""), req, nil) _, err := c.SendRequest(c.adapter.GetHttpUri("admin", ""), req, nil)
return err return err
} }
@ -309,7 +309,7 @@ func (c *IPPClient) RestartJob(jobID int) error {
req := NewRequest(OperationRestartJob, 1) req := NewRequest(OperationRestartJob, 1)
req.OperationAttributes[AttributeJobURI] = c.getJobUri(jobID) req.OperationAttributes[AttributeJobURI] = c.getJobUri(jobID)
_, err := c.SendRequest(c.getHttpUri("jobs", ""), req, nil) _, err := c.SendRequest(c.adapter.GetHttpUri("jobs", ""), req, nil)
return err return err
} }
@ -319,7 +319,7 @@ func (c *IPPClient) HoldJobUntil(jobID int, holdUntil string) error {
req.OperationAttributes[AttributeJobURI] = c.getJobUri(jobID) req.OperationAttributes[AttributeJobURI] = c.getJobUri(jobID)
req.JobAttributes[AttributeHoldJobUntil] = holdUntil req.JobAttributes[AttributeHoldJobUntil] = holdUntil
_, err := c.SendRequest(c.getHttpUri("jobs", ""), req, nil) _, err := c.SendRequest(c.adapter.GetHttpUri("jobs", ""), req, nil)
return err return err
} }