選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

googleApi.native.js 5.6KB

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