Grasshopper Soccer API Client
TypeScript
let Api: GrassHopperApi
let counta: number = 0
$(document).ready(function () {
Api = new GrassHopperApi
$("#results").html($("#results1").html())
})
$("#get-student").click(function (e: JQueryMouseEventObject) {
let promise: JQueryXHR = Api.Get("/api/student/getstudent/50")
promise.done(function (data: Student): void {
$("#results").html(JSON.stringify(data, null, 2))
}).fail(function (data: Student): void {
alert("Fail")
})
})
$("#get-product").click(function (e: JQueryMouseEventObject) {
let promise: JQueryXHR = Api.Get("/api/product/getproduct/5")
promise.done(function (data: Product): void {
$("#results").html(JSON.stringify(data, null, 2))
}).fail(function (data: Product): void {
alert("Fail")
})
})
$("#get-countries").click(function (e: JQueryMouseEventObject) {
let promise: JQueryXHR = Api.Get("/api/country/getcountries")
promise.done(function (data: Country): void {
$("#results").html(JSON.stringify(data, null, 2))
}).fail(function (data: Country): void {
alert("Fail")
})
})
$("#get-states").click(function (e: JQueryMouseEventObject) {
let promise: JQueryXHR = Api.Get("api/state/getstates")
promise.done(function (data: State): void {
$("#results").html(JSON.stringify(data, null, 4))
}).fail(function (data: State): void {
alert("Fail")
})
})
$("#toggle-js").click(function () {
let r: string = $("#results1").html()
let r2: string = $("#results2").html()
if (counta % 2 === 0) {
$("#results").html(r2)
} else {
$("#results").html(r)
}
counta++
})
class GrassHopperApi {
loginData: LoginData
headers: Headers
base: string
constructor() {
this.base = "https://apitest.grasshoppersoccer.com.au/"
this.loginData = new LoginData
$.ajax({
type: "POST",
url: this.base + "token",
data: this.loginData
}).done(function (data) {
sessionStorage.setItem("tokenKey", data.access_token)
}).fail(function (data) {
alert("token error")
})
}
AuthHeaders = (): AuthHeader => {
let headers: AuthHeader = new AuthHeader
headers.Authorization = "Bearer " + sessionStorage.getItem("tokenKey")
return headers
}
Get = (url: string): any => {
return $.ajax({
type: "GET",
url: this.base + url,
headers: this.AuthHeaders()
})
}
Post = (url: string, data: any): any => {
return $.ajax({
type: "POST",
url: this.base + url,
data: data,
headers: this.AuthHeaders()
})
}
Put = (url: string, data: any): any => {
return $.ajax({
type: "PUT",
url: this.base + url,
data: data,
headers: this.AuthHeaders()
})
}
Delete = (url: string, data: any): any => {
return $.ajax({
type: "DELETE",
url: this.base + url,
data: data,
headers: this.AuthHeaders()
})
}
}
class LoginData {
grant_type: string = "password"
username: string = "xxxx"
password: string = "xxxx"
}
class AuthHeader {
Authorization: string
}
class Student {
FullName: string
AgeOf: number
StudentId: number
FirstName: string
LastName: string
Suburb: string
StateId: number
Postcode: string
Address: string
PhoneNumber: string
MobileNumber: string
Birthday: Date
Email: string
EmergencyName: string
EmergencyPhoneNumber: string
Comment: string
Newsletter: boolean
FranchiseeId: number
Female: boolean
TempNotes: string
DismissBirthdayWarning: string
Deleted: boolean
}
class State {
Name: String
StateId: number
Country: Country
}
class Country {
Name: String
CountryId: number
States: Array
}
class Product {
Name: String
ProductId: number
ImageId: number
}
JavaScript
var Api;
var counta = 0;
$(document).ready(function () {
Api = new GrassHopperApi;
$("#results").html($("#results1").html());
});
$("#get-student").click(function (e) {
var promise = Api.Get("/api/student/getstudent/50");
promise.done(function (data) {
$("#results").html(JSON.stringify(data, null, 2));
}).fail(function (data) {
alert("Fail");
});
});
$("#get-product").click(function (e) {
var promise = Api.Get("/api/product/getproduct/5");
promise.done(function (data) {
$("#results").html(JSON.stringify(data, null, 2));
}).fail(function (data) {
alert("Fail");
});
});
$("#get-countries").click(function (e) {
var promise = Api.Get("/api/country/getcountries");
promise.done(function (data) {
$("#results").html(JSON.stringify(data, null, 2));
}).fail(function (data) {
alert("Fail");
});
});
$("#get-states").click(function (e) {
var promise = Api.Get("api/state/getstates");
promise.done(function (data) {
$("#results").html(JSON.stringify(data, null, 4));
}).fail(function (data) {
alert("Fail");
});
});
$("#toggle-js").click(function () {
var r = $("#results1").html();
var r2 = $("#results2").html();
if (counta % 2 === 0) {
$("#results").html(r2);
}
else {
$("#results").html(r);
}
counta++;
});
var GrassHopperApi = class (function () {
function GrassHopperApi() {
var _this = this;
this.AuthHeaders = function () {
var headers = new AuthHeader;
headers.Authorization = "Bearer " + sessionStorage.getItem("tokenKey");
return headers;
};
this.Get = function (url) {
return $.ajax({
type: "GET",
url: _this.base + url,
headers: _this.AuthHeaders()
});
};
this.Post = function (url, data) {
return $.ajax({
type: "POST",
url: _this.base + url,
data: data,
headers: _this.AuthHeaders()
});
};
this.Put = function (url, data) {
return $.ajax({
type: "PUT",
url: _this.base + url,
data: data,
headers: _this.AuthHeaders()
});
};
this.Delete = function (url, data) {
return $.ajax({
type: "DELETE",
url: _this.base + url,
data: data,
headers: _this.AuthHeaders()
});
};
this.base = "https://apitest.grasshoppersoccer.com.au/";
this.loginData = new LoginData;
$.ajax({
type: "POST",
url: this.base + "token",
data: this.loginData
}).done(function (data) {
sessionStorage.setItem("tokenKey", data.access_token);
}).fail(function (data) {
alert("token error");
});
}
return GrassHopperApi;
}());
var LoginData = class (function () {
function LoginData() {
this.grant_type = "password";
this.username = "xxxx";
this.password = "xxxx";
}
return LoginData;
}());
var AuthHeader = class (function () {
function AuthHeader() {
}
return AuthHeader;
}());
var Student = class (function () {
function Student() {
}
return Student;
}());
var State = class (function () {
function State() {
}
return State;
}());
var Country = class (function () {
function Country() {
}
return Country;
}());
var Product = class (function () {
function Product() {
}
return Product;
}());