AsleepConfig

Initialize Config

Asleep.initAsleepConfig()

let apiKey: String = "YOUR_API_KEY"
let userId: String?
let baseUrl: URL?
let callbackUrl: URL?
let service: String?
let delegate: AsleepConfigDelegate = self

Asleep.initAsleepConfig(apiKey: apiKey,
                        userId: userId,
                        baseUrl: baseUrl,
                        callbackUrl: callbackUrl,
                        service: service,
                        delegate: self)
Property NameTypeDescription
apiKeyStringEnter the value obtained from the Generate API key
userIdString?When there is no initial userId, enter nil, and later input the received userId.
baseUrlURL?Enter the proxy server address. If nil, default base url is used.
callbackUrlURL?Enter the URL of the server to receive sleep session analysis results.
serviceService?Enter the app name
delegateAsleepConfigDelegateDelegate to receive results and errors

Token authentication

If you called setup with app credentials, call initAsleepConfig with the same credentials.

Asleep.initAsleepConfig(appId: "YOUR_APP_ID",
                        appSecret: "YOUR_APP_SECRET",
                        userId: userId,
                        delegate: self)
Property NameTypeDescription
appIdString?App ID issued from the dashboard
appSecretString?App Secret issued from the dashboard
isTestEnvironmentBool?Set to true only when using the test environment

The remaining parameters (userId, baseUrl, callbackUrl, service, delegate) are the same as the API Key method.


AsleepConfigDelegate

protocol AsleepConfigDelegate {
    func userDidJoin(userId: String, config: Asleep.Config)
    func didFailUserJoin(error: Asleep.AsleepError)
    func userDidDelete(userId: String)
}
  1. If success, userDidJoin() is called.

    Property NameTypeDescription
    userIdStringgenerated userId
    configAsleep.Configgenearated config
  2. If failure, didFailUserJoin() is called.

    Property NameTypeDescription
    errorAsleep.AsleepErrorError Codes
  3. userDidDelete()
    If success, all user data including the userId will be deleted.

    Property NameTypeDescription
    userIdStringDeleted userId

Delete User

Asleep.deleteUser()

var config: Asleep.Config? 
if let config {
		Asleep.deleteUser(config: config)
}
Property NameTypeDescription
configAsleep.ConfigEnter the Asleep.Config instance


User Metadata


Save and retrieve the user's date of birth, gender, height, and weight.

Asleep.saveUserMetadata()

var config: Asleep.Config?
let metadata = Asleep.Model.UserMetadata(birthYear: 1990,
                                         birthMonth: 5,
                                         birthDay: 21,
                                         gender: .female,
                                         height: 165.5,
                                         weight: 55.2)

Task {
    do {
        if let config {
            try await Asleep.saveUserMetadata(metadata, config: config)
        }
    } catch {
        print(error)
    }
}
Property NameTypeDescription
metadataAsleep.Model.UserMetadataUser information to save
configAsleep.ConfigThe config received from userDidJoin

Asleep.fetchUserMetadata()

var config: Asleep.Config?

Task {
    do {
        if let config {
            let metadata = try await Asleep.fetchUserMetadata(config: config)
        }
    } catch {
        print(error)
    }
}
Property NameTypeDescription
configAsleep.ConfigThe config received from userDidJoin
(return value)Asleep.Model.UserMetadata?The saved information. nil if nothing has been saved yet

Asleep.Model.UserMetadata

struct UserMetadata {
    let birthYear: Int?
    let birthMonth: Int?
    let birthDay: Int?
    let gender: Gender?
    let height: Double?
    let weight: Double?
}

enum Gender: String {
    case male
    case female
    case nonBinary
    case other
    case preferNotToSay
}
Property NameTypeDescription
birthYearInt?Year of birth (1900~2100)
birthMonthInt?Month of birth (1~12)
birthDayInt?Day of birth (1~31)
genderGender?Gender
heightDouble?Height in cm (1~500). Saved to one decimal place (anything below is truncated)
weightDouble?Weight in kg (1~1000). Saved to one decimal place (anything below is truncated)
  • All items are optional. Items that are nil are not included in the request.
  • Values out of range throw a metadataValidationFailed error without sending a request to the server. The combination of month and day (for example, February 31) is not validated.

Error Codes

AsleepErrorCase
metadataValidationFailedInput validation failed before saving. message contains the invalid item
saveMetadataFailedSave failed (network or server error)
fetchMetadataFailedFetch failed (network or server error)

Did this page help you?