Created
July 29, 2017 18:35
-
-
Save philpalmieri/72938bcd2c58f2c5a44b46e40cf02fc7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { Http } from '@angular/http'; | |
import 'rxjs/add/operator/map'; | |
import {environment} from "../../environments/environment"; | |
/** Encryption Stuff **/ | |
var CryptoJS = require("crypto-js"); | |
var hmacsha1 = require('hmacsha1'); | |
@Injectable() | |
export class ContentService{ | |
private baseUrl: string = ''; | |
private formUrl: string = ''; | |
constructor(private http:Http) { | |
this.baseUrl = environment.basePath; | |
this.formUrl = environment.formPath; | |
} | |
public get(url, fullPath = false) { | |
let fullURL: string = ''; | |
if(fullPath) { | |
fullURL = url; | |
} else { | |
fullURL = this.baseUrl + url; | |
} | |
return this.http.get(fullURL).map((resp) => resp.json()); | |
} | |
public gformPost(url:string, data) { | |
let fullURL: string = this.formUrl + url; | |
let d = new Date; | |
let expiration = 3600; // 1 hour, | |
let unixtime = d.getTime() / 1000 | 0; | |
let expires = unixtime + expiration; | |
let stringToSign = environment.gformApiKey + ":POST:" + url + ":" + expires; | |
let signature = this.gformSignature(stringToSign); | |
let completeUrl = fullURL+"?api_key="+environment.gformApiKey+"&signature="+signature+"&expires="+expires; | |
return this.http.post(completeUrl, JSON.stringify(data)) | |
.map(res => res.json()); | |
} | |
private gformSignature(toencrypt:string) { | |
let hash = CryptoJS.HmacSHA1(toencrypt, environment.gformPrivateKey); | |
let base64 = hash.toString(CryptoJS.enc.Base64); | |
return encodeURIComponent(base64); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment