Skip to content

Commit

Permalink
Merge pull request #3 from WURFL/feature/examples
Browse files Browse the repository at this point in the history
Feature/examples
  • Loading branch information
paulborile authored Jan 17, 2025
2 parents 708ef8c + 9840b03 commit 2fa5622
Show file tree
Hide file tree
Showing 5 changed files with 271 additions and 28 deletions.
5 changes: 4 additions & 1 deletion Changelog.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
1.30.2 -
1.30.3 - Jan 15, 2025
- examples, better wording for godoc

1.30.2 - Jan 14, 2025
- removing deprecated GetCapabilities() and GetVirtualCapabilities from tests
- more idiomatic test code (use asserts)
- removing unnecessary or old tests
Expand Down
90 changes: 90 additions & 0 deletions examples/lookup_headers/lookup_headers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package main

import (
"fmt"
"os"

wurfl "github.com/WURFL/golang-wurfl"
)

func main() {

// Replace this with your own WURFL Snapshot URL
wurflUpdaterURL := "https://data.scientiamobile.com/xxxxx/wurfl.zip"

fmt.Println("Downloading WURFL file ...")

if err := wurfl.Download(wurflUpdaterURL, "."); err != nil {
fmt.Printf("Error downloading WURFL file: %v\n", err)
os.Exit(1)
}

fmt.Println("WURFL file downloaded successfully")

wengine, err := wurfl.Create("./wurfl.zip", nil, nil, -1, wurfl.WurflCacheProviderLru, "100000")

if err != nil {
fmt.Printf("Error creating WURFL engine: %v", err)
os.Exit(1)
}
defer wengine.Destroy()

fmt.Println("Engine loaded, version : ", wengine.GetAPIVersion(), "wurfl info ", wengine.GetInfo())

// start the updater : will keep the wurfl.zip file updated to the last version
// if err := wengine.SetUpdaterDataURL(wurflUpdaterURL); err != nil {
// fmt.Printf("Error setting updater data URL: %v\n", err)
// }
// wengine.SetUpdaterDataFrequency(wurfl.WurflUpdaterFrequencyDaily)
// wengine.UpdaterStart()

c := wengine.GetAllCaps()
fmt.Println("Capabilities available = ", c)

ua := "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Mobile Safari/537.36"

ihmap := make(map[string]string)
ihmap["User-Agent"] = ua
ihmap["Accept-Encoding"] = "gzip, deflate, br, zstd"
ihmap["Sec-CH-UA-Platform"] = "Android"
ihmap["Sec-CH-UA-Platform-Version"] = `"13.0.0"`
ihmap["Sec-CH-UA"] = `"Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126"`
ihmap["Sec-CH-UA-Mobile"] = "?1"
ihmap["Sec-Ch-Ua-Model"] = `"SM-S135DL"`
ihmap["Sec-Ch-Ua-Full-Version"] = `"126.0.6478.71"`
ihmap["Sec-CH-UA-Full-Version-List"] = `"Not/A)Brand";v="8.0.0.0", "Chromium";v="126.0.6478.71", "Google Chrome";v="126.0.6478.71"`

device, err := wengine.LookupWithImportantHeaderMap(ihmap)
if err != nil {
fmt.Printf("Error in lookup: %v", err)
os.Exit(1)
}
defer device.Destroy()

// obtain the wurfl_id for this device, a unique device identifier
deviceid, err := device.GetDeviceID()
if err != nil {
fmt.Printf("Error in GetDeviceID: %v", err)
}
fmt.Println(deviceid)

// static capabilities are stored in the wurfl.zip
cap, _ := device.GetStaticCap("model_name")
fmt.Printf("model_name = %s\n", cap)

// virtual capabilities are computed on the fly
vcap, _ := device.GetVirtualCap("complete_device_name")
fmt.Printf("complete_device_name = %s\n", vcap)

caps := []string{
"marketing_name",
"brand_name",
"device_os",
}
// get a list of static caps, returns a map
caplist, err := device.GetStaticCaps(caps)
if err != nil {
fmt.Printf("Error in GetDeviceID: %v", err)
}
fmt.Printf("marketing_name = %s\n", caplist["marketing_name"])
}
79 changes: 79 additions & 0 deletions examples/lookup_request/lookup_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package main

import (
"fmt"
"log"
"net/http"
"os"

wurfl "github.com/WURFL/golang-wurfl"
)

