Skip to content

Instantly share code, notes, and snippets.

@adamyanalunas
Created November 14, 2019 02:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamyanalunas/a7b814a9b7486b617bdb9c10bf1087a8 to your computer and use it in GitHub Desktop.
Save adamyanalunas/a7b814a9b7486b617bdb9c10bf1087a8 to your computer and use it in GitHub Desktop.
A template for creating a request to set an App ID’s data protection capability

How to use

Preparation

Make sure you’ve requested Connect API access and set up a key in App Store Connect.

This example expects the relevant environment variables used to create a token to already be set.

Examples

Thankfully, this endpoint works whether data protection is already set or not. This means you can call it multiple times with different level values.

set_data_protection(bundle_id: 'com.some.bundle-id', level: 'COMPLETE_PROTECTION')
set_data_protection(bundle_id: 'com.some.bundle-id', level: 'PROTECTED_UNLESS_OPEN')

Other uses

This is just example code. It could easily be modified to allow disabling data protection.

You could make this much more generic to handle enabling or disabling any capability.

I hope this helps you get past any blocks around data protection or capability settings!

def set_data_protection(bundle_id: nil, level: nil)
puts "Setting data protection level to `#{level}` for `#{bundle_id}`"
token = Spaceship::ConnectAPI::Token.create()
client = Spaceship::ConnectAPI::Provisioning::Client.new(token: token)
# Bundle IDs have, themselves, a unique ID. We’ll have to look that up first before firing off this request.
bundle_info = Spaceship::ConnectAPI::BundleId.all(filter: {identifier: bundle_id}).first
bundle_id_id = bundle_info.id
result = client.request(:post) { |req|
req.url("bundleIdCapabilities")
req.headers['Content-Type'] = 'application/json'
req.body = {
data: {
type: "bundleIdCapabilities",
attributes: {
capabilityType: "DATA_PROTECTION",
settings: [{
key: "DATA_PROTECTION_PERMISSION_LEVEL",
options: [{
key: level,
enabled: true
}]
}]
},
relationships: {
bundleId: {
data: {
id: bundle_id_id,
type: "bundleIds"
}
}
}
}
}.to_json
}
result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment