Create additional phonebook API methods

This topic shows you how to define another path and data mapping for the PUT, GET, and DELETE verbs. The steps in this topic are optional.

About this task

In this step, you will define a new path, /phonebook/contacts/{lastName}, and the data mapping for the associated PUT, GET, and DELETE verbs.

Table 1. Output for the display and delete actions
Action HTTP Verb Fields to return
Display a contact GET
  • IN-COMMAND to be assigned a value of DISPLAY.
  • Fields to return:
    • OUT-MESSAGE: To indicate success or failure of the action.
    • LAST-NAME, FIRST-NAME, EXTENSION, ZIP-CODE
    LAST-NAME, FIRST-NAME, EXTENSION, and ZIP-CODE.
Update a contact PUT
  • IN-COMMAND to be assigned a value of UPDATE.
  • Fields to return:
    • OUT-MESSAGE: To indicate success or failure of the action.
    • LAST-NAME, FIRST-NAME, EXTENSION, ZIP-CODE: To allow for validation of the updated contact record.
Delete a contact DELETE
  • IN-COMMAND to be assigned a value of DELETE.
  • Fields to return:
    • OUT-MESSAGE: To indicate success or failure of the action.
    • LAST-NAME: To allow for validation of the deleted record.

Procedure

To display, update, or delete a contact record for a given last name, define the path value and the map the request and response message for the action:

  1. In the z/OS® Connect API toolkit where the package.xml is displayed, add a path by clicking the Add a new path button (The add button in the user interface, which is a gray circle with a white plus sign in the center.) next to the Path label.
    Figure 1. Adding another path for the API
    The request mapping interface with a new path added.
  2. Enter the API path and the path parameter, lastName, enclosed in curly brackets. In this case, it is /contacts/{lastName}
  3. Remove the POST method by clicking the remove button (The remove button in the user interface, which is a gray circle with a white cross sign in the center.) next to the method.

The GET method

For the GET method (querying a contact record by last name), map the data structure between the service JSON schema and the HTTP request and response messages.

  1. For the GET method, click Service next to the method, select the phonebook service archive file, and click OK.
  2. Click File > Save or press Ctrl-S to save the service association.
  3. Click Mapping next to the GET method and select Open Request Mapping.
  4. Right-click the IN_COMMAND field in the service definition on the right, and select Add Assign transform.
    An Assign box shows up in the middle.
  5. Click the Assign box, and the Transform - Assign properties view opens.
  6. For the Value field, enter DISPLAY.
  7. Because only the last name is needed for the request and the other fields are not needed, remove those fields so they are hidden from the client.
    1. Hold down the Ctrl key to select multiple fields: IN_EXTENSION, IN_FIRST_NAME, and IN_ZIP_CODE.
    2. Right-click and select Add Remove transform.
    Figure 2. Removing unneeded fields from the requests
    The request mapping interface with IN_FIRST_NAME, IN_EXTENSION, and IN_ZIP_CODE fields removed.

    The last step is to map the path parameter lastName from the HTTP Request section to the IN_LAST_NAME field.

    Tip: You can right-click the field to undo an action, or right-click the added action button to delete the action.
  8. Click and drag the lastName path parameter field in the HTTP Request section to the IN_LAST_NAME field on the right.
    Figure 3. Mapping the lastName path parameter to the IN_LAST_NAME field
    The request mapping interface with the lastName path parameter from the HTTP request section mapped to the IN_LAST_NAME field.

    No JSON body is sent in with this request. All the information needed to display a contact record is carried on the URI with the path parameter.

  9. Click File > Save (Ctrl-S) to save your request mapping.
  10. Close the request mapping tab.

    The next step is to map the response. For the given last name, we want to return all fields except the OUT_COMMAND field, so we need to remove this field from the response.

Note: The response mapping is only effective when the request completes with HTTP status code 300 or lower.

  1. Click Mapping next to the GET method and select Open Response Mapping.
  2. Right-click OUT_COMMAND on the right, and select Add Remove transform.
    Figure 4. Removing the OUT_COMMAND field from the response
    The response mapping interface with the OUT_COMMAND field removed.
  3. Click File > Save (Ctrl-S) to save your response mapping and close the response tab.

The PUT method

For the PUT method (updating a record), the request and response mapping is similar to the GET method. The key tasks are as follows:
  • Assign a service that is associated with this method.
  • Assign a static value to the IN_COMMAND field. The command to update a record in the phonebook application is UPDATE, so set the static value to UPDATE.
  • The lastName path parameter is the key and its value needs to be moved to the IN_LAST_NAME field.
  • The IN_FIRST_NAME, IN_EXTENSION and IN_ZIP_CODE fields need to be exposed in the JSON body for this request for users to update.

  1. Map the request for the PUT method so the mapping looks as follows:
    Figure 5. Request mapping for the PUT method
    The request mapping interface with the lastName path parameter from the HTTP request section mapped to the IN_ LAST_NAME field.
  2. Save your changes.
  3. Close the request tab.

    Next map the response message. All fields should be returned to allow users to validate their updates.

  4. Map the response for the PUT method as follows:
    Figure 6. Response mapping for the PUT method
    The response mapping interface with the OUT_COMMAND field removed.
  5. Save your changes and close the response tab.
    Tip: With this mapping design, if only the extension for a specified last name is provided in the request message, the first name and zip code fields will be updated as blank. Ensuring that field values are not incorrectly wiped out would be the responsibility of the mobile application developer.

The DELETE method

For the DELETE method (deleting a record), the key mapping tasks are as follows:
  • Assign the service that is associated with this method and save
  • Assign a static value to the IN_COMMAND field. In this case, the command to delete a record in the phonebook application is DELETE, so set the value to DELETE.
  • The lastName path parameter is the key and its value needs to be moved to the IN_LAST_NAME field.
  • No fields need to be exposed in the request, so IN_EXTENSION, IN_FIRST_NAME, and IN_ZIP_CODE should be removed.

  1. Map the request for the DELETE method so the mapping looks as follows:
    Figure 7. Request mapping for the DELETE method
    The request mapping interface with the lastName path parameter from the HTTP request section mapped to the IN_ LAST_NAME field.
  2. Save your changes and close the request tab.

    Next map the response message. For a delete operation, all we are interested in is whether the action succeeded. This information is indicated in OUT_MESSAGE. Therefore all fields, except OUT_MESSAGE, should be removed.

  3. Map the response for the DELETE request as follows:
    Figure 8. Response mapping for the DELETE method
    The response mapping interface with all fields, except OUT_MESSAGE, removed.
  4. Save your changes and close the response tab.

Results

The API is now completed.