You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by gg...@apache.org on 2020/01/28 17:17:57 UTC

[httpcomponents-client] branch master updated: Now that ClassicHttpRequests is no longer an enum, I need to way to (#204)

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/httpcomponents-client.git


The following commit(s) were added to refs/heads/master by this push:
     new 776ecda  Now that ClassicHttpRequests is no longer an enum, I need to way to (#204)
776ecda is described below

commit 776ecda7e044514846754d91e76e3ae4d8405380
Author: Gary Gregory <ga...@users.noreply.github.com>
AuthorDate: Tue Jan 28 12:17:46 2020 -0500

    Now that ClassicHttpRequests is no longer an enum, I need to way to (#204)
    
    generically build requests from method names.
---
 .../http/classic/methods/ClassicHttpRequests.java  | 70 ++++++++++++++++++++++
 .../classic/methods/TestClassicHttpRequests.java   | 10 ++++
 2 files changed, 80 insertions(+)

diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/classic/methods/ClassicHttpRequests.java b/httpclient5/src/main/java/org/apache/hc/client5/http/classic/methods/ClassicHttpRequests.java
index 5f3a0a0..db6e7f8 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/classic/methods/ClassicHttpRequests.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/classic/methods/ClassicHttpRequests.java
@@ -28,6 +28,8 @@
 package org.apache.hc.client5.http.classic.methods;
 
 import java.net.URI;
+import java.util.Locale;
+import java.util.Objects;
 
 
 /**
@@ -40,6 +42,74 @@ import java.net.URI;
  */
 public final class ClassicHttpRequests {
 
+    private static String cleanMethod(final String method) {
+        return Objects.requireNonNull(method, "method").toUpperCase(Locale.ROOT);
+    }
+
+    /**
+     * Creates a new HttpUriRequest for the given {@code method} and {@code String} URI.
+     *
+     * @param method A method supported by this class.
+     * @param uri a non-null request string URI.
+     * @throws IllegalArgumentException if the method is not supported.
+     * @throws IllegalArgumentException if the string uri is null.
+     * @return A new HttpUriRequest.
+     */
+    public static HttpUriRequest create(final String method, final String uri) {
+        switch (cleanMethod(method)) {
+        case HttpDelete.METHOD_NAME:
+            return ClassicHttpRequests.delete(uri);
+        case HttpGet.METHOD_NAME:
+            return ClassicHttpRequests.get(uri);
+        case HttpHead.METHOD_NAME:
+            return ClassicHttpRequests.head(uri);
+        case HttpOptions.METHOD_NAME:
+            return ClassicHttpRequests.options(uri);
+        case HttpPatch.METHOD_NAME:
+            return ClassicHttpRequests.patch(uri);
+        case HttpPost.METHOD_NAME:
+            return ClassicHttpRequests.post(uri);
+        case HttpPut.METHOD_NAME:
+            return ClassicHttpRequests.put(uri);
+        case HttpTrace.METHOD_NAME:
+            return ClassicHttpRequests.trace(uri);
+        default:
+            throw new IllegalArgumentException(method);
+        }
+    }
+
+    /**
+     * Creates a new HttpUriRequest for the given {@code method} and {@code URI}.
+     *
+     * @param method A method supported by this class.
+     * @param uri a non-null request URI.
+     * @throws IllegalArgumentException if the method is not supported.
+     * @throws IllegalArgumentException if the uri is null.
+     * @return A new HttpUriRequest.
+     */
+    public static HttpUriRequest create(final String method, final URI uri) {
+        switch (cleanMethod(method)) {
+        case HttpDelete.METHOD_NAME:
+            return ClassicHttpRequests.delete(uri);
+        case HttpGet.METHOD_NAME:
+            return ClassicHttpRequests.get(uri);
+        case HttpHead.METHOD_NAME:
+            return ClassicHttpRequests.head(uri);
+        case HttpOptions.METHOD_NAME:
+            return ClassicHttpRequests.options(uri);
+        case HttpPatch.METHOD_NAME:
+            return ClassicHttpRequests.patch(uri);
+        case HttpPost.METHOD_NAME:
+            return ClassicHttpRequests.post(uri);
+        case HttpPut.METHOD_NAME:
+            return ClassicHttpRequests.put(uri);
+        case HttpTrace.METHOD_NAME:
+            return ClassicHttpRequests.trace(uri);
+        default:
+            throw new IllegalArgumentException(method);
+        }
+    }
+
     public static HttpUriRequest delete(final String uri) {
         return delete(URI.create(uri));
     }
diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/classic/methods/TestClassicHttpRequests.java b/httpclient5/src/test/java/org/apache/hc/client5/http/classic/methods/TestClassicHttpRequests.java
index 7996cdd..63998a6 100644
--- a/httpclient5/src/test/java/org/apache/hc/client5/http/classic/methods/TestClassicHttpRequests.java
+++ b/httpclient5/src/test/java/org/apache/hc/client5/http/classic/methods/TestClassicHttpRequests.java
@@ -71,6 +71,16 @@ public class TestClassicHttpRequests {
   }
 
   @Test
+  public void testCreateMethodUri() {
+      Assert.assertEquals(expectedClass, ClassicHttpRequests.create(methodName, URI_FIXTURE).getClass());
+  }
+
+  @Test
+  public void testCreateMethodUriString() {
+      Assert.assertEquals(expectedClass, ClassicHttpRequests.create(methodName, URI_STRING_FIXTURE).getClass());
+  }
+
+  @Test
   public void testCreateFromString() throws Exception {
       final Method httpMethod = ClassicHttpRequests.class.getMethod(methodName, String.class);
       Assert.assertEquals(expectedClass,