Skip to content

Commit

Permalink
add user-agent header. closes #242
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlos Puchol committed Jul 2, 2020
1 parent 1c71a76 commit 5de9127
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions AmahiAnywhere/AmahiAnywhere/Utils/Network.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,33 @@ public class Network {

static let shared = Network()

//eg. iOS/10_1
func deviceVersion() -> String {
let currentDevice = UIDevice.current
return "\(currentDevice.systemName)/\(currentDevice.systemVersion)"
}
//eg. iPhone5,2
func deviceName() -> String {
var sysinfo = utsname()
uname(&sysinfo)
return String(bytes: Data(bytes: &sysinfo.machine, count: Int(_SYS_NAMELEN)), encoding: .ascii)!.trimmingCharacters(in: .controlCharacters)
}
//eg. MyApp/1
func appNameAndVersion() -> String {
let dictionary = Bundle.main.infoDictionary!
let version = dictionary["CFBundleShortVersionString"] as! String
let name = dictionary["CFBundleName"] as! String
return "\(name)/\(version)"
}

private func getDefaultHeaders() -> HTTPHeaders {
return [
"Accept": "application/json"
// TODO: Add User-Agent header
"Accept": "application/json",
"User-Agent": "\(appNameAndVersion()) \(deviceName()) \(deviceVersion())"
]
}


private func getFinalHeaders(_ headers: HTTPHeaders) -> HTTPHeaders {
var finalHeaders = getDefaultHeaders()
for (key, value) in headers {
Expand Down

0 comments on commit 5de9127

Please sign in to comment.