Merge pull request #5 from phin1x/unify-print-methods

unified print functions
This commit is contained in:
Fabian 2019-06-09 00:05:57 +02:00 committed by GitHub
commit 336a8e359f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 134 additions and 89 deletions

View File

@ -296,6 +296,37 @@ const (
MimeTypeOctetStream = "application/octet-stream" MimeTypeOctetStream = "application/octet-stream"
) )
const (
OperationAttributeCopies string = "copies"
OperationAttributeDocumentFormat = "document-format"
OperationAttributeDocumentName = "document-name"
OperationAttributeJobID = "job-id"
OperationAttributeJobName = "job-name"
OperationAttributeJobPriority = "job-priority"
OperationAttributeJobURI = "job-uri"
OperationAttributeLastDocument = "last-document"
OperationAttributeMyJobs = "my-jobs"
OperationAttributePPDName = "ppd-name"
OperationAttributePrinterIsShared = "printer-is-shared"
OperationAttributePrinterURI = "printer-uri"
OperationAttributePurgeJobs = "purge-jobs"
OperationAttributeRequestedAttributes = "requested-attributes"
OperationAttributeRequestingUserName = "requesting-user-name"
OperationAttributeWhichJobs = "which-jobs"
)
const (
PrinterAttributeDeviceURI string = "device-uri"
PrinterAttributeHoldJobUntil = "job-hold-until"
PrinterAttributePrinterErrorPolicy = "printer-error-policy"
PrinterAttributePrinterInfo = "printer-info"
PrinterAttributePrinterLocation = "printer-location"
PrinterAttributePrinterName = "printer-name"
PrinterAttributePrinterStateReason = "printer-state-reason"
PrinterAttributeJobPrinterURI = "job-printer-uri"
PrinterAttributeMemberURIs = "member-uris"
)
var ( var (
DefaultClassAttributes = []string{"printer-name", "member-names"} DefaultClassAttributes = []string{"printer-name", "member-names"}
DefaultPrinterAttributes = []string{"printer-name", "printer-type", "printer-location", "printer-info", DefaultPrinterAttributes = []string{"printer-name", "printer-type", "printer-location", "printer-info",

View File

@ -22,7 +22,7 @@ func (c *CUPSClient) GetDevices() (map[string]Attributes, error) {
printerNameMap := make(map[string]Attributes) printerNameMap := make(map[string]Attributes)
for _, printerAttributes := range resp.Printers { for _, printerAttributes := range resp.Printers {
printerNameMap[printerAttributes["device-uri"][0].Value.(string)] = printerAttributes printerNameMap[printerAttributes[PrinterAttributeDeviceURI][0].Value.(string)] = printerAttributes
} }
return printerNameMap, nil return printerNameMap, nil
@ -30,8 +30,8 @@ func (c *CUPSClient) GetDevices() (map[string]Attributes, error) {
func (c *CUPSClient) MoveJob(jobID int, destPrinter string) error { func (c *CUPSClient) MoveJob(jobID int, destPrinter string) error {
req := NewRequest(OperationCupsMoveJob, 1) req := NewRequest(OperationCupsMoveJob, 1)
req.OperationAttributes["job-uri"] = c.getJobUri(jobID) req.OperationAttributes[OperationAttributeJobURI] = c.getJobUri(jobID)
req.PrinterAttributes["job-printer-uri"] = c.getPrinterUri(destPrinter) req.PrinterAttributes[PrinterAttributeJobPrinterURI] = c.getPrinterUri(destPrinter)
_, err := c.SendRequest(c.getHttpUri("jobs", ""), req) _, err := c.SendRequest(c.getHttpUri("jobs", ""), req)
return err return err
@ -39,8 +39,8 @@ func (c *CUPSClient) MoveJob(jobID int, destPrinter string) error {
func (c *CUPSClient) MoveAllJob(srcPrinter, destPrinter string) error { func (c *CUPSClient) MoveAllJob(srcPrinter, destPrinter string) error {
req := NewRequest(OperationCupsMoveJob, 1) req := NewRequest(OperationCupsMoveJob, 1)
req.OperationAttributes["printer-uri"] = c.getPrinterUri(srcPrinter) req.OperationAttributes[OperationAttributePrinterURI] = c.getPrinterUri(srcPrinter)
req.PrinterAttributes["job-printer-uri"] = c.getPrinterUri(destPrinter) req.PrinterAttributes[PrinterAttributeJobPrinterURI] = c.getPrinterUri(destPrinter)
_, err := c.SendRequest(c.getHttpUri("jobs", ""), req) _, err := c.SendRequest(c.getHttpUri("jobs", ""), req)
return err return err
@ -57,7 +57,7 @@ func (c *CUPSClient) GetPPDs() (map[string]Attributes, error) {
ppdNameMap := make(map[string]Attributes) ppdNameMap := make(map[string]Attributes)
for _, printerAttributes := range resp.Printers { for _, printerAttributes := range resp.Printers {
ppdNameMap[printerAttributes["ppd-name"][0].Value.(string)] = printerAttributes ppdNameMap[printerAttributes[OperationAttributePPDName][0].Value.(string)] = printerAttributes
} }
return ppdNameMap, nil return ppdNameMap, nil
@ -65,7 +65,7 @@ func (c *CUPSClient) GetPPDs() (map[string]Attributes, error) {
func (c *CUPSClient) AcceptJobs(printer string) error { func (c *CUPSClient) AcceptJobs(printer string) error {
req := NewRequest(OperationCupsAcceptJobs, 1) req := NewRequest(OperationCupsAcceptJobs, 1)
req.OperationAttributes["printer-uri"] = c.getPrinterUri(printer) req.OperationAttributes[OperationAttributePrinterURI] = c.getPrinterUri(printer)
_, err := c.SendRequest(c.getHttpUri("admin", ""), req) _, err := c.SendRequest(c.getHttpUri("admin", ""), req)
return err return err
@ -73,14 +73,14 @@ func (c *CUPSClient) AcceptJobs(printer string) error {
func (c *CUPSClient) RejectJobs(printer string) error { func (c *CUPSClient) RejectJobs(printer string) error {
req := NewRequest(OperationCupsRejectJobs, 1) req := NewRequest(OperationCupsRejectJobs, 1)
req.OperationAttributes["printer-uri"] = c.getPrinterUri(printer) req.OperationAttributes[OperationAttributePrinterURI] = c.getPrinterUri(printer)
_, err := c.SendRequest(c.getHttpUri("admin", ""), req) _, err := c.SendRequest(c.getHttpUri("admin", ""), req)
return err return err
} }
func (c *CUPSClient) AddPrinterToClass(class, printer string) error { func (c *CUPSClient) AddPrinterToClass(class, printer string) error {
attributes, err := c.GetPrinterAttributes(class, []string{"member-uris"}) attributes, err := c.GetPrinterAttributes(class, []string{PrinterAttributeMemberURIs})
if err != nil && !IsNotExistsError(err) { if err != nil && !IsNotExistsError(err) {
return err return err
} }
@ -88,7 +88,7 @@ func (c *CUPSClient) AddPrinterToClass(class, printer string) error {
memberURIList := make([]string, 0) memberURIList := make([]string, 0)
if !IsNotExistsError(err) { if !IsNotExistsError(err) {
for _, member := range attributes["member-uris"] { for _, member := range attributes[PrinterAttributeMemberURIs] {
memberString := strings.Split(member.Value.(string), "/") memberString := strings.Split(member.Value.(string), "/")
printerName := memberString[len(memberString)-1] printerName := memberString[len(memberString)-1]
@ -103,22 +103,22 @@ func (c *CUPSClient) AddPrinterToClass(class, printer string) error {
memberURIList = append(memberURIList, c.getPrinterUri(printer)) memberURIList = append(memberURIList, c.getPrinterUri(printer))
req := NewRequest(OperationCupsAddModifyClass, 1) req := NewRequest(OperationCupsAddModifyClass, 1)
req.OperationAttributes["printer-uri"] = c.getClassUri(class) req.OperationAttributes[OperationAttributePrinterURI] = c.getClassUri(class)
req.PrinterAttributes["member-uris"] = memberURIList req.PrinterAttributes[PrinterAttributeMemberURIs] = memberURIList
_, err = c.SendRequest(c.getHttpUri("admin", ""), req) _, err = c.SendRequest(c.getHttpUri("admin", ""), req)
return err return err
} }
func (c *CUPSClient) DeletePrinterFromClass(class, printer string) error { func (c *CUPSClient) DeletePrinterFromClass(class, printer string) error {
attributes, err := c.GetPrinterAttributes(class, []string{"member-uris"}) attributes, err := c.GetPrinterAttributes(class, []string{PrinterAttributeMemberURIs})
if err != nil { if err != nil {
return err return err
} }
memberURIList := make([]string, 0) memberURIList := make([]string, 0)
for _, member := range attributes["member-uris"] { for _, member := range attributes[PrinterAttributeMemberURIs] {
memberString := strings.Split(member.Value.(string), "/") memberString := strings.Split(member.Value.(string), "/")
printerName := memberString[len(memberString)-1] printerName := memberString[len(memberString)-1]
@ -132,8 +132,8 @@ func (c *CUPSClient) DeletePrinterFromClass(class, printer string) error {
} }
req := NewRequest(OperationCupsAddModifyClass, 1) req := NewRequest(OperationCupsAddModifyClass, 1)
req.OperationAttributes["printer-uri"] = c.getClassUri(class) req.OperationAttributes[OperationAttributePrinterURI] = c.getClassUri(class)
req.PrinterAttributes["member-uris"] = memberURIList req.PrinterAttributes[PrinterAttributeMemberURIs] = memberURIList
_, err = c.SendRequest(c.getHttpUri("admin", ""), req) _, err = c.SendRequest(c.getHttpUri("admin", ""), req)
return err return err
@ -141,7 +141,7 @@ func (c *CUPSClient) DeletePrinterFromClass(class, printer string) error {
func (c *CUPSClient) DeleteClass(class string) error { func (c *CUPSClient) DeleteClass(class string) error {
req := NewRequest(OperationCupsDeleteClass, 1) req := NewRequest(OperationCupsDeleteClass, 1)
req.OperationAttributes["printer-uri"] = c.getClassUri(class) req.OperationAttributes[OperationAttributePrinterURI] = c.getClassUri(class)
_, err := c.SendRequest(c.getHttpUri("admin", ""), req) _, err := c.SendRequest(c.getHttpUri("admin", ""), req)
return err return err
@ -149,14 +149,14 @@ func (c *CUPSClient) DeleteClass(class string) error {
func (c *CUPSClient) CreatePrinter(name, deviceURI, ppd string, shared bool, errorPolicy ErrorPolicy, information, location string) error { func (c *CUPSClient) CreatePrinter(name, deviceURI, ppd string, shared bool, errorPolicy ErrorPolicy, information, location string) error {
req := NewRequest(OperationCupsAddModifyPrinter, 1) req := NewRequest(OperationCupsAddModifyPrinter, 1)
req.OperationAttributes["printer-uri"] = c.getPrinterUri(name) req.OperationAttributes[OperationAttributePrinterURI] = c.getPrinterUri(name)
req.OperationAttributes["ppd-name"] = ppd req.OperationAttributes[OperationAttributePPDName] = ppd
req.OperationAttributes["printer-is-shared"] = shared req.OperationAttributes[OperationAttributePrinterIsShared] = shared
req.PrinterAttributes["printer-state-reason"] = "none" req.PrinterAttributes[PrinterAttributePrinterStateReason] = "none"
req.PrinterAttributes["device-uri"] = deviceURI req.PrinterAttributes[PrinterAttributeDeviceURI] = deviceURI
req.PrinterAttributes["printer-info"] = information req.PrinterAttributes[PrinterAttributePrinterInfo] = information
req.PrinterAttributes["printer-location"] = location req.PrinterAttributes[PrinterAttributePrinterLocation] = location
req.PrinterAttributes["printer-error-policy"] = string(errorPolicy) req.PrinterAttributes[PrinterAttributePrinterErrorPolicy] = string(errorPolicy)
_, err := c.SendRequest(c.getHttpUri("admin", ""), req) _, err := c.SendRequest(c.getHttpUri("admin", ""), req)
return err return err
@ -164,8 +164,8 @@ func (c *CUPSClient) CreatePrinter(name, deviceURI, ppd string, shared bool, err
func (c *CUPSClient) SetPrinterPPD(printer, ppd string) error { func (c *CUPSClient) SetPrinterPPD(printer, ppd string) error {
req := NewRequest(OperationCupsAddModifyPrinter, 1) req := NewRequest(OperationCupsAddModifyPrinter, 1)
req.OperationAttributes["printer-uri"] = c.getPrinterUri(printer) req.OperationAttributes[OperationAttributePrinterURI] = c.getPrinterUri(printer)
req.OperationAttributes["ppd-name"] = ppd req.OperationAttributes[OperationAttributePPDName] = ppd
_, err := c.SendRequest(c.getHttpUri("admin", ""), req) _, err := c.SendRequest(c.getHttpUri("admin", ""), req)
return err return err
@ -173,8 +173,8 @@ func (c *CUPSClient) SetPrinterPPD(printer, ppd string) error {
func (c *CUPSClient) SetPrinterDeviceURI(printer, deviceURI string) error { func (c *CUPSClient) SetPrinterDeviceURI(printer, deviceURI string) error {
req := NewRequest(OperationCupsAddModifyPrinter, 1) req := NewRequest(OperationCupsAddModifyPrinter, 1)
req.OperationAttributes["printer-uri"] = c.getPrinterUri(printer) req.OperationAttributes[OperationAttributePrinterURI] = c.getPrinterUri(printer)
req.PrinterAttributes["device-uri"] = deviceURI req.PrinterAttributes[PrinterAttributeDeviceURI] = deviceURI
_, err := c.SendRequest(c.getHttpUri("admin", ""), req) _, err := c.SendRequest(c.getHttpUri("admin", ""), req)
return err return err
@ -182,8 +182,8 @@ func (c *CUPSClient) SetPrinterDeviceURI(printer, deviceURI string) error {
func (c *CUPSClient) SetPrinterIsShared(printer string, shared bool) error { func (c *CUPSClient) SetPrinterIsShared(printer string, shared bool) error {
req := NewRequest(OperationCupsAddModifyPrinter, 1) req := NewRequest(OperationCupsAddModifyPrinter, 1)
req.OperationAttributes["printer-uri"] = c.getPrinterUri(printer) req.OperationAttributes[OperationAttributePrinterURI] = c.getPrinterUri(printer)
req.OperationAttributes["printer-is-shared"] = shared req.OperationAttributes[OperationAttributePrinterIsShared] = shared
_, err := c.SendRequest(c.getHttpUri("admin", ""), req) _, err := c.SendRequest(c.getHttpUri("admin", ""), req)
return err return err
@ -191,8 +191,8 @@ func (c *CUPSClient) SetPrinterIsShared(printer string, shared bool) error {
func (c *CUPSClient) SetPrinterErrorPolicy(printer string, errorPolicy ErrorPolicy) error { func (c *CUPSClient) SetPrinterErrorPolicy(printer string, errorPolicy ErrorPolicy) error {
req := NewRequest(OperationCupsAddModifyPrinter, 1) req := NewRequest(OperationCupsAddModifyPrinter, 1)
req.OperationAttributes["printer-uri"] = c.getPrinterUri(printer) req.OperationAttributes[OperationAttributePrinterURI] = c.getPrinterUri(printer)
req.PrinterAttributes["printer-error-policy"] = string(errorPolicy) req.PrinterAttributes[PrinterAttributePrinterErrorPolicy] = string(errorPolicy)
_, err := c.SendRequest(c.getHttpUri("admin", ""), req) _, err := c.SendRequest(c.getHttpUri("admin", ""), req)
return err return err
@ -200,8 +200,8 @@ func (c *CUPSClient) SetPrinterErrorPolicy(printer string, errorPolicy ErrorPoli
func (c *CUPSClient) SetPrinterInformation(printer, information string) error { func (c *CUPSClient) SetPrinterInformation(printer, information string) error {
req := NewRequest(OperationCupsAddModifyPrinter, 1) req := NewRequest(OperationCupsAddModifyPrinter, 1)
req.OperationAttributes["printer-uri"] = c.getPrinterUri(printer) req.OperationAttributes[OperationAttributePrinterURI] = c.getPrinterUri(printer)
req.PrinterAttributes["printer-info"] = information req.PrinterAttributes[PrinterAttributePrinterInfo] = information
_, err := c.SendRequest(c.getHttpUri("admin", ""), req) _, err := c.SendRequest(c.getHttpUri("admin", ""), req)
return err return err
@ -209,8 +209,8 @@ func (c *CUPSClient) SetPrinterInformation(printer, information string) error {
func (c *CUPSClient) SetPrinterLocation(printer, location string) error { func (c *CUPSClient) SetPrinterLocation(printer, location string) error {
req := NewRequest(OperationCupsAddModifyPrinter, 1) req := NewRequest(OperationCupsAddModifyPrinter, 1)
req.OperationAttributes["printer-uri"] = c.getPrinterUri(printer) req.OperationAttributes[OperationAttributePrinterURI] = c.getPrinterUri(printer)
req.PrinterAttributes["printer-location"] = location req.PrinterAttributes[PrinterAttributePrinterLocation] = location
_, err := c.SendRequest(c.getHttpUri("admin", ""), req) _, err := c.SendRequest(c.getHttpUri("admin", ""), req)
return err return err
@ -218,7 +218,7 @@ func (c *CUPSClient) SetPrinterLocation(printer, location string) error {
func (c *CUPSClient) DeletePrinter(printer string) error { func (c *CUPSClient) DeletePrinter(printer string) error {
req := NewRequest(OperationCupsDeletePrinter, 1) req := NewRequest(OperationCupsDeletePrinter, 1)
req.OperationAttributes["printer-uri"] = c.getPrinterUri(printer) req.OperationAttributes[OperationAttributePrinterURI] = c.getPrinterUri(printer)
_, err := c.SendRequest(c.getHttpUri("admin", ""), req) _, err := c.SendRequest(c.getHttpUri("admin", ""), req)
return err return err
@ -228,9 +228,9 @@ func (c *CUPSClient) GetPrinters(attributes []string) (map[string]Attributes, er
req := NewRequest(OperationCupsGetPrinters, 1) req := NewRequest(OperationCupsGetPrinters, 1)
if attributes == nil { if attributes == nil {
req.OperationAttributes["requested-attributes"] = DefaultPrinterAttributes req.OperationAttributes[OperationAttributeRequestedAttributes] = DefaultPrinterAttributes
} else { } else {
req.OperationAttributes["requested-attributes"] = append(attributes, "printer-name") req.OperationAttributes[OperationAttributeRequestedAttributes] = append(attributes, PrinterAttributePrinterName)
} }
resp, err := c.SendRequest(c.getHttpUri("", nil), req) resp, err := c.SendRequest(c.getHttpUri("", nil), req)
@ -241,7 +241,7 @@ func (c *CUPSClient) GetPrinters(attributes []string) (map[string]Attributes, er
printerNameMap := make(map[string]Attributes) printerNameMap := make(map[string]Attributes)
for _, printerAttributes := range resp.Printers { for _, printerAttributes := range resp.Printers {
printerNameMap[printerAttributes["printer-name"][0].Value.(string)] = printerAttributes printerNameMap[printerAttributes[PrinterAttributePrinterName][0].Value.(string)] = printerAttributes
} }
return printerNameMap, nil return printerNameMap, nil
@ -251,9 +251,9 @@ func (c *CUPSClient) GetClasses(attributes []string) (map[string]Attributes, err
req := NewRequest(OperationCupsGetClasses, 1) req := NewRequest(OperationCupsGetClasses, 1)
if attributes == nil { if attributes == nil {
req.OperationAttributes["requested-attributes"] = DefaultClassAttributes req.OperationAttributes[OperationAttributeRequestedAttributes] = DefaultClassAttributes
} else { } else {
req.OperationAttributes["requested-attributes"] = append(attributes, "printer-name") req.OperationAttributes[OperationAttributeRequestedAttributes] = append(attributes, PrinterAttributePrinterName)
} }
resp, err := c.SendRequest(c.getHttpUri("", nil), req) resp, err := c.SendRequest(c.getHttpUri("", nil), req)
@ -264,7 +264,7 @@ func (c *CUPSClient) GetClasses(attributes []string) (map[string]Attributes, err
printerNameMap := make(map[string]Attributes) printerNameMap := make(map[string]Attributes)
for _, printerAttributes := range resp.Printers { for _, printerAttributes := range resp.Printers {
printerNameMap[printerAttributes["printer-name"][0].Value.(string)] = printerAttributes printerNameMap[printerAttributes[PrinterAttributePrinterName][0].Value.(string)] = printerAttributes
} }
return printerNameMap, nil return printerNameMap, nil

View File

@ -126,15 +126,21 @@ func (c *IPPClient) SendRequest(url string, req *Request) (*Response, error) {
} }
// Print one or more `Document`s using IPP `Create-Job` followed by `Send-Document` request(s). // Print one or more `Document`s using IPP `Create-Job` followed by `Send-Document` request(s).
func (c *IPPClient) Print(docs []Document, printer, jobName string, copies, priority int) (int, error) { func (c *IPPClient) PrintDocuments(docs []Document, printer string, jobAttributes map[string]interface{}) (int, error) {
printerURI := c.getPrinterUri(printer) printerURI := c.getPrinterUri(printer)
req := NewRequest(OperationCreateJob, 1) req := NewRequest(OperationCreateJob, 1)
req.OperationAttributes["printer-uri"] = printerURI req.OperationAttributes[OperationAttributePrinterURI] = printerURI
req.OperationAttributes["requesting-user-name"] = c.username req.OperationAttributes[OperationAttributeRequestingUserName] = c.username
req.OperationAttributes["job-name"] = jobName
req.JobAttributes["copies"] = copies // set defaults for some attributes, may get overwritten
req.JobAttributes["job-priority"] = priority req.OperationAttributes[OperationAttributeJobName] = docs[0].Name
req.OperationAttributes[OperationAttributeCopies] = 1
req.OperationAttributes[OperationAttributeJobPriority] = DefaultJobPriority
for key, value := range jobAttributes {
req.JobAttributes[key] = value
}
resp, err := c.SendRequest(c.getHttpUri("printers", printer), req) resp, err := c.SendRequest(c.getHttpUri("printers", printer), req)
if err != nil { if err != nil {
@ -145,18 +151,18 @@ func (c *IPPClient) Print(docs []Document, printer, jobName string, copies, prio
return 0, errors.New("server doesn't returned a job id") return 0, errors.New("server doesn't returned a job id")
} }
jobID := resp.Jobs[0]["job-id"][0].Value.(int) jobID := resp.Jobs[0][OperationAttributeJobID][0].Value.(int)
documentCount := len(docs) - 1 documentCount := len(docs) - 1
for docID, doc := range docs { for docID, doc := range docs {
req = NewRequest(OperationSendDocument, 2) req = NewRequest(OperationSendDocument, 2)
req.OperationAttributes["printer-uri"] = printerURI req.OperationAttributes[OperationAttributePrinterURI] = printerURI
req.OperationAttributes["requesting-user-name"] = c.username req.OperationAttributes[OperationAttributeRequestingUserName] = c.username
req.OperationAttributes["job-id"] = jobID req.OperationAttributes[OperationAttributeJobID] = jobID
req.OperationAttributes["document-name"] = doc.Name req.OperationAttributes[OperationAttributeDocumentName] = doc.Name
req.OperationAttributes["document-format"] = doc.MimeType req.OperationAttributes[OperationAttributeDocumentFormat] = doc.MimeType
req.OperationAttributes["last-document"] = docID == documentCount req.OperationAttributes[OperationAttributeLastDocument] = docID == documentCount
req.File = doc.Document req.File = doc.Document
req.FileSize = doc.Size req.FileSize = doc.Size
@ -190,10 +196,14 @@ func (c *IPPClient) PrintJob(doc Document, printer string, jobAttributes map[str
printerURI := c.getPrinterUri(printer) printerURI := c.getPrinterUri(printer)
req := NewRequest(OperationPrintJob, 1) req := NewRequest(OperationPrintJob, 1)
req.OperationAttributes["printer-uri"] = printerURI req.OperationAttributes[OperationAttributePrinterURI] = printerURI
req.OperationAttributes["requesting-user-name"] = c.username req.OperationAttributes[OperationAttributeRequestingUserName] = c.username
req.OperationAttributes["job-name"] = doc.Name req.OperationAttributes[OperationAttributeJobName] = doc.Name
req.OperationAttributes["document-format"] = doc.MimeType req.OperationAttributes[OperationAttributeDocumentFormat] = doc.MimeType
// set defaults for some attributes, may get overwritten
req.OperationAttributes[OperationAttributeCopies] = 1
req.OperationAttributes[OperationAttributeJobPriority] = DefaultJobPriority
for key, value := range jobAttributes { for key, value := range jobAttributes {
req.JobAttributes[key] = value req.JobAttributes[key] = value
@ -211,12 +221,12 @@ func (c *IPPClient) PrintJob(doc Document, printer string, jobAttributes map[str
return 0, errors.New("server doesn't returned a job id") return 0, errors.New("server doesn't returned a job id")
} }
jobID := resp.Jobs[0]["job-id"][0].Value.(int) jobID := resp.Jobs[0][OperationAttributeJobID][0].Value.(int)
return jobID, nil return jobID, nil
} }
func (c *IPPClient) PrintFile(filePath, printer string, copies, priority int) (int, error) { func (c *IPPClient) PrintFile(filePath, printer string, jobAttributes map[string]interface{}) (int, error) {
fileStats, err := os.Stat(filePath) fileStats, err := os.Stat(filePath)
if os.IsNotExist(err) { if os.IsNotExist(err) {
return -1, err return -1, err
@ -230,25 +240,27 @@ func (c *IPPClient) PrintFile(filePath, printer string, copies, priority int) (i
} }
defer document.Close() defer document.Close()
return c.Print([]Document{ jobAttributes[OperationAttributeJobName] = fileName
return c.PrintDocuments([]Document{
{ {
Document: document, Document: document,
Name: fileName, Name: fileName,
Size: int(fileStats.Size()), Size: int(fileStats.Size()),
MimeType: MimeTypeOctetStream, MimeType: MimeTypeOctetStream,
}, },
}, printer, fileName, copies, priority) }, printer, jobAttributes)
} }
func (c *IPPClient) GetPrinterAttributes(printer string, attributes []string) (Attributes, error) { func (c *IPPClient) GetPrinterAttributes(printer string, attributes []string) (Attributes, error) {
req := NewRequest(OperationGetPrinterAttributes, 1) req := NewRequest(OperationGetPrinterAttributes, 1)
req.OperationAttributes["printer-uri"] = c.getPrinterUri(printer) req.OperationAttributes[OperationAttributePrinterURI] = c.getPrinterUri(printer)
req.OperationAttributes["requesting-user-name"] = c.username req.OperationAttributes[OperationAttributeRequestingUserName] = c.username
if attributes == nil { if attributes == nil {
req.OperationAttributes["requested-attributes"] = DefaultPrinterAttributes req.OperationAttributes[OperationAttributeRequestedAttributes] = DefaultPrinterAttributes
} else { } else {
req.OperationAttributes["requested-attributes"] = attributes req.OperationAttributes[OperationAttributeRequestedAttributes] = attributes
} }
resp, err := c.SendRequest(c.getHttpUri("printers", printer), req) resp, err := c.SendRequest(c.getHttpUri("printers", printer), req)
@ -265,7 +277,7 @@ func (c *IPPClient) GetPrinterAttributes(printer string, attributes []string) (A
func (c *IPPClient) ResumePrinter(printer string) error { func (c *IPPClient) ResumePrinter(printer string) error {
req := NewRequest(OperationResumePrinter, 1) req := NewRequest(OperationResumePrinter, 1)
req.OperationAttributes["printer-uri"] = c.getPrinterUri(printer) req.OperationAttributes[OperationAttributePrinterURI] = c.getPrinterUri(printer)
_, err := c.SendRequest(c.getHttpUri("admin", ""), req) _, err := c.SendRequest(c.getHttpUri("admin", ""), req)
return err return err
@ -273,7 +285,7 @@ func (c *IPPClient) ResumePrinter(printer string) error {
func (c *IPPClient) PausePrinter(printer string) error { func (c *IPPClient) PausePrinter(printer string) error {
req := NewRequest(OperationPausePrinter, 1) req := NewRequest(OperationPausePrinter, 1)
req.OperationAttributes["printer-uri"] = c.getPrinterUri(printer) req.OperationAttributes[OperationAttributePrinterURI] = c.getPrinterUri(printer)
_, err := c.SendRequest(c.getHttpUri("admin", ""), req) _, err := c.SendRequest(c.getHttpUri("admin", ""), req)
return err return err
@ -281,12 +293,12 @@ func (c *IPPClient) PausePrinter(printer string) error {
func (c *IPPClient) GetJobAttributes(jobID int, attributes []string) (Attributes, error) { func (c *IPPClient) GetJobAttributes(jobID int, attributes []string) (Attributes, error) {
req := NewRequest(OperationGetJobAttributes, 1) req := NewRequest(OperationGetJobAttributes, 1)
req.OperationAttributes["job-uri"] = c.getJobUri(jobID) req.OperationAttributes[OperationAttributeJobURI] = c.getJobUri(jobID)
if attributes == nil { if attributes == nil {
req.OperationAttributes["requested-attributes"] = DefaultJobAttributes req.OperationAttributes[OperationAttributeRequestedAttributes] = DefaultJobAttributes
} else { } else {
req.OperationAttributes["requested-attributes"] = attributes req.OperationAttributes[OperationAttributeRequestedAttributes] = attributes
} }
resp, err := c.SendRequest(c.getHttpUri("jobs", jobID), req) resp, err := c.SendRequest(c.getHttpUri("jobs", jobID), req)
@ -303,14 +315,14 @@ func (c *IPPClient) GetJobAttributes(jobID int, attributes []string) (Attributes
func (c *IPPClient) GetJobs(printer string, whichJobs JobStateFilter, myJobs bool, attributes []string) (map[int]Attributes, error) { func (c *IPPClient) GetJobs(printer string, whichJobs JobStateFilter, myJobs bool, attributes []string) (map[int]Attributes, error) {
req := NewRequest(OperationGetJobs, 1) req := NewRequest(OperationGetJobs, 1)
req.OperationAttributes["printer-uri"] = c.getPrinterUri(printer) req.OperationAttributes[OperationAttributePrinterURI] = c.getPrinterUri(printer)
req.OperationAttributes["which-jobs"] = string(whichJobs) req.OperationAttributes[OperationAttributeWhichJobs] = string(whichJobs)
req.OperationAttributes["my-jobs"] = myJobs req.OperationAttributes[OperationAttributeMyJobs] = myJobs
if attributes == nil { if attributes == nil {
req.OperationAttributes["requested-attributes"] = DefaultJobAttributes req.OperationAttributes[OperationAttributeRequestedAttributes] = DefaultJobAttributes
} else { } else {
req.OperationAttributes["requested-attributes"] = append(attributes, "job-id") req.OperationAttributes[OperationAttributeRequestedAttributes] = append(attributes, OperationAttributeJobID)
} }
resp, err := c.SendRequest(c.getHttpUri("", nil), req) resp, err := c.SendRequest(c.getHttpUri("", nil), req)
@ -321,7 +333,7 @@ func (c *IPPClient) GetJobs(printer string, whichJobs JobStateFilter, myJobs boo
jobIDMap := make(map[int]Attributes) jobIDMap := make(map[int]Attributes)
for _, jobAttributes := range resp.Jobs { for _, jobAttributes := range resp.Jobs {
jobIDMap[jobAttributes["job-id"][0].Value.(int)] = jobAttributes jobIDMap[jobAttributes[OperationAttributeJobID][0].Value.(int)] = jobAttributes
} }
return jobIDMap, nil return jobIDMap, nil
@ -329,8 +341,8 @@ func (c *IPPClient) GetJobs(printer string, whichJobs JobStateFilter, myJobs boo
func (c *IPPClient) CancelJob(jobID int, purge bool) error { func (c *IPPClient) CancelJob(jobID int, purge bool) error {
req := NewRequest(OperationCancelJob, 1) req := NewRequest(OperationCancelJob, 1)
req.OperationAttributes["job-uri"] = c.getJobUri(jobID) req.OperationAttributes[OperationAttributeJobURI] = c.getJobUri(jobID)
req.OperationAttributes["purge-jobs"] = purge req.OperationAttributes[OperationAttributePurgeJobs] = purge
_, err := c.SendRequest(c.getHttpUri("jobs", ""), req) _, err := c.SendRequest(c.getHttpUri("jobs", ""), req)
return err return err
@ -338,8 +350,8 @@ func (c *IPPClient) CancelJob(jobID int, purge bool) error {
func (c *IPPClient) CancelAllJob(printer string, purge bool) error { func (c *IPPClient) CancelAllJob(printer string, purge bool) error {
req := NewRequest(OperationCancelJobs, 1) req := NewRequest(OperationCancelJobs, 1)
req.OperationAttributes["printer-uri"] = c.getPrinterUri(printer) req.OperationAttributes[OperationAttributePrinterURI] = c.getPrinterUri(printer)
req.OperationAttributes["purge-jobs"] = purge req.OperationAttributes[OperationAttributePurgeJobs] = purge
_, err := c.SendRequest(c.getHttpUri("admin", ""), req) _, err := c.SendRequest(c.getHttpUri("admin", ""), req)
return err return err
@ -347,7 +359,7 @@ func (c *IPPClient) CancelAllJob(printer string, purge bool) error {
func (c *IPPClient) RestartJob(jobID int) error { func (c *IPPClient) RestartJob(jobID int) error {
req := NewRequest(OperationRestartJob, 1) req := NewRequest(OperationRestartJob, 1)
req.OperationAttributes["job-uri"] = c.getJobUri(jobID) req.OperationAttributes[OperationAttributeJobURI] = c.getJobUri(jobID)
_, err := c.SendRequest(c.getHttpUri("jobs", ""), req) _, err := c.SendRequest(c.getHttpUri("jobs", ""), req)
return err return err
@ -355,8 +367,8 @@ func (c *IPPClient) RestartJob(jobID int) error {
func (c *IPPClient) HoldJobUntil(jobID int, holdUntil string) error { func (c *IPPClient) HoldJobUntil(jobID int, holdUntil string) error {
req := NewRequest(OperationRestartJob, 1) req := NewRequest(OperationRestartJob, 1)
req.OperationAttributes["job-uri"] = c.getJobUri(jobID) req.OperationAttributes[OperationAttributeJobURI] = c.getJobUri(jobID)
req.JobAttributes["job-hold-until"] = holdUntil req.JobAttributes[PrinterAttributeHoldJobUntil] = holdUntil
_, err := c.SendRequest(c.getHttpUri("jobs", ""), req) _, err := c.SendRequest(c.getHttpUri("jobs", ""), req)
return err return err
@ -370,14 +382,16 @@ func (c *IPPClient) PrintTestPage(printer string) (int, error) {
testPage.WriteString("printer-driver-version paper-size imageable-area job-id options time-at-creation") testPage.WriteString("printer-driver-version paper-size imageable-area job-id options time-at-creation")
testPage.WriteString("time-at-processing\n\n") testPage.WriteString("time-at-processing\n\n")
return c.Print([]Document{ return c.PrintDocuments([]Document{
{ {
Document: testPage, Document: testPage,
Name: "Test Page", Name: "Test Page",
Size: testPage.Len(), Size: testPage.Len(),
MimeType: MimeTypePostscript, MimeType: MimeTypePostscript,
}, },
}, printer, "Test Page", 1, DefaultJobPriority) }, printer, map[string]interface{}{
OperationAttributeJobName: "Test Page",
})
} }
func (c *IPPClient) TestConnection() error { func (c *IPPClient) TestConnection() error {