vatavuk / verano-http Goto Github PK
View Code? Open in Web Editor NEWJava HTTP Client in OOP style
License: MIT License
Java HTTP Client in OOP style
License: MIT License
We need to normalize header key in Header
class
Let's make Kvp.Template
take Scalar<Kvp>
in ctor, instead of Kvp
, to achieve lazy loading.
Let's create it
Let's create ExpectedStatus
that will assert whether received status code matches the expected one.
Let's create OkHttpWire
that will wrap okHttp client
The code is:
public class AppTest {
@Test
public void httpPost() {
final Response response = new Response(
new ApiUrl().asString(),
new Post(
"/api/auth/login",
new JsonBody(
Json.createObjectBuilder().add(
"username",
"***"
).add(
"password",
"***"
).build()
)
)
);
System.out.println(new Body.Of(response).asString());
}
}
The result is:
java.lang.VerifyError: class org.cactoos.list.Mapped overrides final method toString.()Ljava/lang/String;
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:756)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
at hr.com.vgv.verano.http.HashDict.<init>(HashDict.java:59)
at hr.com.vgv.verano.http.HashDict.<init>(HashDict.java:51)
at hr.com.vgv.verano.http.DictInput$Envelope.<init>(DictInput.java:67)
at hr.com.vgv.verano.http.parts.RequestUri.<init>(RequestUri.java:47)
at hr.com.vgv.verano.http.wire.apache.ApacheWire.<init>(ApacheWire.java:109)
at hr.com.vgv.verano.http.wire.apache.ApacheWire.<init>(ApacheWire.java:80)
at hr.com.vgv.verano.http.response.Response.<init>(Response.java:81)
at ru.fusionsoft.AppTest.httpPost(AppTest.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
Note: I have in my pom.xml
<dependency>
<groupId>org.cactoos</groupId>
<artifactId>cactoos</artifactId>
<version>0.50</version>
</dependency>
You have in your pom.xml
<dependency>
<groupId>org.cactoos</groupId>
<artifactId>cactoos</artifactId>
<version>0.39</version>
</dependency>
Create QueryParam
Create a header that will guarantee its uniqueness in header map.
Implement SocketTimeout
apache config
Let's create inner classes in Method.class that contains information for Get/Post/Put/Delete method names
BasicAuth should take only credentials in its ctor, not the uri
Code coverage should not be below 80%
Lets add support for handling of async http calls via new AsyncResponse class
JsonBody
provides empty string instead of a value
For releases
Add more information about request (method, url...) to ExpectedStatus
exception message
Implement it
Add additional ctor to *Response
classes that takes only url
.
Create ApacheHeaders
that will transform apache Header
array to Iterable<Kvp>
create cookie header
Implement it
Add support for multipart form body
Let's override Request/Response toString
methods to have more comprehensive view when
an error occur.
Create FormParam
Let's enrich exception message provided by ObjectMapper
with "Cannot deserialize body %s"
Update jackson to 2.9.9
ApacheWire does not check if the response contains a body before fetching it.
ObjectMapper
should be injectable into DtoBody.Of
Let's add pqulice static analysis
Create HeadersOf as iterable if headers
Create it
Multiple Key-Value pairs with the same key are present in Dict.iterator
Add proxy config for apache
Let's create mock wire for testing client. Mock wire shouldn't run on http, it should operate on java memory structures.
Instead of
new ExpectedStatus("Cannot fetch from Google", 301)
use
new ExpectedStatus(301, new FailWith("CannotFetch from Google))
Note: Status code should always go before fail message
Create separate inner classes Of
for reading request params. For example: Body.Of(request)
Create HeadersOf
that will extract headers from dict
Implement it
Let's create PostRequest
, PutRequest
, DeleteRequest
Implement it
Lets add asList
method to Header
class that will return a list of header values.
Create accept header
Enable cookie fetching by key
Let's create our own Dict
interface that will act as Map<String, String>
but will be immutable and less verbose
Wire doesnt really need to extend Map interface
Let's add support for HTTP/2
Create ContentType header
JsonResponse.asJson()
should return JsonReader
instead of JsonObject
FormParams (plural) override all the other params being sent in the same request.
Implement ConnectionTimeout
apache config
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.