@@ -42,21 +42,15 @@ public class FeedbinAPI : Object {
42
42
{
43
43
m_session. user_agent = user_agent;
44
44
}
45
-
46
- m_session. authenticate. connect(authenticate);
47
- }
48
-
49
- ~FeedbinAPI ()
50
- {
51
- m_session. authenticate. disconnect(authenticate);
52
45
}
53
46
54
- private void authenticate (Soup .Message msg , Soup .Auth auth , bool retrying )
55
- {
56
- if (! retrying)
57
- {
58
- auth. authenticate(this . username, this . password);
47
+ private bool authenticate (Soup .Auth auth , bool retrying ) {
48
+ if (! retrying) {
49
+ auth. authenticate (this . username, this . password);
50
+ return true ;
59
51
}
52
+
53
+ return false ;
60
54
}
61
55
62
56
private Soup .Message request (string method , string last_part , string ? input = null ) throws FeedbinError
@@ -68,35 +62,34 @@ public class FeedbinAPI : Object {
68
62
var path = m_base_uri + last_part;
69
63
var message = new Soup .Message (method, path);
70
64
71
- if (method == " POST" )
72
- {
65
+ message. authenticate. connect (authenticate);
66
+
67
+ if (method == " POST" ) {
73
68
message. request_headers. append(" Content-Type" , " application/json; charset=utf-8" );
74
69
}
75
70
76
- if (input != null )
77
- {
78
- message. request_body. append_take(input. data);
71
+ if (input != null ) {
72
+ message. set_request_body_from_bytes (" application/x-www-form-urlencoded" , new Bytes (input. data));
79
73
}
80
74
81
- m_session. send_and_read(message);
82
- var status = message. status_code;
83
- if (status < 200 || status >= 400 )
84
- {
85
- switch (status)
86
- {
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" );
92
- case Soup . Status . UNAUTHORIZED:
93
- throw new FeedbinError .NOT_AUTHORIZED (@" Not authorized to $method $path " );
94
- case Soup . Status . NOT_FOUND:
95
- throw new FeedbinError .NOT_FOUND (@" $method $path not found" );
96
- }
97
- string phrase = Soup . Status . get_phrase(status);
98
- throw new FeedbinError .UNKNOWN_ERROR (@" Unexpected status $status ($phrase ) for $method $path " );
75
+ try {
76
+ m_session. send_and_read(message);
77
+ var status = message. status_code;
78
+
79
+ if (status < 200 || status >= 400 ) {
80
+ switch (status) {
81
+ case Soup . Status . UNAUTHORIZED:
82
+ throw new FeedbinError .NOT_AUTHORIZED (@" Not authorized to $method $path " );
83
+ case Soup . Status . NOT_FOUND:
84
+ throw new FeedbinError .NOT_FOUND (@" $method $path not found" );
85
+ }
86
+ string phrase = Soup . Status . get_phrase(status);
87
+ throw new FeedbinError .UNKNOWN_ERROR (@" Unexpected status $status ($phrase ) for $method $path " );
88
+ }
89
+ } catch (Error e) {
90
+ throw new FeedbinError .NO_CONNECTION (@" Connection to $m_base_uri failed" );
99
91
}
92
+
100
93
return message;
101
94
}
102
95
@@ -138,22 +131,21 @@ public class FeedbinAPI : Object {
138
131
private static Json .Node parse_json (Soup .Message response ) throws FeedbinError
139
132
{
140
133
var method = response. method;
141
- var uri = response. uri. to_string(false );
134
+ var uri = response. uri. to_string();
142
135
string content = (string )response. response_body. flatten(). data;
143
- if (content == null )
144
- {
136
+
137
+ if (content == null ) {
145
138
throw new FeedbinError .INVALID_FORMAT (@" $method $uri returned no content but expected JSON" );
146
139
}
147
140
148
141
var parser = new Json .Parser ();
149
- try
150
- {
142
+
143
+ try {
151
144
parser. load_from_data(content, - 1 );
152
- }
153
- catch (Error e)
154
- {
145
+ } catch (Error e) {
155
146
throw new FeedbinError .INVALID_FORMAT (@" $method $uri returned invalid JSON: " + e. message + " \n Content is: $content" );
156
147
}
148
+
157
149
return parser. get_root();
158
150
}
159
151
0 commit comments