Spaces:
Paused
Paused
ML Intern deploy
Deploy httpSMS gateway to Hugging Face Spaces (Docker SDK): nginx :7860 -> Go API :8000, Nuxt SPA, Android APK, bootstrap-mode entrypoint
83a59ca Download api/pkg/requests/request.go from red-kit/httpsms-gateway: direct link, hf CLI and curl.
- Browser
- Download file 3.65 kB
-
https://huggingface.co/spaces/red-kit/httpsms-gateway/resolve/83a59cae175e3c9a43b6f5d3f8749ca61b6b5072/api/pkg/requests/request.go
- Command line
-
hf download hf://spaces/red-kit/httpsms-gateway@83a59cae175e3c9a43b6f5d3f8749ca61b6b5072/api/pkg/requests/request.go
-
curl -L -o request.go https://huggingface.co/spaces/red-kit/httpsms-gateway/resolve/83a59cae175e3c9a43b6f5d3f8749ca61b6b5072/api/pkg/requests/request.go
3.65 kB
| package requests | |
| import ( | |
| "net/url" | |
| "strconv" | |
| "strings" | |
| "unicode" | |
| "github.com/NdoleStudio/httpsms/pkg/entities" | |
| "github.com/nyaruka/phonenumbers" | |
| ) | |
| type request struct{} | |
| func (input *request) sanitizeAddresses(value []string) []string { | |
| var result []string | |
| for _, address := range value { | |
| result = append(result, input.sanitizeAddress(address)) | |
| } | |
| return result | |
| } | |
| func (input *request) sanitizeAddress(value string) string { | |
| value = strings.TrimSpace(value) | |
| if !strings.HasPrefix(value, "+") && input.isDigits(value) && len(value) > 9 { | |
| value = "+" + value | |
| } | |
| if number, err := phonenumbers.Parse(value, phonenumbers.UNKNOWN_REGION); err == nil { | |
| value = phonenumbers.Format(number, phonenumbers.E164) | |
| } | |
| return value | |
| } | |
| func (input *request) sanitizeContact(owner string, contact string) string { | |
| contact = strings.TrimSpace(contact) | |
| if len(contact) < 8 || !input.isDigits(contact) { | |
| return contact | |
| } | |
| regionPhoneNumber, err := phonenumbers.Parse(owner, phonenumbers.UNKNOWN_REGION) | |
| if err != nil { | |
| return contact | |
| } | |
| if number, err := phonenumbers.Parse(contact, phonenumbers.GetRegionCodeForNumber(regionPhoneNumber)); err == nil { | |
| contact = phonenumbers.Format(number, phonenumbers.E164) | |
| } | |
| return contact | |
| } | |
| // sanitizeBool sanitizes a boolean string | |
| func (input *request) sanitizeBool(value string) string { | |
| value = strings.TrimSpace(value) | |
| if value == "1" || strings.ToLower(value) == "true" { | |
| value = "true" | |
| } | |
| if value == "0" || strings.ToLower(value) == "false" { | |
| value = "false" | |
| } | |
| return value | |
| } | |
| func (input *request) sanitizeSIM(value string) string { | |
| if value == entities.SIM1.String() || value == entities.SIM2.String() { | |
| return value | |
| } | |
| return entities.SIM1.String() | |
| } | |
| func (input *request) sanitizeURL(value string) string { | |
| value = strings.TrimSpace(value) | |
| website, err := url.Parse(value) | |
| if err != nil { | |
| return value | |
| } | |
| if website.Scheme == "" { | |
| return "https://" + value | |
| } | |
| return value | |
| } | |
| func (input *request) sanitizeStringPointer(value string) *string { | |
| value = strings.TrimSpace(value) | |
| if value == "" { | |
| return nil | |
| } | |
| return &value | |
| } | |
| func (input *request) removeStringDuplicates(values []string) []string { | |
| cache := map[string]struct{}{} | |
| for _, value := range values { | |
| cache[value] = struct{}{} | |
| } | |
| var result []string | |
| for key := range cache { | |
| result = append(result, key) | |
| } | |
| return result | |
| } | |
| func (input *request) removeEmptyStrings(values []string) []string { | |
| var result []string | |
| for _, value := range values { | |
| value = strings.TrimSpace(value) | |
| if value != "" { | |
| result = append(result, value) | |
| } | |
| } | |
| return result | |
| } | |
| func sanitizeUniqueStrings(values []string, normalize func(string) string) []string { | |
| seen := map[string]struct{}{} | |
| result := make([]string, 0, len(values)) | |
| for _, value := range values { | |
| value = normalize(value) | |
| if value == "" { | |
| continue | |
| } | |
| if _, ok := seen[value]; ok { | |
| continue | |
| } | |
| seen[value] = struct{}{} | |
| result = append(result, value) | |
| } | |
| return result | |
| } | |
| func (input *request) sanitizeMessageID(value string) string { | |
| id := strings.Builder{} | |
| for _, char := range value { | |
| if char == '.' { | |
| return id.String() | |
| } | |
| id.WriteRune(char) | |
| } | |
| return id.String() | |
| } | |
| // getLimit gets the take as a string | |
| func (input *request) getBool(value string) bool { | |
| if value == "true" { | |
| return true | |
| } | |
| return false | |
| } | |
| // getLimit gets the take as a string | |
| func (input *request) getInt(value string) int { | |
| val, _ := strconv.Atoi(value) | |
| return val | |
| } | |
| func (input *request) isDigits(value string) bool { | |
| for _, c := range value { | |
| if !unicode.IsDigit(c) { | |
| return false | |
| } | |
| } | |
| return true | |
| } | |