Skip to content
This repository has been archived by the owner on Nov 19, 2018. It is now read-only.

Commit

Permalink
Added automatic paging for all resources and added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KrauseFx committed May 24, 2015
1 parent df15b0a commit d594c09
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 22 deletions.
73 changes: 51 additions & 22 deletions lib/spaceship/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,29 @@ def api_key
end
end

# Automatic paging

def page_size
@page_size ||= 500
end

# Handles the paging for you... for free
# Just pass a block and use the parameter as page number
def paging
page = 0
results = []
loop do
page += 1
current = yield(page)

results = results + current

break if ((current || []).count < page_size) # no more results
end

return results
end

##
# perform login procedure. this sets a cookie that will be used in subsequent requests
# raises InvalidUserCredentialsError if authentication failed.
Expand Down Expand Up @@ -105,13 +128,15 @@ def current_team_id=(team_id)
end

def apps
r = request(:post, 'account/ios/identifiers/listAppIds.action', {
teamId: team_id,
pageNumber: 1,
pageSize: 500,
sort: 'name=asc'
})
parse_response(r, 'appIds')
paging do |page_number|
r = request(:post, 'account/ios/identifiers/listAppIds.action', {
teamId: team_id,
pageNumber: page_number,
pageSize: page_size,
sort: 'name=asc'
})
parse_response(r, 'appIds')
end
end

def create_app!(type, name, bundle_id)
Expand Down Expand Up @@ -153,24 +178,28 @@ def delete_app!(app_id)
end

def devices
r = request(:post, 'account/ios/device/listDevices.action', {
teamId: team_id,
pageNumber: 1,
pageSize: 500,
sort: 'name=asc'
})
parse_response(r, 'devices')
paging do |page_number|
r = request(:post, 'account/ios/device/listDevices.action', {
teamId: team_id,
pageNumber: page_number,
pageSize: page_size,
sort: 'name=asc'
})
parse_response(r, 'devices')
end
end

def certificates(types)
r = request(:post, 'account/ios/certificate/listCertRequests.action', {
teamId: team_id,
types: types.join(','),
pageNumber: 1,
pageSize: 500,
sort: 'certRequestStatusCode=asc'
})
parse_response(r, 'certRequests')
paging do |page_number|
r = request(:post, 'account/ios/certificate/listCertRequests.action', {
teamId: team_id,
types: types.join(','),
pageNumber: page_number,
pageSize: page_size,
sort: 'certRequestStatusCode=asc'
})
parse_response(r, 'certRequests')
end
end

def create_certificate!(type, csr, app_id = nil)
Expand Down
13 changes: 13 additions & 0 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,19 @@
end
end

describe "#paging" do
it "default page size is correct" do
expect(subject.page_size).to eq(500)
end

it "Properly pages if required with support for a custom page size", now: true do
allow(subject).to receive(:page_size).and_return(8)

expect(subject.devices.count).to eq(9)
expect(subject.devices.last['name']).to eq("The last phone")
end
end

