createAgent method

Future<ManagedAgent> createAgent({
  1. required String projectId,
  2. required Map<String, dynamic> configuration,
  3. bool ifNotExists = false,
})

Implementation

Future<ManagedAgent> createAgent({
  required String projectId,
  required Map<String, dynamic> configuration,
  bool ifNotExists = false,
}) async {
  final encodedProjectId = Uri.encodeComponent(projectId);
  final uri = Uri.parse('$baseUrl/accounts/projects/$encodedProjectId/agents');
  final body = {'configuration': _jsonMapWithoutNulls(configuration), 'if_not_exists': ifNotExists};
  final response = await httpClient.post(uri, body: jsonEncode(body));

  if (response.statusCode == 409) {
    throw NameInUseException("The agent name is already in use");
  } else if (response.statusCode >= 400) {
    throw MeshagentException(
      'Failed to create agent. '
      'Status code: ${response.statusCode}, body: ${response.body}',
    );
  }

  return ManagedAgent.fromJson(jsonDecode(response.body) as Map<String, dynamic>);
}