Compare commits
5 Commits
47a595c757
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ca66bafe72 | ||
|
|
6fa9ba0057 | ||
|
|
d296e21220 | ||
|
|
87cfe02231 | ||
|
|
bb7d81952c |
2
.env.example
Normal file
2
.env.example
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
DHL_API_KEY = "...."
|
||||||
|
DHL_API_SECRET = "...."
|
||||||
1
go.mod
1
go.mod
@@ -7,6 +7,7 @@ require modernc.org/sqlite v1.46.1
|
|||||||
require (
|
require (
|
||||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||||
github.com/google/uuid v1.6.0 // indirect
|
github.com/google/uuid v1.6.0 // indirect
|
||||||
|
github.com/joho/godotenv v1.5.1 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
github.com/ncruces/go-strftime v1.0.0 // indirect
|
github.com/ncruces/go-strftime v1.0.0 // indirect
|
||||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||||
|
|||||||
2
go.sum
2
go.sum
@@ -6,6 +6,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
|||||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
|
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
|
||||||
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
|
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
|
||||||
|
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||||
|
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||||
github.com/ncruces/go-strftime v1.0.0 h1:HMFp8mLCTPp341M/ZnA4qaf7ZlsbTc+miZjCLOFAw7w=
|
github.com/ncruces/go-strftime v1.0.0 h1:HMFp8mLCTPp341M/ZnA4qaf7ZlsbTc+miZjCLOFAw7w=
|
||||||
|
|||||||
155
main.go
155
main.go
@@ -11,100 +11,107 @@ In this project i play around with go. This is my first time writing go, and im
|
|||||||
// https://stackoverflow.com/questions/16466320/is-there-a-way-to-do-repetitive-tasks-at-intervals, How timer intervals work in GoLang
|
// 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://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://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 (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"log"
|
"log"
|
||||||
|
"errors"
|
||||||
|
"net/http"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/joho/godotenv"
|
||||||
_ "modernc.org/sqlite"
|
_ "modernc.org/sqlite"
|
||||||
)
|
)
|
||||||
|
|
||||||
func executeQuery(db *sql.DB, query string) {
|
const API_URL string = "http://www.google.com/robots.txt"
|
||||||
_, err := db.Exec(query)
|
|
||||||
|
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 {
|
if err != nil {
|
||||||
log.Printf("%q: %s\n", err, query)
|
log.Fatal(err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
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 createTables(db *sql.DB) {
|
|
||||||
executeQuery(db, `
|
|
||||||
CREATE TABLE shipments (
|
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
||||||
tracking_number TEXT NOT NULL UNIQUE,
|
|
||||||
service TEXT NOT NULL,
|
|
||||||
sender_name TEXT,
|
|
||||||
receiver_name TEXT,
|
|
||||||
origin_country TEXT,
|
|
||||||
origin_city TEXT,
|
|
||||||
origin_postal TEXT,
|
|
||||||
origin_address TEXT,
|
|
||||||
destination_country TEXT,
|
|
||||||
destination_city TEXT,
|
|
||||||
destination_postal TEXT,
|
|
||||||
destination_address TEXT,
|
|
||||||
pickup_date TEXT,
|
|
||||||
estimated_delivery_from TEXT,
|
|
||||||
estimated_delivery_to TEXT,
|
|
||||||
current_status TEXT,
|
|
||||||
last_update TEXT DEFAULT (datetime('now')),
|
|
||||||
created_at TEXT DEFAULT (datetime('now'))
|
|
||||||
);
|
|
||||||
|
|
||||||
-- Status Events: history of status updates
|
|
||||||
CREATE TABLE shipment_events (
|
|
||||||
id INTEGER PRIMARY KEY,
|
|
||||||
shipment_id INTEGER NOT NULL,
|
|
||||||
event_time TEXT NOT NULL,
|
|
||||||
status_code TEXT NOT NULL,
|
|
||||||
status_desc TEXT,
|
|
||||||
description TEXT,
|
|
||||||
remark TEXT,
|
|
||||||
next_steps TEXT,
|
|
||||||
location_country TEXT,
|
|
||||||
location_city TEXT,
|
|
||||||
location_postal TEXT,
|
|
||||||
FOREIGN KEY (shipment_id) REFERENCES shipments(id)
|
|
||||||
);
|
|
||||||
CREATE INDEX idx_events_shipment_time ON shipment_events(shipment_id, event_time);
|
|
||||||
|
|
||||||
CREATE TABLE shipment_updates (
|
|
||||||
id INTEGER PRIMARY KEY,
|
|
||||||
shipment_id INTEGER NOT NULL,
|
|
||||||
update_time TEXT DEFAULT (datetime('now')),
|
|
||||||
service TEXT,
|
|
||||||
location_country TEXT,
|
|
||||||
location_city TEXT,
|
|
||||||
status_code TEXT,
|
|
||||||
status_desc TEXT,
|
|
||||||
estimated_from TEXT,
|
|
||||||
estimated_to TEXT,
|
|
||||||
FOREIGN KEY (shipment_id) REFERENCES shipments(id)
|
|
||||||
);
|
|
||||||
CREATE INDEX idx_updates_shipment_time ON shipment_updates(shipment_id, update_time);
|
|
||||||
`)
|
|
||||||
|
|
||||||
log.Printf("[Database] tables created")
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func dropTables(db *sql.DB) {
|
|
||||||
executeQuery(db, `
|
|
||||||
DROP TABLE IF EXISTS shipments;
|
|
||||||
DROP TABLE IF EXISTS shipment_events;
|
|
||||||
DROP TABLE IF EXISTS shipment_updates;
|
|
||||||
`)
|
|
||||||
|
|
||||||
log.Printf("[Database] tables dropped")
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
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")
|
db, err := sql.Open("sqlite", "./dhl.db")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
dropTables(db) // temporarely for debugging
|
code := "blablbla"
|
||||||
createTables(db)
|
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)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user