Go to file
pSox 825e135ec1 Update gitignore 2022-11-17 11:27:25 +00:00
.gitignore Update gitignore 2022-11-17 11:27:25 +00:00
CREDITS.MD
LICENSE
README.md
adapter-http.go Added Apache 2.0 copyright notice 2022-11-17 11:02:26 +00:00
adapter-socket.go Added Apache 2.0 copyright notice 2022-11-17 11:02:26 +00:00
adapter.go Added Apache 2.0 copyright notice 2022-11-17 11:02:26 +00:00
attribute.go Added Apache 2.0 copyright notice 2022-11-17 11:02:26 +00:00
attribute_test.go Added Apache 2.0 copyright notice 2022-11-17 11:02:26 +00:00
constants.go Added Apache 2.0 copyright notice 2022-11-17 11:02:26 +00:00
cups-client.go Added Apache 2.0 copyright notice 2022-11-17 11:02:26 +00:00
error.go Added Apache 2.0 copyright notice 2022-11-17 11:02:26 +00:00
go.mod Update git version and module path 2022-11-17 11:04:12 +00:00
ipp-client.go Added Apache 2.0 copyright notice 2022-11-17 11:02:26 +00:00
request.go Added Apache 2.0 copyright notice 2022-11-17 11:02:26 +00:00
response.go Added Apache 2.0 copyright notice 2022-11-17 11:02:26 +00:00
utils.go Added Apache 2.0 copyright notice 2022-11-17 11:02:26 +00:00

README.md

go-ipp

Version Documentation Go Report Card Licence

Go Get

To get the package, execute:

go get -u github.com/phin1x/go-ipp

Features

  • basic ipp 2.0 compatible Client
  • extended client for cups server
  • create custom ipp requests
  • parse ipp responses and ipp control files

Example

Print a file with the ipp client

package main

import "github.com/phin1x/go-ipp"

func main() {
    // create a new ipp client
    client := ipp.NewIPPClient("printserver", 631, "user", "password", true)
    // print file
    client.PrintFile("/path/to/file", "my-printer", map[string]interface{}{})
}

Craft and send a custom request


package main

import "github.com/phin1x/go-ipp"

func main() {             
    // define a ipp request
    req := ipp.NewRequest(OperationGetJobs, 1)
    req.OperationAttributes[ipp.AttributeWhichJobs] = "completed"
    req.OperationAttributes[ipp.AttributeMyJobs] = myJobs
    req.OperationAttributes[ipp.AttributeFirstJobID] = 42
    req.OperationAttributes[ipp.AttributeRequestingUserName] = "fabian"
    
    // encode request to bytes
    payload, err := req.Encode()
    if err != nil {
        panic(err)
    }
    
    // send ipp request to remote server via http
    httpReq, err := http.NewRequest("POST", "http://my-print-server:631/printers/my-printer", bytes.NewBuffer(payload))
    if err != nil {
        panic(err)
    }
    
    // set ipp headers
    httpReq.Header.Set("Content-Length", len(payload))
    httpReq.Header.Set("Content-Type", ipp.ContentTypeIPP)
    
    httpClient := &http.Client()
    httpResp, err := httpClient.Do(httpReq)
    if err != nil {
        panic(err)
    }
    defer httpResp.Body.Close()
    
    // response must be 200 for a successful operation
    // other possible http codes are: 
    // - 500 -> server error
    // - 426 -> sever requests a encrypted connection
    // - 401 -> forbidden -> need authorization header or user is not permitted
    if httpResp.StatusCode != 200 {
        panic("non 200 response from server")
    }
    
    // decode ipp response
    resp, err := ipp.NewResponseDecoder(httpResp.Body).Decode(nil)
    if err != nil {
        panic(err)
    }
    
    // check if the response status is "ok"
    if resp.StatusCode == ipp.StatusOk {
        panic(resp.StatusCode)
    }
    
    // do something with the returned data
    for _, job := resp.JobAttributes {
        // ...
    }
}

Licence

Apache Licence Version 2.0