Files
DHL-Tracker-GO/main.go
2026-03-10 08:49:28 +01:00

118 lines
2.6 KiB
Go

package main
/*
Author: Valentijn van der Jagt
In this project i play around with go. This is my first time writing go, and im using a lot of help from sources. all useful sources are listed in the comments below.
*/
// https://stackoverflow.com/questions/61080317/go-lang-very-simple-http-post-requests-and-response-endpoint, HTTP Server example
// https://stackoverflow.com/questions/16466320/is-there-a-way-to-do-repetitive-tasks-at-intervals, How timer intervals work in GoLang
// https://pkg.go.dev/modernc.org/sqlite#section-documentation, how to interface with specifically sqlite in GoLang
// https://github.com/mattn/go-sqlite3/blob/v1.14.34/_example/simple/simple.go, How to inferface with a database in GoLang.
// https://go.dev/tour/moretypes/6, arrays....
// https://go.dev/doc/tutorial/handle-errors, deal with returning errors
import (
"database/sql"
"log"
"errors"
"net/http"
"io"
"os"
"github.com/joho/godotenv"
_ "modernc.org/sqlite"
)
const API_URL string = "http://www.google.com/robots.txt"
var dhl_api_key string
func findParcelProvider(code string, postal_code string)(string, error){
providers := [4]string{"express", "parcel-nl", "ecommerce", "ecommerce-europe"}
for _, v := range providers {
// do http api reqiest
success := true
if (success){
return v, nil
}
}
return "", errors.New("Parcel not found in the common providers!")
}
func markPackageSubscription(code string, postal_code string, service string){
/*
{
"type": "Shipment",
"format": "Default",
"service": "parcel-nl",
"hook": {
"uri": "https://your-server.example.com/dhl-webhook"
},
"events": [
"pre-transit",
"transit",
"delivered",
"failure",
"unknown"
],
"shipmentIDsWithChallengeDetails": [
{
"shipmentID": "3SABCDEF123456",
"postalCode": "1012AB"
}
]
}
*/
res, err := http.Get(API_URL)
if err != nil {
log.Fatal(err)
}
body, err := io.ReadAll(res.Body)
res.Body.Close()
if res.StatusCode > 299 {
log.Fatalf("Response failed with status code: %d and\nbody: %s\n", res.StatusCode, body)
}
if err != nil {
log.Fatal(err)
}
log.Printf("%s, %s\n", body, dhl_api_key)
}
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
dhl_api_key = os.Getenv("DHL_API_KEY")
db, err := sql.Open("sqlite", "./dhl.db")
if err != nil {
log.Fatal(err)
}
defer db.Close()
code := "blablbla"
postal := "1234ab"
provider, err := findParcelProvider(code, postal)
if (err != nil){
log.Fatal(err)
}
// if theres a result, print it.
log.Printf("Code: %s, Postal Code: %s, Tracking Service: %s", code, postal, provider)
markPackageSubscription(code, postal, provider)
}