KUMAR, ANISH

Getting started with Pre-positioning using Mobile App Performance (MAP) SDK

 

Prepositioning helps in downloading larger assets in advance and serving them to the end-user when needed from the cache. The assets can be images, JSON, or any type of cacheable response.

Once assets are prepositioned, they are downloaded by Mobile App Performance (MAP) SDK and saved in the cache. You can access them using normal API/ image requests from your mobile app and responses will be served from cache.

If the response for any requests is not yet downloaded then such requests would continue to go over the network as usual and fetch a response.

 

Required For Setup:

1. Follow our Getting Started guide and configure MAP SDK if not already configured.

2. Locate the MAP SDK configuration where you would like to enable pre-positioning.

  • Click on the drop-down arrow next to “Actions”.
  • Click “Edit App Config”.

img1

3. Follow instructions documented in our Configuration Guide (Page 5) to enable pre-positioning.

 

Global Controls for Pre-Positioning under MAP SDK Configuration

1. Turn on/off toggle helps enable/disable Pre-Positioning

img 2

2. Configure to Download over WiFi and Cellular for best results.

 

Adding Content

1. Click on the ingestion tab while you are in MAP configuration (edit mode), to add segments (categorization) and objects (content/responses).

img3

2. Click on add segments button, to get started

img 4

3. Add objects to the segment

img5

4. Configure the segment to your preference

  • Name - Name the segments to your preference.
  • Priority - Add a priority to segment, this is optional.
    • 1 is the highest priority and 100 is the lowest.
    • Segments with the highest priority will be downloaded first.
  • Network - Use global setting or configure network settings, for each segment.
  • URLs - Add URLs to download

5. Once you have configured the segment, add it by clicking the “Add Segment” button.

6. Click on the “Save Changes” button, to save the changes.

img 9

7. Editing segment

img 10

Priority - Update the priority of segment

Network - Update network for downloading

URLs - Add URLs or delete URLs or Replace all URLs

Once you have edited the segment, save it by tapping the “Save Changes” button.

 

8. Download Host Mappings

If you would like to introduce host mapping for primary hostnames used during pre-positioning, you can manage it from the “Download host mappings” section.

img 11

 

Working with Akamai Open API to manage ingestion for Pre-Positioning 

  1. Configure OpenAPI Credentials (Ensure your newly generated credentials have read/write access for “MAP Ingest API”)

  2. Design content ingestion workflow with our APIs documented under Mobile Application Performance Ingest API v1 documentation.

  3. Refer to the example shared below to help demonstrate pre-positioning using Akamai OpenAPI with httpie-edgegrid

 

Example of working with HTTPIE to ingest content for pre-positioning using APIs

1. Install HTTPIE: https://httpie.org/

$ brew install httpie

 

2. install EdgeGrid from: https://github.com/akamai/httpie-edgegrid

$ pip install httpie-edgegrid

 

3. You may require OpenSSL:

$ pip install --ignore-installed pyOpenSSL

 

4. create ~/.edgerc file

  • For client secret required in further steps, send email to specialist@akamai.com
  • Create a text file with the following content:

Name: Sample
Base URL: xxxxxxxxxxxxxxxxxxxxxxx.akamaiapis.net
Access Tokens: xxxx-xxxxxxxxxxx-xxxxxxxxxxxxxx
Client Credentials:
Client token: xxxxx-xxxxxxxxxxxx-xxxxxxxxxxx    
Secret: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

  • Create .edgerc file with a segment name. Here we are naming it as MAP.

Usage: python gen_edgerc.py -s <section_name> -f <export_file>
$ python gen_edgerc.py -s MAP -f api.txt

  • Output: ~/.edgerc will becreated in the User directory with following content.

[MAP]
client_secret = xxxxxxxxxxxxxxxxxxxxxxxxx+xxxxxx=
host = xxxx-xxxxxxxxxxxxxxxxxxxxxxxx.xxx.xxxx.net
access_token = xxxx-xxxxxxxxxxxxxxxxx-xxxxxxxxxxxxxx
client_token = xxxx-xxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxx
max-body = 131072

 

5. List applications

Returns a list of MAP SDK applications enabled on your account.

usage: http --auth-type edgegrid -a  <section_name>: :/<api_endpoint>
$ http --auth-type edgegrid -a MAP: :/mobile-app-perf-sdk-api/v1/applications

Response:

