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

/beintoosdk/mobile/android/src/com/beintoo/beintoosdk/BeintooUser.java

https://github.com/neojjang/titanium_modules-1
Java | 431 lines | 257 code | 75 blank | 99 comment | 40 complexity | 1b0ab1db7dd368b3a6f2f21cf10f9cd7 MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright 2011 Beintoo
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. ******************************************************************************/
  16. package com.beintoo.beintoosdk;
  17. import java.lang.reflect.Type;
  18. import java.net.URLEncoder;
  19. import java.util.List;
  20. import com.beintoo.beintoosdkutility.ApiCallException;
  21. import com.beintoo.beintoosdkutility.BeintooSdkParams;
  22. import com.beintoo.beintoosdkutility.HeaderParams;
  23. import com.beintoo.beintoosdkutility.PostParams;
  24. import com.beintoo.wrappers.Challenge;
  25. import com.beintoo.wrappers.Message;
  26. import com.beintoo.wrappers.User;
  27. import com.beintoo.wrappers.UserCredit;
  28. import com.google.beintoogson.Gson;
  29. import com.google.beintoogson.JsonParseException;
  30. import com.google.beintoogson.reflect.TypeToken;
  31. public class BeintooUser {
  32. String apiPreUrl = null;
  33. public BeintooUser() {
  34. if(!BeintooSdkParams.useSandbox)
  35. apiPreUrl = BeintooSdkParams.apiUrl;
  36. else
  37. apiPreUrl = BeintooSdkParams.sandboxUrl;
  38. }
  39. /**
  40. * Returns an array of users from the same deviceUDID
  41. * @param deviceUDID the requested device unique identifier
  42. * @return a json array of users
  43. */
  44. public User[] getUsersByDeviceUDID (String deviceUDID) {
  45. String apiUrl = apiPreUrl+"user/bydeviceUDID/"+deviceUDID;
  46. HeaderParams header = new HeaderParams();
  47. header.getKey().add("apikey");
  48. header.getValue().add(DeveloperConfiguration.apiKey);
  49. BeintooConnection conn = new BeintooConnection();
  50. String json = conn.httpRequest(apiUrl, header, null);
  51. Gson gson = new Gson();
  52. User[] users = gson.fromJson(json, User[].class);
  53. return users;
  54. }
  55. /**
  56. * Return a user by email and password
  57. * @param email user email
  58. * @param password user password
  59. * @return json object of the requested user
  60. */
  61. public User getUsersByEmail (String email, String password) {
  62. String apiUrl = apiPreUrl+"user/byemail";
  63. HeaderParams header = new HeaderParams();
  64. header.getKey().add("apikey");
  65. header.getValue().add(DeveloperConfiguration.apiKey);
  66. header.getKey().add("email");
  67. header.getValue().add(email);
  68. header.getKey().add("password");
  69. header.getValue().add(password);
  70. BeintooConnection conn = new BeintooConnection();
  71. Gson gson = new Gson();
  72. User user = null;
  73. try {
  74. String json = conn.httpRequest(apiUrl, header, null);
  75. user = gson.fromJson(json, User.class);
  76. } catch (final JsonParseException e) {
  77. return user;
  78. } catch (Exception e){
  79. return user;
  80. }
  81. return user;
  82. }
  83. /**
  84. * Return a user by his userExt
  85. * @param userExt the user unique id identifier
  86. * @param (optional) a string that represents the position in your code. We will use it to indentify different api calls of the same nature.
  87. * @return json object of the requested user
  88. */
  89. public User getUserByUserExt (String userExt, String codeID) {
  90. String apiUrl = apiPreUrl+"user/"+userExt;
  91. HeaderParams header = new HeaderParams();
  92. header.getKey().add("apikey");
  93. header.getValue().add(DeveloperConfiguration.apiKey);
  94. BeintooConnection conn = new BeintooConnection();
  95. Gson gson = new Gson();
  96. User user = null;
  97. try {
  98. String json = conn.httpRequest(apiUrl, header, null);
  99. user = gson.fromJson(json, User.class);
  100. } catch (final JsonParseException e) {
  101. return user;
  102. } catch (Exception e){
  103. return user;
  104. }
  105. return user;
  106. }
  107. /**
  108. * Returns challenges of the requested user
  109. * @param userExt usr_ext of the request user
  110. * @param status (path parameter) one of TO_BE_ACCEPTED, STARTED, ENDED.
  111. * @param codeID (optional) a string that represents the position in your code. We will use it to indentify different api calls of the same nature.
  112. * @return
  113. */
  114. public Challenge[] challengeShow (String userExt, String status, String codeID){
  115. String apiUrl = apiPreUrl+"user/challenge/show/"+userExt+"/"+status;
  116. HeaderParams header = new HeaderParams();
  117. header.getKey().add("apikey");
  118. header.getValue().add(DeveloperConfiguration.apiKey);
  119. if(codeID != null){
  120. header.getKey().add("codeID");
  121. header.getValue().add(codeID);
  122. }
  123. BeintooConnection conn = new BeintooConnection();
  124. String json = conn.httpRequest(apiUrl, header, null);
  125. return new Gson().fromJson(json, Challenge[].class);
  126. }
  127. public Challenge[] challengeShow (String userExt, String status){
  128. return challengeShow(userExt, status, null);
  129. }
  130. /**
  131. * Manage challenges, used for accept, invite and refuse challenges
  132. * @param userExtFrom (path parameter) the unique beintoo id of registered user
  133. * @param userExtTo (path parameter) the unique beintoo id of registered user
  134. * @param action action (path parameter) one of INVITE, ACCEPT, REFUSE
  135. * @param codeID (optional) a string that represents the position in your code. We will use it to indentify different api calls of the same nature.
  136. * @return a json object of request challenges
  137. * @throws Exception
  138. */
  139. public Challenge challenge (String userExtFrom, String userExtTo, String action, String codeID){
  140. String apiUrl = apiPreUrl+"user/challenge/"+userExtFrom+"/"+action+"/"+userExtTo;
  141. HeaderParams header = new HeaderParams();
  142. header.getKey().add("apikey");
  143. header.getValue().add(DeveloperConfiguration.apiKey);
  144. if(codeID != null){
  145. header.getKey().add("codeID");
  146. header.getValue().add(codeID);
  147. }
  148. BeintooConnection conn = new BeintooConnection();
  149. String json = conn.httpRequest(apiUrl, header, null);
  150. Gson gson = new Gson();
  151. Challenge challenge = gson.fromJson(json, Challenge.class);
  152. /*if (challenge.getStatus() == null){
  153. Message msg = gson.fromJson(json, Message.class);
  154. if(msg.getMessageID() == -15)
  155. throw new Exception("-15");
  156. }*/
  157. return challenge;
  158. }
  159. public Challenge challenge (String userExtFrom, String userExtTo, String action){
  160. return challenge (userExtFrom, userExtTo, action, null);
  161. }
  162. /**
  163. * Get friends of a given user
  164. *
  165. * @param userExt requested user
  166. * @param codeID (optional) a string that represents the position in your code. We will use it to indentify different api calls of the same nature
  167. * @return an array of Users
  168. */
  169. public User[] getUserFriends(String userExt, String codeID){
  170. String apiUrl = apiPreUrl+"user/friend/"+userExt;
  171. HeaderParams header = new HeaderParams();
  172. header.getKey().add("apikey");
  173. header.getValue().add(DeveloperConfiguration.apiKey);
  174. if(codeID != null){
  175. header.getKey().add("codeID");
  176. header.getValue().add(codeID);
  177. }
  178. BeintooConnection conn = new BeintooConnection();
  179. String json = conn.httpRequest(apiUrl, header, null);
  180. Gson gson = new Gson();
  181. User [] user = gson.fromJson(json, User[].class);
  182. return user;
  183. }
  184. public Message detachUserFromDevice (String deviceID,String userID){
  185. String apiUrl = apiPreUrl+"user/removeUDID/"+deviceID+"/"+userID;
  186. HeaderParams header = new HeaderParams();
  187. header.getKey().add("apikey");
  188. header.getValue().add(DeveloperConfiguration.apiKey);
  189. BeintooConnection conn = new BeintooConnection();
  190. String json = conn.httpRequest(apiUrl, header, null);
  191. Gson gson = new Gson();
  192. Message msg = gson.fromJson(json, Message.class);
  193. return msg;
  194. }
  195. public List<UserCredit> getUserBalance (String userExt, int start, int rows){
  196. String apiUrl = apiPreUrl+"user/balance/"+userExt+"/";
  197. if(start != -1 && rows != -1){
  198. String params = "?start="+start+"&rows="+rows;
  199. apiUrl = apiUrl + params;
  200. }
  201. HeaderParams header = new HeaderParams();
  202. header.getKey().add("apikey");
  203. header.getValue().add(DeveloperConfiguration.apiKey);
  204. BeintooConnection conn = new BeintooConnection();
  205. String json = conn.httpRequest(apiUrl, header, null);
  206. Gson gson = new Gson();
  207. Type type = new TypeToken<List<UserCredit>>() {}.getType();
  208. List<UserCredit> balance = gson.fromJson(json, type);
  209. return balance;
  210. }
  211. /**
  212. * Return pending friend requests
  213. *
  214. * @param userExt the unique beintoo id of registered user.
  215. * @param codeID (optional) a string that represents the position in your code. We will use it to indentify different api calls of the same nature.
  216. * @return a list of users
  217. */
  218. public User[] getUserFriendshipRequests(String userExt, String codeID){
  219. String apiUrl = apiPreUrl+"user/friendshiprequest/"+userExt+"/";
  220. HeaderParams header = new HeaderParams();
  221. header.getKey().add("apikey");
  222. header.getValue().add(DeveloperConfiguration.apiKey);
  223. if(codeID != null){
  224. header.getKey().add("codeID");
  225. header.getValue().add(codeID);
  226. }
  227. BeintooConnection conn = new BeintooConnection();
  228. String json = conn.httpRequest(apiUrl, header, null);
  229. Gson gson = new Gson();
  230. User[] friendship = gson.fromJson(json, User[].class);
  231. return friendship;
  232. }
  233. /**
  234. * Respond to a friendship request
  235. *
  236. * @param userExtFrom the unique beintoo id of the current registered user.
  237. * @param userExtTo the unique beintoo id of registered user to respond the request.
  238. * @param action accept, refuse, invite
  239. * @param codeID (optional) a string that represents the position in your code. We will use it to indentify different api calls of the same nature.
  240. * @return a message
  241. */
  242. public Message respondToFriendshipRequests(String userExtFrom, String userExtTo, String action, String codeID){
  243. String apiUrl = apiPreUrl+"user/friendshiprequest/"+userExtFrom+"/"+action+"/"+userExtTo;
  244. HeaderParams header = new HeaderParams();
  245. header.getKey().add("apikey");
  246. header.getValue().add(DeveloperConfiguration.apiKey);
  247. if(codeID != null){
  248. header.getKey().add("codeID");
  249. header.getValue().add(codeID);
  250. }
  251. BeintooConnection conn = new BeintooConnection();
  252. String json = conn.httpRequest(apiUrl, header, null);
  253. Gson gson = new Gson();
  254. Message msg = gson.fromJson(json, Message.class);
  255. return msg;
  256. }
  257. /**
  258. * Find a friend from nickname, name or email
  259. *
  260. * @param query Data to search
  261. * @param codeID (optional) a string that represents the position in your code. We will use it to indentify different api calls of the same nature.
  262. * @return a list of users
  263. */
  264. public User[] findFriendsByQuery(String query, String userExt, Boolean skipFriends, String codeID){
  265. @SuppressWarnings("deprecation")
  266. String apiUrl = apiPreUrl+"user/byquery/?query="+URLEncoder.encode(query) + ((userExt!=null) ? ("&userExt="+userExt+"&skipFriends="+skipFriends) : "");
  267. HeaderParams header = new HeaderParams();
  268. header.getKey().add("apikey");
  269. header.getValue().add(DeveloperConfiguration.apiKey);
  270. if(codeID != null){
  271. header.getKey().add("codeID");
  272. header.getValue().add(codeID);
  273. }
  274. BeintooConnection conn = new BeintooConnection();
  275. String json = conn.httpRequest(apiUrl, header, null);
  276. Gson gson = new Gson();
  277. User[] queryres = gson.fromJson(json, User[].class);
  278. return queryres;
  279. }
  280. /**
  281. * You can use this method to automatize the user-registration of one of your player, providing us some information.
  282. *
  283. * @param guid the current player guid
  284. * @param email mandatory, return error if a user with that email is already registered
  285. * @param nickname mandatory
  286. * @param password (optional), will be generated automatically if not provided
  287. * @param name mandatory
  288. * @param address (optional)
  289. * @param country (optional)
  290. * @param gender (optional), integer
  291. * @param sendGreetingsEmail (optional), default true, whether or not to send a greeting email to the new user
  292. * @param imageurl (optional), URL of userimage
  293. * @param codeID (optional) a string that represents the position in your code. We will use it to indentify different api calls of the same nature.
  294. * @return a user wrapper
  295. */
  296. public User setOrUpdateUser(String action, String guid, String userExt, String email, String nickname, String password, String name, String address, String country, Integer gender, Boolean sendGreetingsEmail, String imageurl, String codeID){
  297. String apiUrl;
  298. if(action.equals("set"))
  299. apiUrl = apiPreUrl+"user/set";
  300. else if(action.equals("update"))
  301. apiUrl = apiPreUrl+"user/update";
  302. else
  303. throw new ApiCallException();
  304. HeaderParams header = new HeaderParams();
  305. header.getKey().add("apikey");
  306. header.getValue().add(DeveloperConfiguration.apiKey);
  307. if(guid != null){
  308. header.getKey().add("guid");
  309. header.getValue().add(guid);
  310. }
  311. if(userExt != null){
  312. header.getKey().add("userExt");
  313. header.getValue().add(userExt);
  314. }
  315. if(codeID != null) {
  316. header.getKey().add("guid");
  317. header.getValue().add(guid);
  318. }
  319. PostParams post = new PostParams();
  320. post.getKey().add("email");
  321. post.getValue().add(email);
  322. post.getKey().add("nickname");
  323. post.getValue().add(nickname);
  324. if(password != null) {
  325. post.getKey().add("password");
  326. post.getValue().add(password);
  327. }
  328. if(name != null) {
  329. post.getKey().add("name");
  330. post.getValue().add(name);
  331. }
  332. if(address != null) {
  333. post.getKey().add("address");
  334. post.getValue().add(address);
  335. }
  336. if(country != null) {
  337. post.getKey().add("country");
  338. post.getValue().add(country);
  339. }
  340. if(gender != null) {
  341. post.getKey().add("gender");
  342. post.getValue().add(gender.toString());
  343. }
  344. if(sendGreetingsEmail != null) {
  345. post.getKey().add("sendGreetingsEmail");
  346. post.getValue().add(sendGreetingsEmail.toString());
  347. }
  348. if(imageurl != null) {
  349. post.getKey().add("imageurl");
  350. post.getValue().add(imageurl);
  351. }
  352. BeintooConnection conn = new BeintooConnection();
  353. String json = conn.httpRequest(apiUrl, header, post,true);
  354. return new Gson().fromJson(json, User.class);
  355. }
  356. }