001/*
002 *   Copyright 2020 Vonage
003 *
004 *   Licensed under the Apache License, Version 2.0 (the "License");
005 *   you may not use this file except in compliance with the License.
006 *   You may obtain a copy of the License at
007 *
008 *        http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *   Unless required by applicable law or agreed to in writing, software
011 *   distributed under the License is distributed on an "AS IS" BASIS,
012 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *   See the License for the specific language governing permissions and
014 *   limitations under the License.
015 */
016package com.vonage.client.sms;
017
018import com.vonage.client.AbstractMethod;
019import com.vonage.client.HttpWrapper;
020import com.vonage.client.auth.TokenAuthMethod;
021import org.apache.http.HttpResponse;
022import org.apache.http.client.methods.RequestBuilder;
023import org.apache.http.impl.client.BasicResponseHandler;
024
025import java.io.IOException;
026import java.io.UnsupportedEncodingException;
027
028public class SmsSearchEndpoint extends AbstractMethod<SearchSmsRequest, SearchSmsResponse> {
029
030    private static final Class[] ALLOWED_AUTH_METHODS = new Class[]{TokenAuthMethod.class};
031
032    private static final String PATH = "/search/messages";
033
034    public SmsSearchEndpoint(HttpWrapper httpWrapper) {
035        super(httpWrapper);
036    }
037
038    @Override
039    protected Class[] getAcceptableAuthMethods() {
040        return ALLOWED_AUTH_METHODS;
041    }
042
043    @Override
044    public RequestBuilder makeRequest(SearchSmsRequest request) throws UnsupportedEncodingException {
045        RequestBuilder requestBuilder = RequestBuilder.get(httpWrapper.getHttpConfig().getRestBaseUri() + PATH);
046        request.addParams(requestBuilder);
047        return requestBuilder;
048    }
049
050    @Override
051    public SearchSmsResponse parseResponse(HttpResponse response) throws IOException {
052        return SearchSmsResponse.fromJson(new BasicResponseHandler().handleResponse(response));
053    }
054}