getCampaigns method

  1. @override
Future<Campaigns> getCampaigns(
  1. String envId,
  2. String visitorId,
  3. Map<String, Object> context
)
override

Implementation

@override
Future<Campaigns> getCampaigns(
    String envId, String visitorId, Map<String, Object> context) async {
  // Create url
  String urlString = Endpoints.DECISION_API + envId + Endpoints.CAMPAIGNS;
  // if the consent is false , we set the sendContext to false
  if (isConsent() == false) {
    urlString = urlString + Endpoints.DO_NOT_SEND_CONTEXT;
  }

  Flagship.logger(Level.INFO, 'GET CAMPAIGNS :' + urlString);

  // create headers
  Map<String, String> fsHeaders = {
    "x-api-key": Flagship.sharedInstance().apiKey ?? "",
    "x-sdk-client": "flutter",
    "x-sdk-version": FlagshipVersion,
    "Content-type": "application/json"
  };

  // Create data to post
  Object data = json.encode(
      {"visitorId": visitorId, "context": context, "trigger_hit": false});
  var response = await service.sendHttpRequest(
      RequestType.Post, urlString, fsHeaders, data,
      timeoutMs:
          Flagship.sharedInstance().getConfiguration()?.timeout ?? TIMEOUT);
  switch (response.statusCode) {
    case 200:
      Flagship.logger(Level.ALL, response.body, isJsonString: true);
      return Campaigns.fromJson(json.decode(response.body));
    default:
      Flagship.logger(
        Level.ALL,
        "Failed to synchronize",
      );
      throw Exception('Flagship, Failed to synchronize');
  }
}