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

com.tinypass.client.anon.api.OauthApi Maven / Gradle / Ivy

There is a newer version: 16.331.0
Show newest version
package com.tinypass.client.anon.api;

import com.tinypass.client.common.*;
import com.tinypass.client.anon.model.OAuthToken;

import java.util.*;
import java.math.*;
import java.io.*;

public class OauthApi {

	private String basePath = "https://api.piano.io/api/v3";
	private String token;
	private ApiInvoker apiInvoker = null;
	private Map headerParams = new LinkedHashMap();

	public OauthApi(String basePath, String token) {
		this.basePath = basePath;
		this.token = token;
	}

	public OauthApi(String basePath, String token, ApiInvoker invoker, Map headerParams) {
		this(basePath, token);
		this.apiInvoker = invoker;
		this.headerParams = headerParams;
	}

	public ApiInvoker getInvoker() {
        if(apiInvoker == null) apiInvoker = ApiInvoker.getInstance();
		return apiInvoker;
	}

	public void setApiToken(String token){
		this.token = token;
	}

	public void setBasePath(String basePath) {
		this.basePath = basePath;
	}

	public String getBasePath() {
		return basePath;
	}

	public String toQueryParam(Object arg) {
        if(arg == null)
            return null;
		if (arg instanceof Date) {
			Date date = (Date) arg;
			long epochTime = date.getTime() / 1000;
			return String.valueOf(epochTime);
		} else {
			return String.valueOf(arg);
		}

	}

    public void setQueryParam(Map> params, String paramName, Object value) {
        List currentValues = params.computeIfAbsent(paramName, k -> new ArrayList<>());
        if (value instanceof List) {
            List values = (List) value;
            values.forEach(o -> addQueryParam(currentValues, o));
        } else {
            addQueryParam(currentValues, value);
        }
    }

    private void addQueryParam(List currentValues, Object value) {
        String v = toQueryParam(value);
        if (v != null)
            currentValues.add(v);
    }


    /**
      * API Request for authToken
      */
	public class authTokenRequest implements ApiRequest {

        Map headerParams;
        Map> queryParams = new HashMap<>();
        Map> formParams = new HashMap<>();
        Object body = null;

        public authTokenRequest(Map headerParams) {
            this.headerParams = headerParams;
        }

        public Map> getPreparedFormParams() {
            return Collections.unmodifiableMap(formParams);
        }


		public authTokenRequest clientId(String clientId) {
            setQueryParam(this.formParams, "client_id", clientId);
			return this;
		}
		public authTokenRequest clientSecret(String clientSecret) {
            setQueryParam(this.formParams, "client_secret", clientSecret);
			return this;
		}
		public authTokenRequest code(String code) {
            setQueryParam(this.formParams, "code", code);
			return this;
		}
		public authTokenRequest refreshToken(String refreshToken) {
            setQueryParam(this.formParams, "refresh_token", refreshToken);
			return this;
		}
		public authTokenRequest grantType(String grantType) {
            setQueryParam(this.formParams, "grant_type", grantType);
			return this;
		}
		public authTokenRequest redirectUri(String redirectUri) {
            setQueryParam(this.formParams, "redirect_uri", redirectUri);
			return this;
		}
		public authTokenRequest username(String username) {
            setQueryParam(this.formParams, "username", username);
			return this;
		}
		public authTokenRequest password(String password) {
            setQueryParam(this.formParams, "password", password);
			return this;
		}
		public authTokenRequest state(String state) {
            setQueryParam(this.formParams, "state", state);
			return this;
		}
		public authTokenRequest deviceId(String deviceId) {
            setQueryParam(this.formParams, "device_id", deviceId);
			return this;
		}





		public OAuthToken  execute() throws ApiException{

			String path = "/oauth/authToken".replaceAll("\\{format\\}","json");

			return getInvoker().invokeAPI(basePath, path, "POST", token,
                                                     queryParams, null,
                                                     headerParams,
                                                     formParams,
                                                     "",
                                                     OAuthToken.class);
		}

	}


    /**
      * Helper method to create a new request.
      */
	public authTokenRequest authTokenRequest(){
		return new authTokenRequest(headerParams);
	}




}