p2p-storage-ipfs.ts 994 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import { HttpClient } from "@angular/common/http";
  2. import { Injectable } from "@angular/core";
  3. /*
  4. Generated class for the P2pStorageIpfsProvider provider.
  5. See https://angular.io/guide/dependency-injection for more info on providers
  6. and Angular DI.
  7. */
  8. @Injectable()
  9. export class P2pStorageIpfsProvider {
  10. private infuraUrl = "https://ipfs.infura.io:5001/api/v0/";
  11. constructor(public http: HttpClient) {}
  12. public storeTweet(tweet) {
  13. const formData = new FormData();
  14. formData.append("data", JSON.stringify(tweet));
  15. return this.http.post(this.infuraUrl + "add", formData).toPromise();
  16. }
  17. public fetchTweet(hash: string) {
  18. const options = {
  19. params: { arg: hash }
  20. };
  21. return this.http.get(this.infuraUrl + "cat", options).toPromise();
  22. }
  23. public async fetchTweets(hashs: string[]) {
  24. const tweetPromises = [];
  25. hashs.forEach(hash => {
  26. tweetPromises.push(this.fetchTweet(hash));
  27. });
  28. return await Promise.all(tweetPromises);
  29. }
  30. }