Skip to content

Commit

Permalink
standardize language property on Click model
Browse files Browse the repository at this point in the history
  • Loading branch information
EricFrancis12 committed Sep 30, 2024
1 parent 463f433 commit 49f1d49
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 21 deletions.
25 changes: 25 additions & 0 deletions pkg/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"os"
"regexp"
"strings"

"github.com/mileusna/useragent"
)

var CustomClient = NewCustomHTTPClient()
Expand Down Expand Up @@ -97,6 +99,29 @@ func createJSONResp(v any) *http.Response {
}
}

func GetDeviceType(ua useragent.UserAgent) DeviceType {
if ua.Desktop {
return DeviceTypeDesktop
} else if ua.Tablet {
return DeviceTypeTablet
} else if ua.Mobile {
return DeviceTypeMobile
}
return DeviceTypeUnknown
}

func GetLanguage(r http.Request) string {
langStr := r.Header.Get("Accept-Language")
if len(langStr) < 2 {
return "unknown"
}
return strings.ToLower(langStr[:2])
}

func GetScreenRes(r http.Request) string {
return r.Header.Get("Viewport-Width")
}

func IPInfoEndpoint(ipAddr string, ipInfoToken string) string {
return "https://ipinfo.io/" + ipAddr + "?token=" + ipInfoToken
}
29 changes: 29 additions & 0 deletions pkg/http_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pkg

import (
"net/http"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -21,3 +22,31 @@ func TestFetchIpInfo(t *testing.T) {
assert.Equal(t, ipInfo3.Country, "")
assert.NotNil(t, err)
}

func TestGetLanguage(t *testing.T) {
var langStrs = []string{
"en",
"En",
"EN",
"en-us",
"En-us",
"EN-us",
"en-US,en;q=0.5",
"en-US,en;q=0.9",
"en,es;q=0.9",
"en-US,en;q=0.9,es;q=0.8",
"en-US,en",
"en-US,en;q=0.9,pt;q=0.8",
"en-US,en;q=0.9,sw;q=0.8",
"en-US,en;q=0.9,es-US;q=0.8,es;q=0.7",
"en-NI,es;q=0.9,en-US;q=0.8,en;q=0.7,es-419;q=0.6",
}

for _, langStr := range langStrs {
assert.Equal(t, "en", GetLanguage(http.Request{
Header: http.Header{
"Accept-Language": []string{langStr},
},
}))
}
}
21 changes: 0 additions & 21 deletions pkg/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package pkg
import (
"net/http"
"os"

"github.com/mileusna/useragent"
)

const defaultCatchAllRedirectUrl = "https://bing.com"
Expand All @@ -31,22 +29,3 @@ func catchAllDest() Destination {
URL: CatchAllUrl(),
}
}

func GetDeviceType(ua useragent.UserAgent) DeviceType {
if ua.Desktop {
return DeviceTypeDesktop
} else if ua.Tablet {
return DeviceTypeTablet
} else if ua.Mobile {
return DeviceTypeMobile
}
return DeviceTypeUnknown
}

func GetLanguage(r http.Request) string {
return r.Header.Get("Accept-Language")
}

func GetScreenRes(r http.Request) string {
return r.Header.Get("Viewport-Width")
}

0 comments on commit 49f1d49

Please sign in to comment.