def find_operation(api, key_op):
  """
  Find an operation in api that matches key_op. This method first attempts to find a match
  by using the operation name (nickname). Failing that, it attempts to match up HTTP methods.

  Args:
    api - A Swagger API description (dictionary)
    key_op - A Swagger operation description (dictionary)

  Returns:
    An operation that matches key_op, or None if nothing found
  """
  operations = api['operations']
  for op in operations:
    if op['nickname'] == key_op['nickname']:
      return op

  for op in operations:
    if op['method'] == key_op['method']:
      return op
  return None