@@ -42,62 +42,60 @@ public class FeedbinAPI : Object {
4242 {
4343 m_session. user_agent = user_agent;
4444 }
45-
46- m_session. authenticate. connect(authenticate);
47- }
48-
49- ~FeedbinAPI ()
50- {
51- m_session. authenticate. disconnect(authenticate);
5245 }
5346
54- private void authenticate (Soup .Message msg , Soup .Auth auth , bool retrying )
55- {
56- if (! retrying)
57- {
58- auth. authenticate(this . username, this . password);
59- }
60- }
61-
62- private Soup .Message request (string method , string last_part , string ? input = null ) throws FeedbinError
47+ private Bytes request (string method , string last_part , string ? input = null ) throws FeedbinError
6348 requires (method == "DELETE " || method == "GET " || method == "POST ")
6449 requires (input == null || method != "GET ")
65- ensures (result.status_code >= 200)
66- ensures (result.status_code < 400)
6750 {
6851 var path = m_base_uri + last_part;
6952 var message = new Soup .Message (method, path);
7053
54+ message. authenticate. connect((auth, retrying) = > {
55+ if (! retrying)
56+ {
57+ auth. authenticate(this . username, this . password);
58+ return true ;
59+ }
60+ return false ;
61+ });
62+
7163 if (method == " POST" )
7264 {
7365 message. request_headers. append(" Content-Type" , " application/json; charset=utf-8" );
7466 }
7567
7668 if (input != null )
7769 {
78- message. request_body. append_take(input. data);
70+ message. set_request_body_from_bytes(" application/json; charset=utf-8" , new Bytes (input. data));
71+ }
72+
73+ Bytes response_body;
74+ try
75+ {
76+ response_body = m_session. send_and_read(message);
77+ }
78+ catch (GLib . Error e)
79+ {
80+ throw new FeedbinError .NO_CONNECTION (@" Connection to $m_base_uri failed: $(e.message)" );
7981 }
8082
81- m_session. send_and_read(message);
8283 var status = message. status_code;
83- if (status < 200 || status >= 400 )
84+ if (status < 200 || status >= 400 || status == 300 )
8485 {
8586 switch (status)
8687 {
87- case Soup . Status . CANT_RESOLVE:
88- case Soup . Status . CANT_RESOLVE_PROXY:
89- case Soup . Status . CANT_CONNECT:
90- case Soup . Status . CANT_CONNECT_PROXY:
91- throw new FeedbinError .NO_CONNECTION (@" Connection to $m_base_uri failed" );
9288 case Soup . Status . UNAUTHORIZED:
9389 throw new FeedbinError .NOT_AUTHORIZED (@" Not authorized to $method $path " );
9490 case Soup . Status . NOT_FOUND:
9591 throw new FeedbinError .NOT_FOUND (@" $method $path not found" );
92+ case Soup . Status . MULTIPLE_CHOICES:
93+ throw new FeedbinError .MULTIPLE_CHOICES (@" Multiple choices for $method $path " );
9694 }
9795 string phrase = Soup . Status . get_phrase(status);
9896 throw new FeedbinError .UNKNOWN_ERROR (@" Unexpected status $status ($phrase ) for $method $path " );
9997 }
100- return message ;
98+ return response_body ;
10199 }
102100
103101 // TODO: Move to DateUtils
@@ -119,30 +117,28 @@ public class FeedbinAPI : Object {
119117 return string_to_datetime(s);
120118 }
121119
122- private Soup . Message post_request (string path , string input ) throws FeedbinError
120+ private Bytes post_request (string path , string input ) throws FeedbinError
123121 requires (input != "")
124122 {
125123 return request(" POST" , path, input);
126124 }
127125
128- private Soup . Message delete_request (string path ) throws FeedbinError
126+ private void delete_request (string path ) throws FeedbinError
129127 {
130- return request(" DELETE" , path);
128+ request(" DELETE" , path);
131129 }
132130
133- private Soup . Message get_request (string path ) throws FeedbinError
131+ private Bytes get_request (string path ) throws FeedbinError
134132 {
135133 return request(" GET" , path);
136134 }
137135
138- private static Json .Node parse_json (Soup . Message response ) throws FeedbinError
136+ private static Json .Node parse_json (Bytes response_body , string path ) throws FeedbinError
139137 {
140- var method = response. method;
141- var uri = response. uri. to_string(false );
142- string content = (string )response. response_body. flatten(). data;
138+ string content = (string )response_body. get_data();
143139 if (content == null )
144140 {
145- throw new FeedbinError .INVALID_FORMAT (@" $method $uri returned no content but expected JSON" );
141+ throw new FeedbinError .INVALID_FORMAT (@" $path returned no content but expected JSON" );
146142 }
147143
148144 var parser = new Json .Parser ();
@@ -152,19 +148,19 @@ public class FeedbinAPI : Object {
152148 }
153149 catch (Error e)
154150 {
155- throw new FeedbinError .INVALID_FORMAT (@" $method $uri returned invalid JSON: " + e. message + " \n Content is: $content" );
151+ throw new FeedbinError .INVALID_FORMAT (@" $path returned invalid JSON: " + e. message + " \n Content is: $content" );
156152 }
157153 return parser. get_root();
158154 }
159155
160156 private Json .Node get_json (string path ) throws FeedbinError
161157 requires (path != "")
162158 {
163- var response = get_request(path);
164- return parse_json(response );
159+ var response_body = get_request(path);
160+ return parse_json(response_body, path );
165161 }
166162
167- private Soup . Message post_json_object (string path , Json .Object obj ) throws FeedbinError
163+ private Bytes post_json_object (string path , Json .Object obj ) throws FeedbinError
168164 {
169165 var root = new Json .Node (Json . NodeType . OBJECT );
170166 root. set_object(obj);
@@ -180,8 +176,8 @@ public class FeedbinAPI : Object {
180176 {
181177 try
182178 {
183- var res = get_request(" authentication.json" );
184- return res . status_code == Soup . Status . OK ;
179+ get_request(" authentication.json" );
180+ return true ;
185181 }
186182 catch (FeedbinError . NOT_AUTHORIZED e)
187183 {
@@ -240,13 +236,8 @@ public class FeedbinAPI : Object {
240236
241237 try
242238 {
243- var response = post_json_object(" subscriptions.json" , object );
244- if (response. status_code == 300 )
245- {
246- throw new FeedbinError .MULTIPLE_CHOICES (" Site $url has multiple feeds to subscribe to" );
247- }
248-
249- var root = parse_json(response);
239+ var response_body = post_json_object(" subscriptions.json" , object );
240+ var root = parse_json(response_body, " subscriptions.json" );
250241 return Subscription . from_json(root. get_object());
251242 }
252243 catch (FeedbinError . NOT_FOUND e)
0 commit comments