View Javadoc
1   /*
2    * Copyright 2022 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.bremersee.apiclient.webflux.contract;
18  
19  import java.util.Arrays;
20  import java.util.Optional;
21  import org.springframework.web.reactive.function.client.WebClient;
22  import org.springframework.web.reactive.function.client.WebClient.RequestHeadersUriSpec;
23  
24  /**
25   * The http request method.
26   *
27   * @author Christian Bremer
28   */
29  public enum HttpRequestMethod {
30  
31    /**
32     * Get http request method.
33     */
34    GET,
35  
36    /**
37     * Head http request method.
38     */
39    HEAD,
40  
41    /**
42     * Post http request method.
43     */
44    POST,
45  
46    /**
47     * Put http request method.
48     */
49    PUT,
50  
51    /**
52     * Patch http request method.
53     */
54    PATCH,
55  
56    /**
57     * Delete http request method.
58     */
59    DELETE,
60  
61    /**
62     * Options http request method.
63     */
64    OPTIONS;
65  
66    /**
67     * Invoke request headers uri spec.
68     *
69     * @param webClient the web client
70     * @return the request headers uri spec
71     */
72    public RequestHeadersUriSpec<?> invoke(WebClient webClient) {
73      switch (this) {
74        case GET:
75          return webClient.get();
76        case HEAD:
77          return webClient.head();
78        case POST:
79          return webClient.post();
80        case PUT:
81          return webClient.put();
82        case PATCH:
83          return webClient.patch();
84        case DELETE:
85          return webClient.delete();
86        case OPTIONS:
87          return webClient.options();
88        default:
89          throw new IllegalStateException(String.format(
90              "There is no action defined for http method  %s",
91              this.name()));
92      }
93    }
94  
95    /**
96     * Resolve.
97     *
98     * @param method the method
99     * @return the optional
100    */
101   public static Optional<HttpRequestMethod> resolve(String method) {
102     return Arrays.stream(HttpRequestMethod.values())
103         .filter(httpRequestMethod -> httpRequestMethod.name().equalsIgnoreCase(method))
104         .findFirst();
105   }
106 }