11package org .javawebstack .httpclient ;
22
3+ import org .javawebstack .graph .GraphElement ;
34import org .javawebstack .httpclient .interceptor .ResponseTransformer ;
45import org .javawebstack .querystring .QueryString ;
56
@@ -75,20 +76,34 @@ public HTTPRequest basicAuthorization(String username, String password){
7576 return authorization ("Basic" , Base64 .getEncoder ().encodeToString ((username +":" +password ).getBytes ()));
7677 }
7778
78- public HTTPRequest formBody ( Map < String , String > data ) {
79- return body (new QueryString ( data ) .toString ());
79+ public HTTPRequest formBodyString ( QueryString query ) {
80+ return body (query .toString ()). contentType ( "application/x-www-form-urlencoded" );
8081 }
8182
82- public HTTPRequest formBody (QueryString query ) {
83- return body (query .toString ());
83+ public HTTPRequest formBody (Object object ){
84+ if (object instanceof QueryString )
85+ return formBodyString ((QueryString ) object );
86+ if (object instanceof GraphElement )
87+ return formBodyElement ((GraphElement ) object );
88+ return formBodyElement (client .getGraphMapper ().toGraph (object ));
89+ }
90+
91+ public HTTPRequest formBodyElement (GraphElement element ){
92+ return body (element .toJsonString ()).contentType ("application/x-www-form-urlencoded" );
8493 }
8594
8695 public HTTPRequest bearer (String token ){
8796 return authorization ("Bearer" , token );
8897 }
8998
9099 public HTTPRequest jsonBody (Object object ){
91- return body (client .getGson ().toJson (object )).contentType ("application/json" );
100+ if (object instanceof GraphElement )
101+ return jsonBodyElement ((GraphElement ) object );
102+ return jsonBodyElement (client .getGraphMapper ().toGraph (object ));
103+ }
104+
105+ public HTTPRequest jsonBodyElement (GraphElement element ){
106+ return body (element .toJsonString ()).contentType ("application/json" );
92107 }
93108
94109 public int status (){
@@ -106,14 +121,23 @@ public String string(){
106121 return new String (bytes (), StandardCharsets .UTF_8 );
107122 }
108123
109- public <T > T json (Class <T > type ){
124+ public <T > T object (Class <T > type ){
110125 if (type == null )
111126 return null ;
112127 if (type .equals (byte [].class ))
113128 return (T ) responseBody ;
114129 if (type .equals (String .class ))
115130 return (T ) string ();
116- return client .getGson ().fromJson (string (), type );
131+ return client .getGraphMapper ().fromGraph (graph (), type );
132+ }
133+
134+ public GraphElement graph (){
135+ String contentType = header ("Content-Type" );
136+ switch (contentType ){
137+ case "application/x-www-form-urlencoded" :
138+ GraphElement .fromFormData (string ());
139+ }
140+ return GraphElement .fromJson (string ());
117141 }
118142
119143 public String header (String key ){
0 commit comments