PageRenderTime 52ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/twilio/rest/api/v2010/account/CallReader.java

http://github.com/twilio/twilio-java
Java | 379 lines | 177 code | 35 blank | 167 comment | 32 complexity | 33001c594782ff9970bd3eda8191db66 MD5 | raw file
Possible License(s): MIT
  1. /**
  2. * This code was generated by
  3. * \ / _ _ _| _ _
  4. * | (_)\/(_)(_|\/| |(/_ v1.0.0
  5. * / /
  6. */
  7. package com.twilio.rest.api.v2010.account;
  8. import com.twilio.base.Page;
  9. import com.twilio.base.Reader;
  10. import com.twilio.base.ResourceSet;
  11. import com.twilio.converter.DateConverter;
  12. import com.twilio.converter.Promoter;
  13. import com.twilio.exception.ApiConnectionException;
  14. import com.twilio.exception.ApiException;
  15. import com.twilio.exception.RestException;
  16. import com.twilio.http.HttpMethod;
  17. import com.twilio.http.Request;
  18. import com.twilio.http.Response;
  19. import com.twilio.http.TwilioRestClient;
  20. import com.twilio.rest.Domains;
  21. import java.time.ZonedDateTime;
  22. import java.time.format.DateTimeFormatter;
  23. public class CallReader extends Reader<Call> {
  24. private String pathAccountSid;
  25. private com.twilio.type.PhoneNumber to;
  26. private com.twilio.type.PhoneNumber from;
  27. private String parentCallSid;
  28. private Call.Status status;
  29. private ZonedDateTime startTime;
  30. private ZonedDateTime startTimeBefore;
  31. private ZonedDateTime startTimeAfter;
  32. private ZonedDateTime endTime;
  33. private ZonedDateTime endTimeBefore;
  34. private ZonedDateTime endTimeAfter;
  35. /**
  36. * Construct a new CallReader.
  37. */
  38. public CallReader() {
  39. }
  40. /**
  41. * Construct a new CallReader.
  42. *
  43. * @param pathAccountSid The SID of the Account that created the resource(s) to
  44. * read
  45. */
  46. public CallReader(final String pathAccountSid) {
  47. this.pathAccountSid = pathAccountSid;
  48. }
  49. /**
  50. * Only show calls made to this phone number, SIP address, Client identifier or
  51. * SIM SID..
  52. *
  53. * @param to Phone number or Client identifier of calls to include
  54. * @return this
  55. */
  56. public CallReader setTo(final com.twilio.type.PhoneNumber to) {
  57. this.to = to;
  58. return this;
  59. }
  60. /**
  61. * Only show calls made to this phone number, SIP address, Client identifier or
  62. * SIM SID..
  63. *
  64. * @param to Phone number or Client identifier of calls to include
  65. * @return this
  66. */
  67. public CallReader setTo(final String to) {
  68. return setTo(Promoter.phoneNumberFromString(to));
  69. }
  70. /**
  71. * Only include calls from this phone number, SIP address, Client identifier or
  72. * SIM SID..
  73. *
  74. * @param from Phone number or Client identifier to filter `from` on
  75. * @return this
  76. */
  77. public CallReader setFrom(final com.twilio.type.PhoneNumber from) {
  78. this.from = from;
  79. return this;
  80. }
  81. /**
  82. * Only include calls from this phone number, SIP address, Client identifier or
  83. * SIM SID..
  84. *
  85. * @param from Phone number or Client identifier to filter `from` on
  86. * @return this
  87. */
  88. public CallReader setFrom(final String from) {
  89. return setFrom(Promoter.phoneNumberFromString(from));
  90. }
  91. /**
  92. * Only include calls spawned by calls with this SID..
  93. *
  94. * @param parentCallSid Parent call SID to filter on
  95. * @return this
  96. */
  97. public CallReader setParentCallSid(final String parentCallSid) {
  98. this.parentCallSid = parentCallSid;
  99. return this;
  100. }
  101. /**
  102. * The status of the calls to include. Can be: `queued`, `ringing`,
  103. * `in-progress`, `canceled`, `completed`, `failed`, `busy`, or `no-answer`..
  104. *
  105. * @param status The status of the resources to read
  106. * @return this
  107. */
  108. public CallReader setStatus(final Call.Status status) {
  109. this.status = status;
  110. return this;
  111. }
  112. /**
  113. * Only include calls that started on this date. Specify a date as `YYYY-MM-DD`
  114. * in GMT, for example: `2009-07-06`, to read only calls that started on this
  115. * date. You can also specify an inequality, such as `StartTime&lt;=YYYY-MM-DD`,
  116. * to read calls that started on or before midnight of this date, and
  117. * `StartTime&gt;=YYYY-MM-DD` to read calls that started on or after midnight of
  118. * this date..
  119. *
  120. * @param startTime Only include calls that started on this date
  121. * @return this
  122. */
  123. public CallReader setStartTime(final ZonedDateTime startTime) {
  124. this.startTimeBefore = null;
  125. this.startTimeAfter = null;
  126. this.startTime = startTime;
  127. return this;
  128. }
  129. /**
  130. * Only include calls that started on this date. Specify a date as `YYYY-MM-DD`
  131. * in GMT, for example: `2009-07-06`, to read only calls that started on this
  132. * date. You can also specify an inequality, such as `StartTime&lt;=YYYY-MM-DD`,
  133. * to read calls that started on or before midnight of this date, and
  134. * `StartTime&gt;=YYYY-MM-DD` to read calls that started on or after midnight of
  135. * this date..
  136. *
  137. * @param startTimeBefore Only include calls that started on this date
  138. * @return this
  139. */
  140. public CallReader setStartTimeBefore(final ZonedDateTime startTimeBefore) {
  141. this.startTime = null;
  142. this.startTimeBefore = startTimeBefore;
  143. return this;
  144. }
  145. /**
  146. * Only include calls that started on this date. Specify a date as `YYYY-MM-DD`
  147. * in GMT, for example: `2009-07-06`, to read only calls that started on this
  148. * date. You can also specify an inequality, such as `StartTime&lt;=YYYY-MM-DD`,
  149. * to read calls that started on or before midnight of this date, and
  150. * `StartTime&gt;=YYYY-MM-DD` to read calls that started on or after midnight of
  151. * this date..
  152. *
  153. * @param startTimeAfter Only include calls that started on this date
  154. * @return this
  155. */
  156. public CallReader setStartTimeAfter(final ZonedDateTime startTimeAfter) {
  157. this.startTime = null;
  158. this.startTimeAfter = startTimeAfter;
  159. return this;
  160. }
  161. /**
  162. * Only include calls that ended on this date. Specify a date as `YYYY-MM-DD` in
  163. * GMT, for example: `2009-07-06`, to read only calls that ended on this date.
  164. * You can also specify an inequality, such as `EndTime&lt;=YYYY-MM-DD`, to read
  165. * calls that ended on or before midnight of this date, and
  166. * `EndTime&gt;=YYYY-MM-DD` to read calls that ended on or after midnight of
  167. * this date..
  168. *
  169. * @param endTime Only include calls that ended on this date
  170. * @return this
  171. */
  172. public CallReader setEndTime(final ZonedDateTime endTime) {
  173. this.endTimeBefore = null;
  174. this.endTimeAfter = null;
  175. this.endTime = endTime;
  176. return this;
  177. }
  178. /**
  179. * Only include calls that ended on this date. Specify a date as `YYYY-MM-DD` in
  180. * GMT, for example: `2009-07-06`, to read only calls that ended on this date.
  181. * You can also specify an inequality, such as `EndTime&lt;=YYYY-MM-DD`, to read
  182. * calls that ended on or before midnight of this date, and
  183. * `EndTime&gt;=YYYY-MM-DD` to read calls that ended on or after midnight of
  184. * this date..
  185. *
  186. * @param endTimeBefore Only include calls that ended on this date
  187. * @return this
  188. */
  189. public CallReader setEndTimeBefore(final ZonedDateTime endTimeBefore) {
  190. this.endTime = null;
  191. this.endTimeBefore = endTimeBefore;
  192. return this;
  193. }
  194. /**
  195. * Only include calls that ended on this date. Specify a date as `YYYY-MM-DD` in
  196. * GMT, for example: `2009-07-06`, to read only calls that ended on this date.
  197. * You can also specify an inequality, such as `EndTime&lt;=YYYY-MM-DD`, to read
  198. * calls that ended on or before midnight of this date, and
  199. * `EndTime&gt;=YYYY-MM-DD` to read calls that ended on or after midnight of
  200. * this date..
  201. *
  202. * @param endTimeAfter Only include calls that ended on this date
  203. * @return this
  204. */
  205. public CallReader setEndTimeAfter(final ZonedDateTime endTimeAfter) {
  206. this.endTime = null;
  207. this.endTimeAfter = endTimeAfter;
  208. return this;
  209. }
  210. /**
  211. * Make the request to the Twilio API to perform the read.
  212. *
  213. * @param client TwilioRestClient with which to make the request
  214. * @return Call ResourceSet
  215. */
  216. @Override
  217. public ResourceSet<Call> read(final TwilioRestClient client) {
  218. return new ResourceSet<>(this, client, firstPage(client));
  219. }
  220. /**
  221. * Make the request to the Twilio API to perform the read.
  222. *
  223. * @param client TwilioRestClient with which to make the request
  224. * @return Call ResourceSet
  225. */
  226. @Override
  227. @SuppressWarnings("checkstyle:linelength")
  228. public Page<Call> firstPage(final TwilioRestClient client) {
  229. this.pathAccountSid = this.pathAccountSid == null ? client.getAccountSid() : this.pathAccountSid;
  230. Request request = new Request(
  231. HttpMethod.GET,
  232. Domains.API.toString(),
  233. "/2010-04-01/Accounts/" + this.pathAccountSid + "/Calls.json"
  234. );
  235. addQueryParams(request);
  236. return pageForRequest(client, request);
  237. }
  238. /**
  239. * Retrieve the target page from the Twilio API.
  240. *
  241. * @param targetUrl API-generated URL for the requested results page
  242. * @param client TwilioRestClient with which to make the request
  243. * @return Call ResourceSet
  244. */
  245. @Override
  246. @SuppressWarnings("checkstyle:linelength")
  247. public Page<Call> getPage(final String targetUrl, final TwilioRestClient client) {
  248. this.pathAccountSid = this.pathAccountSid == null ? client.getAccountSid() : this.pathAccountSid;
  249. Request request = new Request(
  250. HttpMethod.GET,
  251. targetUrl
  252. );
  253. return pageForRequest(client, request);
  254. }
  255. /**
  256. * Retrieve the next page from the Twilio API.
  257. *
  258. * @param page current page
  259. * @param client TwilioRestClient with which to make the request
  260. * @return Next Page
  261. */
  262. @Override
  263. public Page<Call> nextPage(final Page<Call> page,
  264. final TwilioRestClient client) {
  265. Request request = new Request(
  266. HttpMethod.GET,
  267. page.getNextPageUrl(Domains.API.toString())
  268. );
  269. return pageForRequest(client, request);
  270. }
  271. /**
  272. * Retrieve the previous page from the Twilio API.
  273. *
  274. * @param page current page
  275. * @param client TwilioRestClient with which to make the request
  276. * @return Previous Page
  277. */
  278. @Override
  279. public Page<Call> previousPage(final Page<Call> page,
  280. final TwilioRestClient client) {
  281. Request request = new Request(
  282. HttpMethod.GET,
  283. page.getPreviousPageUrl(Domains.API.toString())
  284. );
  285. return pageForRequest(client, request);
  286. }
  287. /**
  288. * Generate a Page of Call Resources for a given request.
  289. *
  290. * @param client TwilioRestClient with which to make the request
  291. * @param request Request to generate a page for
  292. * @return Page for the Request
  293. */
  294. private Page<Call> pageForRequest(final TwilioRestClient client, final Request request) {
  295. Response response = client.request(request);
  296. if (response == null) {
  297. throw new ApiConnectionException("Call read failed: Unable to connect to server");
  298. } else if (!TwilioRestClient.SUCCESS.test(response.getStatusCode())) {
  299. RestException restException = RestException.fromJson(response.getStream(), client.getObjectMapper());
  300. if (restException == null) {
  301. throw new ApiException("Server Error, no content");
  302. }
  303. throw new ApiException(restException);
  304. }
  305. return Page.fromJson(
  306. "calls",
  307. response.getContent(),
  308. Call.class,
  309. client.getObjectMapper()
  310. );
  311. }
  312. /**
  313. * Add the requested query string arguments to the Request.
  314. *
  315. * @param request Request to add query string arguments to
  316. */
  317. private void addQueryParams(final Request request) {
  318. if (to != null) {
  319. request.addQueryParam("To", to.toString());
  320. }
  321. if (from != null) {
  322. request.addQueryParam("From", from.toString());
  323. }
  324. if (parentCallSid != null) {
  325. request.addQueryParam("ParentCallSid", parentCallSid);
  326. }
  327. if (status != null) {
  328. request.addQueryParam("Status", status.toString());
  329. }
  330. if (startTime != null) {
  331. request.addQueryParam("StartTime", startTime.format(DateTimeFormatter.ofPattern(Request.QUERY_STRING_DATE_TIME_FORMAT)));
  332. } else if (startTimeAfter != null || startTimeBefore != null) {
  333. request.addQueryDateTimeRange("StartTime", startTimeAfter, startTimeBefore);
  334. }
  335. if (endTime != null) {
  336. request.addQueryParam("EndTime", endTime.format(DateTimeFormatter.ofPattern(Request.QUERY_STRING_DATE_TIME_FORMAT)));
  337. } else if (endTimeAfter != null || endTimeBefore != null) {
  338. request.addQueryDateTimeRange("EndTime", endTimeAfter, endTimeBefore);
  339. }
  340. if (getPageSize() != null) {
  341. request.addQueryParam("PageSize", Integer.toString(getPageSize()));
  342. }
  343. }
  344. }