Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

googleApi.native.js 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // @flow
  2. import { NativeModules } from 'react-native';
  3. let GoogleSignin;
  4. if (NativeModules.RNGoogleSignin) {
  5. GoogleSignin = require('react-native-google-signin').GoogleSignin;
  6. }
  7. import {
  8. API_URL_BROADCAST_STREAMS,
  9. API_URL_LIVE_BROADCASTS
  10. } from './constants';
  11. /**
  12. * Class to encapsulate Google API functionalities and provide a similar
  13. * interface to what WEB has. The methods are different, but the point is that
  14. * the export object is similar so no need for different export logic.
  15. *
  16. * For more detailed documentation of the {@code GoogleSignin} API, please visit
  17. * https://github.com/react-native-community/react-native-google-signin.
  18. */
  19. class GoogleApi {
  20. /**
  21. * Wraps the {@code GoogleSignin.configure} method.
  22. *
  23. * @param {Object} config - The config object to be passed to
  24. * {@code GoogleSignin.configure}.
  25. * @returns {void}
  26. */
  27. configure(config: Object) {
  28. if (GoogleSignin) {
  29. GoogleSignin.configure(config);
  30. }
  31. }
  32. /**
  33. * Retrieves the available YouTube streams the user can use for live
  34. * streaming.
  35. *
  36. * @param {string} accessToken - The Google auth token.
  37. * @returns {Promise}
  38. */
  39. getYouTubeLiveStreams(accessToken: string): Promise<*> {
  40. return new Promise((resolve, reject) => {
  41. // Fetching the list of available broadcasts first.
  42. this._fetchGoogleEndpoint(accessToken,
  43. API_URL_LIVE_BROADCASTS)
  44. .then(broadcasts => {
  45. // Then fetching all the available live streams that the
  46. // user has access to with the broadcasts we retreived
  47. // earlier.
  48. this._getLiveStreamsForBroadcasts(
  49. accessToken, broadcasts).then(resolve, reject);
  50. }, reject);
  51. });
  52. }
  53. /**
  54. * Wraps the {@code GoogleSignin.hasPlayServices} method.
  55. *
  56. * @returns {Promise<*>}
  57. */
  58. hasPlayServices() {
  59. if (!GoogleSignin) {
  60. return Promise.reject(new Error('Google SignIn not supported'));
  61. }
  62. return GoogleSignin.hasPlayServices();
  63. }
  64. /**
  65. * Wraps the {@code GoogleSignin.signIn} method.
  66. *
  67. * @returns {Promise<*>}
  68. */
  69. signIn() {
  70. return GoogleSignin.signIn();
  71. }
  72. /**
  73. * Wraps the {@code GoogleSignin.signInSilently} method.
  74. *
  75. * @returns {Promise<*>}
  76. */
  77. signInSilently() {
  78. return GoogleSignin.signInSilently();
  79. }
  80. /**
  81. * Wraps the {@code GoogleSignin.signOut} method.
  82. *
  83. * @returns {Promise<*>}
  84. */
  85. signOut() {
  86. return GoogleSignin.signOut();
  87. }
  88. /**
  89. * Helper method to fetch a Google API endpoint in a generic way.
  90. *
  91. * @private
  92. * @param {string} accessToken - The access token used for the API call.
  93. * @param {string} endpoint - The endpoint to fetch, including the URL
  94. * params if needed.
  95. * @returns {Promise}
  96. */
  97. _fetchGoogleEndpoint(accessToken, endpoint): Promise<*> {
  98. return new Promise((resolve, reject) => {
  99. const headers = {
  100. Authorization: `Bearer ${accessToken}`
  101. };
  102. fetch(endpoint, {
  103. headers
  104. }).then(response => response.json())
  105. .then(responseJSON => {
  106. if (responseJSON.error) {
  107. reject(responseJSON.error.message);
  108. } else {
  109. resolve(responseJSON.items || []);
  110. }
  111. }, reject);
  112. });
  113. }
  114. /**
  115. * Retrieves the available YouTube streams that are available for the
  116. * provided broadcast IDs.
  117. *
  118. * @private
  119. * @param {string} accessToken - The Google access token.
  120. * @param {Array<Object>} broadcasts - The list of broadcasts that we want
  121. * to retreive streams for.
  122. * @returns {Promise}
  123. */
  124. _getLiveStreamsForBroadcasts(accessToken, broadcasts): Promise<*> {
  125. return new Promise((resolve, reject) => {
  126. const ids = [];
  127. for (const broadcast of broadcasts) {
  128. broadcast.contentDetails
  129. && broadcast.contentDetails.boundStreamId
  130. && ids.push(broadcast.contentDetails.boundStreamId);
  131. }
  132. this._fetchGoogleEndpoint(
  133. accessToken,
  134. `${API_URL_BROADCAST_STREAMS}${ids.join(',')}`)
  135. .then(streams => {
  136. const keys = [];
  137. // We construct an array of keys bind with the broadcast
  138. // name for a nice display.
  139. for (const stream of streams) {
  140. const key = stream.cdn.ingestionInfo.streamName;
  141. let title;
  142. // Finding title from the broadcast with the same
  143. // boundStreamId. If not found (unknown scenario), we
  144. // use the key as title again.
  145. for (const broadcast of broadcasts) {
  146. if (broadcast.contentDetails
  147. && broadcast.contentDetails.boundStreamId
  148. === stream.id) {
  149. title = broadcast.snippet.title;
  150. }
  151. }
  152. keys.push({
  153. key,
  154. title: title || key
  155. });
  156. }
  157. resolve(keys);
  158. }, reject);
  159. });
  160. }
  161. }
  162. export default new GoogleApi();