describe '#devices' do
let(:devices) { subject.devices }
it 'returns a list of device hashes' do
Expand Down
65 changes: 65 additions & 0 deletions spec/fixtures/listDevicesPage1-2.action.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"creationTimestamp": "2015-05-24T16:37:32Z",
"resultCode": 0,
"userLocale": "en_US",
"protocolVersion": "QH65B2",
"requestId": null,
"requestUrl": "https://developer.apple.com:443//services-account/QH65B2/account/ios/device/listDevices.action",
"responseId": "881a1f34-bc13-425b-ae18-9f4f8fb40aaa",
"isAdmin": true,
"isMember": false,
"isAgent": true,
"pageNumber": 1,
"pageSize": 8,
"totalRecords": 9,
"httpResponseHeaders": null,
"devices": [{
"deviceId": "9T5RA84AAA",
"name": "Felix Krause's iPhone 6",
"deviceNumber": "aaabbbccccddddaaabbb",
"devicePlatform": "ios",
"status": "c"
}, {
"deviceId": "L4378H2AAA",
"name": "Felix's iPad",
"deviceNumber": "aaabbbccccddddaaabbb",
"devicePlatform": "ios",
"status": "c"
}, {
"deviceId": "LEL449RAAA",
"name": "4s",
"deviceNumber": "aaabbbccccddddaaabbb",
"devicePlatform": "ios",
"status": "c"
}, {
"deviceId": "S4227Y4AAA",
"name": "Simon iOS",
"deviceNumber": "aaabbbccccddddaaabbb",
"devicePlatform": "ios",
"status": "c"
}, {
"deviceId": "5YTNZ5AAAA",
"name": "iPhone 4S",
"deviceNumber": "aaabbbccccddddaaabbb",
"devicePlatform": "ios",
"status": "c"
}, {
"deviceId": "WXQ7V239BE",
"name": "iPhone 4s",
"deviceNumber": "aaabbbccccddddaaabbb",
"devicePlatform": "ios",
"status": "c"
}, {
"deviceId": "9K3WR66AAA",
"name": "MyName",
"deviceNumber": "aaabbbccccddddaaabbb",
"devicePlatform": "ios",
"status": "c"
}, {
"deviceId": "GD25LDGAAA",
"name": "iPhonekajsdf",
"deviceNumber": "aaabbbccccddddaaabbb",
"devicePlatform": "ios",
"status": "c"
}]
}
23 changes: 23 additions & 0 deletions spec/fixtures/listDevicesPage2-2.action.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"creationTimestamp": "2015-05-24T16:37:33Z",
"resultCode": 0,
"userLocale": "en_US",
"protocolVersion": "QH65B2",
"requestId": null,
"requestUrl": "https://developer.apple.com:443//services-account/QH65B2/account/ios/device/listDevices.action",
"responseId": "558ba377-d979-45c6-b2f8-ee6d2d692aaa",
"isAdmin": true,
"isMember": false,
"isAgent": true,
"pageNumber": 2,
"pageSize": 8,
"totalRecords": 9,
"httpResponseHeaders": null,
"devices": [{
"deviceId": "XJXGVS4AAAA",
"name": "The last phone",
"deviceNumber": "4c24a7ee5c8674847f49b4fb2d87483053faaaaa",
"devicePlatform": "ios",
"status": "c"
}]
}
10 changes: 10 additions & 0 deletions spec/spaceship_stubbing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ def stub_devices
stub_request(:post, 'https://developer.apple.com/services-account/QH65B2/account/ios/device/listDevices.action').
with(:body => {:teamId => 'XXXXXXXXXX', :pageSize => "500", :pageNumber => "1", :sort => 'name=asc'}, :headers => {'Cookie' => 'myacinfo=abcdef;'}).
to_return(:status => 200, :body => read_fixture_file('listDevices.action.json'), :headers => {'Content-Type' => 'application/json'})

# Custom paging
stub_request(:post, "https://developer.apple.com/services-account/QH65B2/account/ios/device/listDevices.action").
with(:body => {"pageNumber"=>"1", "pageSize"=>"8", "sort"=>"name=asc", "teamId"=>"XXXXXXXXXX"},
:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/x-www-form-urlencoded', 'Cookie'=>'myacinfo=abcdef;', 'User-Agent'=>'spaceship'}).
to_return(:status => 200, :body => read_fixture_file('listDevicesPage1-2.action.json'), :headers => {'Content-Type' => 'application/json'})
stub_request(:post, "https://developer.apple.com/services-account/QH65B2/account/ios/device/listDevices.action").
with(:body => {"pageNumber"=>"2", "pageSize"=>"8", "sort"=>"name=asc", "teamId"=>"XXXXXXXXXX"},
:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/x-www-form-urlencoded', 'Cookie'=>'myacinfo=abcdef;', 'User-Agent'=>'spaceship'}).
to_return(:status => 200, :body => read_fixture_file('listDevicesPage2-2.action.json'), :headers => {'Content-Type' => 'application/json'})
end

def stub_certificates
Expand Down

3 comments on commit d594c09

@orff
Copy link

@orff orff commented on d594c09 May 6, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing on the provisioning profiles which is most likely to have a high number. Testing out a fix based on your commit.

@KrauseFx
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a very old commit. What do you mean?

@orff
Copy link

@orff orff commented on d594c09 May 6, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean you set up pagination on most of the objects, but forgot to paginate the provisioning profiles which for us has the most items.

Might also be related to: fastlane/fastlane#4280

We are working on a pull request for you, but here is the working local code for the provisioning profiles method:

`def provisioning_profiles(include_invalid_profiles=true, mac: false)

      @page_size = 30
      paging do |page_number|
    
        puts "provisioning profiles page '#{page_number}'..."
        req = request(:post) do |r|

          r.url "https://developerservices2.apple.com/services/#{PROTOCOL_VERSION}/#{platform_slug(mac)}/listProvisio..."
          r.params = {
            teamId: team_id,
            includeInactiveProfiles: include_invalid_profiles,
            onlyCountLists: true,
            pageSize:page_size,
            pageNumber:page_number,
            sidx: "name",
            sort: "name=dasc"
          }
        end

      parse_response(req, 'provisioningProfiles')
    end
  end`

Note: For us the default page size of 500 did not work, we had to back it down to < 200.

Please sign in to comment.