Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Names = require("../../names");
- import HttpiService = require("./HttpiService");
- import IHttpService = angular.IHttpService;
- import IHttpPromise = angular.IHttpPromise;
- export interface IInstagramService {
- getPhotosFromLocation(locationId): IHttpPromise<any>;
- getLocationByCoordinates(long, lat): IHttpPromise<any>;
- login();
- }
- export class InstagramService implements IInstagramService{
- public isLoggedIn = false;
- public loggedInUser = null;
- private accessToken = null;
- private clientId = "3300cb45b8a243ddafd1b09d693df0fd";
- private redirectUri = "http://latishka.com/";
- // @ngInject
- constructor(private $httpi: HttpiService,
- private $q: ng.IQService) {
- }
- private parseRedirectUri(url){
- return _.chain( url.slice(this.redirectUri.length).split('&') )
- // Split each array item into [key, value]
- // ignore empty string if search is empty
- .map(function(item) { if (item) return item.split('='); })
- // Remove undefined in the case the search is empty
- .compact()
- // Turn [key, value] arrays into object parameters
- .object()
- // Return the value of the chain operation
- .value();
- }
- public login() {
- var deferred = this.$q.defer();
- var config = {
- method: "get",
- url: "https://api.instagram.com/oauth/authorize/?client_id=:clientId&redirect_uri=:redirectUri&response_type=token",
- data: {
- clientId: this.clientId,
- redirectUri: "http://latishka.com/"
- }
- };
- this.$httpi.httpProxy(config).then((response: any) => {
- debugger;
- var params = this.parseRedirectUri(response);
- if(params["access_token"]){
- this.accessToken = params["access_token"];
- }
- deferred.resolve(this.accessToken);
- }).catch((rejection) => {
- deferred.reject(rejection);
- });
- return deferred.promise;
- }
- public getLocationByCoordinates(lng, lat): IHttpPromise<any> {
- var deferred = this.$q.defer();
- var config = {
- method: "get",
- url: "https://api.instagram.com/v1/locations/search?lat=:latitude&lng=:longtitude",
- data: {
- latitude: lat,
- longtitude: lng
- }
- };
- this.request(config).then((response) => {
- if(response && response.data){
- deferred.resolve(response.data);
- }
- });
- return deferred.promise;
- }
- public getPhotosFromLocation(locationId): IHttpPromise<any> {
- var deferred = this.$q.defer();
- var config = {
- method: "get",
- url: "https://api.instagram.com/v1/locations/:locationId/media/recent",
- data: {
- locationId: locationId,
- }
- };
- this.request(config).then((response)=>{
- deferred.resolve(response);
- }).catch((rejection) => {
- deferred.reject(rejection);
- });
- return deferred.promise;
- }
- private request(config, jsonPCallback?): IHttpPromise<any> {
- var deferred = this.$q.defer();
- if(!this.isLoggedIn) {
- var that = this;
- this.login().then(()=> {
- if(config.method.toLocaleLowerCase() === "get"){
- config.url += "&access_token=" + that.accessToken;
- } else {
- config.data["access_token"] = that.accessToken;
- }
- if(jsonPCallback) {
- config.url += "&callback=jsonPCallback";
- }
- that.$httpi.httpProxy(config).then((response: any) => {
- deferred.resolve(response);
- }).catch((rejection) => {
- deferred.reject(rejection);
- });
- }).catch((rejection) => {
- deferred.reject(rejection);
- });
- } else {
- if(config.method.toLocaleLowerCase() === "get"){
- config.url += "&access_token=" + that.accessToken;
- } else {
- config.data["access_token"] = that.accessToken;
- }
- if(jsonPCallback) {
- config.url += "&callback=jsonPCallback";
- }
- this.$httpi.httpProxy(config).then((response: any) => {
- deferred.resolve(response);
- }).catch((rejection) => {
- deferred.reject(rejection);
- });
- }
- return deferred.promise;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement