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

com.microsoft.bingads.internal.restful.ReportingService Maven / Gradle / Ivy

Go to download

The Bing Ads Java SDK is a library improving developer experience when working with the Bing Ads services by providing high-level access to features such as Bulk API, OAuth Authorization and SOAP API.

There is a newer version: 13.0.22.1
Show newest version
package com.microsoft.bingads.internal.restful;

import java.util.Map;
import java.util.concurrent.Future;
import java.util.function.BiFunction;
import java.util.function.Supplier;

import jakarta.xml.ws.AsyncHandler;
import jakarta.xml.ws.Response;

import com.microsoft.bingads.ApiEnvironment;
import com.microsoft.bingads.InternalException;
import com.microsoft.bingads.v13.reporting.*;

public class ReportingService extends RestfulServiceClient implements IReportingService {

	private Supplier fallbackService;

	public ReportingService(Map headers, ApiEnvironment env, Supplier fallbackService) {
        super(headers, env, IReportingService.class);

		this.fallbackService = fallbackService;
    }

	Exception processFaultDetail(ApplicationFault faultDetail, boolean shouldThrow) 
			throws AdApiFaultDetail_Exception, ApiFaultDetail_Exception {
		String message = getFaultMessage(faultDetail.getTrackingId());

		if (faultDetail instanceof AdApiFaultDetail) {
			AdApiFaultDetail_Exception ex = new AdApiFaultDetail_Exception(message, (AdApiFaultDetail)faultDetail);
			
			if (shouldThrow) {				
				throw ex;
			} 
			
			return ex;
		}

		if (faultDetail instanceof ApiFaultDetail) {
			ApiFaultDetail_Exception ex = new ApiFaultDetail_Exception(message, (ApiFaultDetail)faultDetail);
			
			if (shouldThrow) {				
				throw ex;
			} 
			
			return ex;
		}

		if (shouldThrow) {
			throw new RuntimeException("Unknown fault type: " + faultDetail.getClass());
		}

		return null;
	}

	protected  Resp sendRequest(Req request, String entityEndpoint, String verb, Class respClass)
			throws AdApiFaultDetail_Exception, ApiFaultDetail_Exception {
		ResponseInfo responseInfo = getResponseInfo(request, entityEndpoint, verb, respClass, ApplicationFault.class);

		if (responseInfo == null) {
			return null;
		}

		ApplicationFault faultDetail = responseInfo.getFaultDetail();

		if (faultDetail != null) {
			processFaultDetail(faultDetail, true);
		}

		return responseInfo.getResponse();
	}

	private Exception getFaultException(ApplicationFault faultDetail) {
		try {
			Exception ex = processFaultDetail(faultDetail, false);

			return ex;
		} catch (Exception ex) {
			throw new InternalException(ex);
		}
	}

	protected  Response sendRequestAsync(Req request, String entityEndpoint, String verb, Class respClass, BiFunction, Future> soapMethod, AsyncHandler handler) {
		return processRequestAsync(request, entityEndpoint, verb, respClass, ApplicationFault.class, x -> getFaultException(x), soapMethod, handler);
	}
		
	public SubmitGenerateReportResponse submitGenerateReport(SubmitGenerateReportRequest request)
		  throws AdApiFaultDetail_Exception, ApiFaultDetail_Exception {
		SubmitGenerateReportResponse response = sendRequest(request, "/GenerateReport/Submit", HttpPost, SubmitGenerateReportResponse.class);
			
		if (response == null) {
			response = fallbackService.get().submitGenerateReport(request);
		}
		
		return response;
	}

    public Response submitGenerateReportAsync(SubmitGenerateReportRequest request) {
		return sendRequestAsync(request, "/GenerateReport/Submit", HttpPost, SubmitGenerateReportResponse.class, (r, h) -> fallbackService.get().submitGenerateReportAsync(r, h), null);
	}

	public Future submitGenerateReportAsync(SubmitGenerateReportRequest request, AsyncHandler asyncHandler) {
		return sendRequestAsync(request, "/GenerateReport/Submit", HttpPost, SubmitGenerateReportResponse.class, (r, h) -> fallbackService.get().submitGenerateReportAsync(r, h), asyncHandler);
	}
	
    	
	public PollGenerateReportResponse pollGenerateReport(PollGenerateReportRequest request)
		  throws AdApiFaultDetail_Exception, ApiFaultDetail_Exception {
		PollGenerateReportResponse response = sendRequest(request, "/GenerateReport/Poll", HttpPost, PollGenerateReportResponse.class);
			
		if (response == null) {
			response = fallbackService.get().pollGenerateReport(request);
		}
		
		return response;
	}

    public Response pollGenerateReportAsync(PollGenerateReportRequest request) {
		return sendRequestAsync(request, "/GenerateReport/Poll", HttpPost, PollGenerateReportResponse.class, (r, h) -> fallbackService.get().pollGenerateReportAsync(r, h), null);
	}

	public Future pollGenerateReportAsync(PollGenerateReportRequest request, AsyncHandler asyncHandler) {
		return sendRequestAsync(request, "/GenerateReport/Poll", HttpPost, PollGenerateReportResponse.class, (r, h) -> fallbackService.get().pollGenerateReportAsync(r, h), asyncHandler);
	}
	
    }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy