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 Name | Type | Description |
|---|---|---|
| apiKey | String | Enter the value obtained from the Generate API key |
| userId | String? | When there is no initial userId, enter nil, and later input the received userId. |
| baseUrl | URL? | Enter the proxy server address. If nil, default base url is used. |
| callbackUrl | URL? | Enter the URL of the server to receive sleep session analysis results. |
| service | Service? | Enter the app name |
| delegate | AsleepConfigDelegate | Delegate 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 Name | Type | Description |
|---|---|---|
| appId | String? | App ID issued from the dashboard |
| appSecret | String? | App Secret issued from the dashboard |
| isTestEnvironment | Bool? | 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)
}-
If success, userDidJoin() is called.
Property Name Type Description userId String generated userId config Asleep.Config genearated config -
If failure, didFailUserJoin() is called.
Property Name Type Description error Asleep.AsleepError Error Codes -
userDidDelete()
If success, all user data including the userId will be deleted.Property Name Type Description userId String Deleted userId
Delete User
Asleep.deleteUser()
var config: Asleep.Config?
if let config {
Asleep.deleteUser(config: config)
}| Property Name | Type | Description |
|---|---|---|
| config | Asleep.Config | Enter 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 Name | Type | Description |
|---|---|---|
| metadata | Asleep.Model.UserMetadata | User information to save |
| config | Asleep.Config | The 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 Name | Type | Description |
|---|---|---|
| config | Asleep.Config | The 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 Name | Type | Description |
|---|---|---|
| birthYear | Int? | Year of birth (1900~2100) |
| birthMonth | Int? | Month of birth (1~12) |
| birthDay | Int? | Day of birth (1~31) |
| gender | Gender? | Gender |
| height | Double? | Height in cm (1~500). Saved to one decimal place (anything below is truncated) |
| weight | Double? | Weight in kg (1~1000). Saved to one decimal place (anything below is truncated) |
- All items are optional. Items that are
nilare 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
| AsleepError | Case |
|---|---|
| metadataValidationFailed | Input validation failed before saving. message contains the invalid item |
| saveMetadataFailed | Save failed (network or server error) |
| fetchMetadataFailed | Fetch failed (network or server error) |
Updated about 1 month ago
Did this page help you?
