/
printjobs.go
179 lines (158 loc) · 5.42 KB
/
printjobs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package torchprint
import (
"bytes"
"encoding/json"
"io"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"strconv"
"time"
"github.com/libertylocked/torchprint/errors"
)
// FinishingOptions finishing options of a printjob
type FinishingOptions struct {
Mono bool `json:"Mono"`
Duplex bool `json:"Duplex"`
PagesPerSide int `json:"PagesPerSide"`
Copies int `json:"Copies"`
DefaultPageSize string `json:"DefaultPageSize"` // Letter or something
PageRange string `json:"PageRange"`
}
// PrintJobItem is an item in printjobs
type PrintJobItem struct {
ProtectedBy string `json:"ProtectedBy"`
Owner string `json:"Owner"`
Name string `json:"Name"`
ApplicationName string `json:"ApplicationName"`
JobFormat string `json:"JobFormat"`
SubmissionTimeUtc time.Time `json:"SubmissionTimeUtc"`
Stats struct {
SizeInKb int `json:"SizeInKb"`
TotalPages int `json:"TotalPages"`
TotalBWPages int `json:"TotalBWPages"`
TotalColorPages int `json:"TotalColorPages"`
TotalSheets int `json:"TotalSheets"`
} `json:"Stats"`
FinishingOptions FinishingOptions `json:"FinishingOptions"`
// SupportedFinishingOptions FinishingOptions `json:"SupportedFinishingOptions"` // its a bool map, dont need for now
DeviceGroup string `json:"DeviceGroup"`
PrinterName string `json:"PrinterName"`
DocumentType string `json:"DocumentType"`
AllowableActions string `json:"AllowableActions"`
PrintState string `json:"PrintState"` // this is pretty important
LastModified time.Time `json:"LastModified"`
Location string `json:"Location"`
RawPageCounterResult string `json:"RawPageCounterResult"`
SubmissionTimeDelta float64 `json:"SubmissionTimeDelta"`
Cost string `json:"Cost"`
}
// PrintJobsResponseData is the response of view printjob request
type PrintJobsResponseData struct {
PreviousPageLink string `json:"PreviousPageLink"`
NextPageLink string `json:"NextPageLink"`
Count int `json:"Count"`
Items []PrintJobItem `json:"Items"`
}
// PrintJobDeleteLocation is an entry in delete request array
type PrintJobDeleteLocation struct {
Location string `json:"Location"`
}
// PrintJobDeleteRequestData is the request data to delete printjobs
type PrintJobDeleteRequestData struct {
PrintJobs []PrintJobDeleteLocation `json:"PrintJobs"`
}
// PrintJobDeleteStatus the status response of a delete request
type PrintJobDeleteStatus struct {
Location string `json:"Location"`
Status int `json:"Status"`
}
type addPrintJobRequestData struct {
FinishingOptions struct {
Mono bool `json:"Mono"`
Duplex bool `json:"Duplex"`
PagesPerSide string `json:"PagesPerSide"`
Copies string `json:"Copies"`
DefaultPageSize string `json:"DefaultPageSize"`
PageRange string `json:"PageRange"`
} `json:"FinishingOptions"`
}
// GetPrintJobs sends a request to view print jobs
func (api *API) GetPrintJobs() (*PrintJobsResponseData, error) {
var respData PrintJobsResponseData
_, err := api.GetJSON("users/"+api.UserID+"/printjobs", nil, &respData)
if err != nil {
return nil, err
}
return &respData, err
}
// AddPrintJob adds a printjob
func (api *API) AddPrintJob(filename string, options FinishingOptions) (*PrintJobItem, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
bodyform := &bytes.Buffer{}
writer := multipart.NewWriter(bodyform)
metapart, err := writer.CreateFormField("MetaData")
if err != nil {
return nil, err
}
// XXX: pretty sure it's a bug in pharos web API, that PagesPerSide and Copies
// is string in the request (server will 500 if they are int)
// sorry about this nasty hack
reqMeta := addPrintJobRequestData{}
reqMeta.FinishingOptions.Copies = strconv.Itoa(options.Copies)
reqMeta.FinishingOptions.DefaultPageSize = options.DefaultPageSize
reqMeta.FinishingOptions.Duplex = options.Duplex
reqMeta.FinishingOptions.Mono = options.Mono
reqMeta.FinishingOptions.PageRange = options.PageRange
reqMeta.FinishingOptions.PagesPerSide = strconv.Itoa(options.PagesPerSide)
err = json.NewEncoder(metapart).Encode(reqMeta)
if err != nil {
return nil, err
}
filepart, err := writer.CreateFormFile("content", filepath.Base(filename))
if err != nil {
return nil, err
}
_, err = io.Copy(filepart, file)
if err != nil {
return nil, err
}
// finally send the POST
writer.Close()
var respData PrintJobItem
resp, err := api.client().Body(bodyform).
Set("Content-Type", writer.FormDataContentType()).
Post("users/" + api.UserID + "/printjobs").
ReceiveSuccess(&respData)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusCreated {
return &respData, errors.NewHTTPError(resp)
}
return &respData, nil
}
// DeletePrintJobs removes printjobs from job queue
func (api *API) DeletePrintJobs(locations []PrintJobDeleteLocation) ([]*PrintJobDeleteStatus, error) {
var respData []*PrintJobDeleteStatus
// turn array of locations into request
reqData := PrintJobDeleteRequestData{
PrintJobs: locations,
}
resp, err := api.client().
Delete("printjobs").
BodyJSON(reqData).
ReceiveSuccess(&respData)
if err != nil {
return nil, err
}
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return nil, errors.NewHTTPError(resp)
}
return respData, err
}