All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.urbanairship.api.customevents.CustomEventRequest Maven / Gradle / Ivy

There is a newer version: 9.4.2
Show newest version
package com.urbanairship.api.customevents;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.base.Preconditions;
import com.google.common.net.HttpHeaders;
import com.urbanairship.api.client.Request;
import com.urbanairship.api.client.RequestUtils;
import com.urbanairship.api.client.ResponseParser;
import com.urbanairship.api.customevents.model.CustomEventPayload;
import com.urbanairship.api.customevents.model.CustomEventResponse;
import com.urbanairship.api.push.parse.PushObjectMapper;
import org.apache.http.entity.ContentType;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CustomEventRequest implements Request {

    private final static String API_CUSTOM_EVENTS_PATH = "/api/custom-events/";

    private final List payloads;

    private CustomEventRequest(CustomEventPayload payload) {
        Preconditions.checkNotNull(payload, "Payload required when creating a custom-events request");
        this.payloads = Collections.singletonList(payload);
    }

    public CustomEventRequest(List customEventPayloads) {
        Preconditions.checkNotNull(customEventPayloads, "Payloads required when creating a custom-events request");
        Preconditions.checkArgument(!customEventPayloads.isEmpty(), "Payloads required when creating a custom-events request");
        this.payloads = customEventPayloads;
    }

    public static CustomEventRequest newRequest(CustomEventPayload customEventPayload) {
        return new CustomEventRequest(customEventPayload);
    }

    public static CustomEventRequest newRequest(List customEventPayloads) {
        return new CustomEventRequest(customEventPayloads);
    }

    @Override
    public HttpMethod getHttpMethod() {
        return HttpMethod.POST;
    }

    @Override
    public String getRequestBody() {
        try {
            return PushObjectMapper.getInstance().writeValueAsString(payloads);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public ContentType getContentType() {
        return ContentType.APPLICATION_JSON;
    }

    @Override
    public Map getRequestHeaders() {
        Map headers = new HashMap();
        headers.put(HttpHeaders.CONTENT_TYPE, CONTENT_TYPE_JSON);
        headers.put(HttpHeaders.ACCEPT, UA_VERSION_JSON);
        return headers;
    }

    @Override
    public URI getUri(URI baseUri) throws URISyntaxException {
        return RequestUtils.resolveURI(baseUri, API_CUSTOM_EVENTS_PATH);
    }

    @Override
    public ResponseParser getResponseParser() {
        return response -> PushObjectMapper.getInstance().readValue(response, CustomEventResponse.class);
    }

    @Override
    public boolean bearerTokenAuthRequired() {
        return true;
    }

    @Override
    public boolean canUseBearerTokenAuth() {
        return true;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy