/*
 *  Copyright (c) 2019 Red Hat, Inc.
 *
 *  Red Hat licenses this file to you under the Apache License, version
 *  2.0 (the "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 *  implied.  See the License for the specific language governing
 *  permissions and limitations under the License.
 */

package io.quarkus.smallrye.restclient.runtime;

import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;
import org.eclipse.microprofile.rest.client.RestClientBuilder;

public class RestClientBase {

    public static final String REST_URL_FORMAT = "%s/mp-rest/url";

    private final Class<?> proxyType;
    private final String baseUriFromAnnotation;

    private final Config config;

    public RestClientBase(Class<?> proxyType, String baseUriFromAnnotation) {
        this.proxyType = proxyType;
        this.baseUriFromAnnotation = baseUriFromAnnotation;
        this.config = ConfigProvider.getConfig();
    }

    public Object create() {
        RestClientBuilder builder = RestClientBuilder.newBuilder();
        String baseUrl = getBaseUrl();
        try {
            return builder.baseUrl(new URL(baseUrl)).build(proxyType);
        } catch (MalformedURLException e) {
            throw new IllegalArgumentException("The value of URL was invalid " + baseUrl, e);
        } catch (Exception e) {
            if ("com.oracle.svm.core.jdk.UnsupportedFeatureError".equals(e.getClass().getCanonicalName())) {
                throw new IllegalArgumentException(baseUrl
                        + " requires SSL support but it is disabled. You probably have set quarkus.ssl.native to false.");
            }
            throw e;
        }
    }

    private String getBaseUrl() {
        if ((baseUriFromAnnotation == null) || baseUriFromAnnotation.isEmpty()) {
            String property = String.format(REST_URL_FORMAT, proxyType.getName());
            return config.getValue(property, String.class);
        }
        return baseUriFromAnnotation;
    }
}

@@ -736,16 +736,43 @@
 net.URL;
+%0Aimport java.util.Optional;
 %0A%0Aimport
@@ -971,67 +971,203 @@
    p
-ublic static final String REST_URL_FORMAT = %22%25s/mp-rest/url
+rivate static final String MP_REST = %22mp-rest%22;%0A    public static final String REST_URL_FORMAT = %22%25s/%22 + MP_REST + %22/url%22;%0A    public static final String REST_URI_FORMAT = %22%25s/%22 + MP_REST + %22/uri
 %22;%0A%0A
@@ -2298,76 +2298,219 @@
     
-if ((baseUriFromAnnotation == null) %7C%7C baseUriFromAnnota
+String propertyName = String.format(REST_URI_FORMAT, proxyType.getName());%0A        Optional%3CString%3E propertyOptional = config.getOptionalValue(propertyName, String.class);%0A        if (!propertyOp
 tion
+al
 .is
-Empty
+Present
 ()) 
@@ -2523,31 +2523,24 @@
         
-String 
 property
  = Strin
@@ -2531,16 +2531,20 @@
 property
+Name
  = Strin
@@ -2603,22 +2603,34 @@
         
-return
+propertyOptional =
  config.
@@ -2632,16 +2632,24 @@
 nfig.get
+Optional
 Value(pr
@@ -2654,16 +2654,20 @@
 property
+Name
 , String
@@ -2693,23 +2693,513 @@
         
-return 
+if (((baseUriFromAnnotation == null) %7C%7C baseUriFromAnnotation.isEmpty())%0A                && !propertyOptional.isPresent()) %7B%0A            throw new IllegalArgumentException(%0A                    String.format(%0A                            %22Unable to determine the proper baseUrl/baseUri. Consider registering using @RegisterRestClient(baseUri=%5C%22someuri%5C%22), or by adding '%25s' to your Quarkus configuration%22,%0A                            propertyName));%0A        %7D%0A        return propertyOptional.orElse(
 baseUriF
@@ -3207,22 +3207,23 @@
 omAnnotation
+)
 ;%0A    %7D%0A%7D%0A

Introduce handling for mp/uri in accordance to spec