/orca-validation-go

This is a fully working example of how to validate barcode scans using Orca Scan in golang

Primary LanguageGo

orca-validation-go

Example of how to validate barcode scans in real-time in using Go.

Install

First ensure you have Go installed. If not, follow this guide.

# should return 1.13 or higher
go version

Then execute the following:

# download this example code
git clone https://github.com/orca-scan/orca-validation-go.git

# go into the new directory
cd orca-validation-go

# install dependencies
go get -d ./...

If you get an error from this command like "go.mod file not found in current directory or any parent directory.", enter the following command, then try again:

go env -w GO111MODULE=auto

Run

# start the project
go run server.go

Your server will now be running on port 3000.

You can emulate an Orca Scan Validation input using cURL by running the following:

curl --location --request POST 'http://localhost:3000/' \
--header 'Content-Type: application/json' \
--data-raw '{
    "___orca_sheet_name": "Vehicle Checks",
    "___orca_user_email": "hidden@requires.https",
    "Barcode": "orca-scan-test",
    "Date": "2022-04-19T16:45:02.851Z",
    "Name": "Orca Scan Validation"
}'

Important things to note

  1. Only Orca Scan system fields start with ___
  2. Properties in the JSON payload are an exact match to the field names in your sheet (case and space)

How this example works

This example work as follows:

func validationHandler(w http.ResponseWriter, r *http.Request) {
	// Read body
	body, err := ioutil.ReadAll(r.Body)
	defer r.Body.Close()
	if err != nil {
		fmt.Println(err)
		http.Error(w, err.Error(), 500)
		return
	}

	// Parse JSON data
	var barcode OrcaBarcode
	jsonErr := json.Unmarshal([]byte(body), &barcode)
	if jsonErr != nil {
		fmt.Println(jsonErr)
		http.Error(w, jsonErr.Error(), 500)
		return
	}

    // debug purpose: show in console raw data received
	fmt.Println(barcode)

	// NOTE:
	// orca system fields start with ___
	// you can access the value of each field using the field name (data.Name, data.Barcode, data.Location)
	name := barcode.Name

	// validation example
	if(len(name) > 20){
		// return error message with json format
		w.Write([]byte(`{
			"title": "Invalid Name",
			"message": "Name must be less than 20 characters"}
			`))
		return
	}

	// return HTTP Status 200 with no body
	w.Write([]byte(""))
}

Test server locally on Orca Cloud

To expose the server securely from localhost and test it easily on the real Orca Cloud environment you can use Secure Tunnels. Take a look at Ngrok or Cloudflare.

ngrok http 3000

Troubleshooting

If you run into any issues not listed here, please open a ticket.

Examples in other langauges

History

For change-log, check releases.

License

© Orca Scan, the Barcode Scanner app for iOS and Android.