[
    {
        "appId": 7201,
        "appName": "MAP-App1",
        "sdkKey": "xxxxxxxxxxxxxxxxx",
        "activeDevices": 10
    },
    {
        "appId": 7202,
        "appName": "MAP-App2",
        "sdkKey": "xxxxxxxxxxxxxxxxx",
        "activeDevices": 10
    }
]

 

6. Create and publish a new segment

a) Create a json file and name it ingest.json as shared in the example below. You could also change the segment name and URLs to your preference for this exercise.

[
  {
    "networkSelection": "WIFI_AND_CELLULAR",
    "segment": "map-segment-3",
    "priority": "1",
    "downloadHostMapping":[
      {
        "urlHostname":"static.akamai.com",
        "downloadHostname":"secure.akamai.com"
      }
    ],
    "urls": [
      {
        "url": "https://www.akamai.com/1"
      },
      {
        "url": "https://static.akamai.com/2"
      }   ]
  },
  {
    "networkSelection": "WIFI_ONLY",
    "segment": "map-segment-4",
    "priority": "10",
    "urls": [
      {
        "url": "https://www.akamai2.com/10"
      },
      {
        "url": "https://www.akamai2.com/11"
      }
    ]
  }
]

 

b) Run the command:

Usage : http --auth-type edgegrid -a <section_name>: POST :/<api_endpoint> < requestBody.json
$ http --auth-type edgegrid -a MAP: POST :/mobile-app-perf-sdk-api/v1/applications/7202/ingest < ingest.json

 

c) Validate response from API server to confirm ingestion was successful. In this case we see “201 Created” indicative of success.

HTTP/1.1 201 Created
Connection: keep-alive
Content-Length: 0
Date: Thu, 04 Jun 2020 11:31:02 GMT
X-Trace-Id: 8d335ed8db6b0008

 

d) You can also see the segment getting created on Akamai Control Center under your MAP configuration (Ingestion Tab).

https://control.akamai.com/apps/mobile-app-perf-sdk/#/applications/xxxx/ingestionxxxx is your AppId.

img 12

 

Subscribing to Segments

Static Subscription - iOS

  1. Subscribe Segments in Info.plist

  2. In XCode project, open Info.plist

  3. Add segments as Array in the map dictionary and add the segment string.

Earlier we had created a segment Numbers, adding it in the plist.

img 13

Static Subscription - Android

  1. Android Studio → Your project → res → xml → open akamai_sdk_init.xml.
  2. Add comma separated segment names as below.

Earlier we had created a segment Numbers, adding it to akamai_sdk_init.xml.

<com_akamai_sdk_init>
<com_akamai_map_license_key>XXXYYY4798389VVV27da2</com_akamai_map_license_key>
<com_akamai_map_segments>Numbers</com_akamai_map_segments>
</com_akamai_sdk_init>

Dynamic Subscription - iOS

You can also subscribe segments in code at runtime. Make sure that you have already created segments in the portal.

[[AkaMap shared] subscribeSegments:[NSSet setWithArray:@[@"Alphabets"]]];

Dynamic Subscription - Android

AkaMap mMapInstance = AkaMap.getInstance();
String segments = "Alphabets";
mMapInstance.subscribeSegments(new HashSet<>(Arrays.asList(segments)));

 

Verify Cache

1. Using cacheAdded method

You can verify the cache added to a local device using the cacheAdded method.

- (void)cacheAdded:(nullable NSSet<AkaCachedFile*> *)added updated:(nullable NSSet<AkaCachedFile*> *)updated removed:(nullable NSSet<AkaCachedFile*> *)removed
{
    for (AkaCachedFile *files in added) {
        NSLog(@"File URL = %@\n MIME = %@\n ResponseHeaders = %@\n, localPath =%@", files.urlString, files.inferredMIMEType, files.responseHeaders,files.localPath);
    }
    NSLog(@"Added count: %ld", [added count]);
    NSLog(@"Updated count: %ld", [updated count]);
    NSLog(@"Removed count: %ld", [removed count]);
}

The above code will provide details of objects added, updated and removed from cache.It will provide more information on each object's cache URL, MIME type, Response Headers and LocalPath.

2. Charles Proxy

Charles Proxy will provide details of URLs which are downloaded using pre-positioning.In the below image we can see images being downloaded. 

The images are not requested from code, they are downloaded based on segments configured on Akamai Control Center under your MAP SDK configuration.

img 14

The example shared above request a prepositioned image for an iOS App:

completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
        self.imageView.image = image;
}];

In the above example 1.jpg is already prepositioned, and can be accessed the way how we normally request an API or an image from the app without any code change as long as MAP SDK is integrated.

For more information check our integration guides listed under our developer landing page here.