func main() {

// Replace this with your own WURFL Snapshot URL
wurflUpdaterURL := "https://data.scientiamobile.com/xxxxx/wurfl.zip"

fmt.Println("Downloading WURFL file ...")

if err := wurfl.Download(wurflUpdaterURL, "."); err != nil {
fmt.Printf("Error downloading WURFL file: %v\n", err)
os.Exit(1)
}

fmt.Println("WURFL file downloaded successfully")

wengine, err := wurfl.Create("./wurfl.zip", nil, nil, -1, wurfl.WurflCacheProviderLru, "100000")

if err != nil {
fmt.Printf("Error creating WURFL engine: %v\n", err)
os.Exit(1)
}

fmt.Println("Engine loaded, version : ", wengine.GetAPIVersion(), "wurfl info ", wengine.GetInfo())

// start the updater : will keep the wurfl.zip file updated to the last version

if err := wengine.SetUpdaterDataURL(wurflUpdaterURL); err != nil {
fmt.Printf("Error setting updater data URL: %v\n", err)
}
wengine.SetUpdaterDataFrequency(wurfl.WurflUpdaterFrequencyDaily)
wengine.UpdaterStart()

// start server and process reqs

fmt.Printf("Starting server on port 8080\n")

// I need to inject the wurfl enginee into the handler
// so that it can be used to lookup the device
// for each request

http.HandleFunc("/detect", makeHandler(wengine))

log.Fatal(http.ListenAndServe(":8080", nil))

wengine.Destroy()
}

// makeHandler creates a http.HandlerFunc injecting the wurfl engine
func makeHandler(wengine *wurfl.Wurfl) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {

device, err := wengine.LookupRequest(r)
if err != nil {
fmt.Fprintf(w, "Error in lookup: %v", err)
}

ado, _ := device.GetVirtualCap("advertised_device_os")
isbot, _ := device.GetVirtualCap("is_robot")
ab, _ := device.GetVirtualCap("advertised_browser")
device.Destroy()

fmt.Fprintf(w, "is_robot = %s\n", isbot)
fmt.Fprintf(w, "advertised_device_os = %s\n", ado)
fmt.Fprintf(w, "advertised_browser = %s\n", ab)
// close response write and send out the response
w.WriteHeader(http.StatusOK)

}
}
72 changes: 72 additions & 0 deletions examples/lookup_ua/lookup_ua.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package main

import (
"fmt"
"os"

wurfl "github.com/WURFL/golang-wurfl"
)

func main() {

// Replace this with your own WURFL Snapshot URL
wurflUpdaterURL := "https://data.scientiamobile.com/xxxxx/wurfl.zip"

fmt.Println("Downloading WURFL file ...")

if err := wurfl.Download(wurflUpdaterURL, "."); err != nil {
fmt.Printf("Error downloading WURFL file: %v\n", err)
os.Exit(1)
}

fmt.Println("WURFL file downloaded successfully")

wengine, err := wurfl.Create("./wurfl.zip", nil, nil, -1, wurfl.WurflCacheProviderLru, "100000")
if err != nil {
fmt.Printf("Error creating WURFL engine: %v\n", err)
os.Exit(1)
}
defer wengine.Destroy()

fmt.Println("Engine loaded, version : ", wengine.GetAPIVersion(), "wurfl info ", wengine.GetInfo())

// start the updater : will keep the wurfl.zip file updated to the last version
// if err := wengine.SetUpdaterDataURL(wurflUpdaterURL); err != nil {
// fmt.Printf("Error setting updater data URL: %v\n", err)
// }
// wengine.SetUpdaterDataFrequency(wurfl.WurflUpdaterFrequencyDaily)
// wengine.UpdaterStart()

c := wengine.GetAllCaps()
fmt.Println("Capabilities available = ", c)

ua := "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Mobile Safari/537.36"

device, err := wengine.LookupUserAgent(ua)
if err != nil {
fmt.Printf("Error in lookup: %v", err)
os.Exit(1)
}

// obtain the wurfl_id for this device, a unique device identifier
deviceid, err := device.GetDeviceID()
if err != nil {
fmt.Printf("Error in GetDeviceID: %v", err)
os.Exit(1)
}
fmt.Println(deviceid)

// static capabilities are stored in the wurfl.zip
cap, _ := device.GetStaticCap("model_name")
fmt.Printf("model_name = %s\n", cap)

// virtual capabilities are computed on the fly
vcap, _ := device.GetVirtualCap("is_android")
fmt.Printf("is_android = %s\n", vcap)

if wengine.IsUserAgentFrozen(ua) {
fmt.Printf("UA %s is frozen. Sec-Ch-Ua headers are necessary for correct device identification.\n", ua)
}

device.Destroy()
}
Loading

0 comments on commit 2fa5622

Please sign in to comment.