Skip to content

Commit

Permalink
#35 added basic rest service
Browse files Browse the repository at this point in the history
  • Loading branch information
artifactdev committed Mar 4, 2022
1 parent 54ae93e commit ed47936
Show file tree
Hide file tree
Showing 2 changed files with 229 additions and 0 deletions.
16 changes: 16 additions & 0 deletions devday/src/app/services/basic-rest.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';

import { BasicRestService } from './basic-rest.service';

describe('BasicRestService', () => {
let service: BasicRestService;

beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(BasicRestService);
});

it('should be created', () => {
expect(service).toBeTruthy();
});
});
213 changes: 213 additions & 0 deletions devday/src/app/services/basic-rest.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
import {
HttpClient,
HttpParams,
HttpHeaders,
HttpResponse
} from '@angular/common/http';
import { Injectable, isDevMode } from '@angular/core';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';

@Injectable({
providedIn: 'root'
})
export class BasicRestService {

constructor(
private httpClient: HttpClient
) {
}

/**
* Does a basic HTTP-GET request against a given path.
* @param path the path the request is fired to
* Returns Observable which can be subscribed to
*/
public get<T>(path: string, options?: {
headers?: HttpHeaders | {
[header: string]: string | string[];
};
observe?: 'body';
params?: HttpParams | {
[param: string]: string | string[];
};
reportProgress?: boolean;
responseType?: 'json';
withCredentials?: boolean;
}): Observable<T> {
return this.httpClient.get<T>(path, options).pipe(
tap(
() => {
if (isDevMode()) {
console.log('⬇ GET success ' + path);
}

}
)
);
}

/**
* Does a basic HTTP-GET request against a given path.
* @param path the path the request is fired to
* Returns Observable which can be subscribed to
*/
public getRaw(path: string, options?: {
headers?: HttpHeaders | {
[header: string]: string | string[];
};
observe?: 'body';
params?: HttpParams | {
[param: string]: string | string[];
};
reportProgress?: boolean;
responseType: 'text';
withCredentials?: boolean;
}): Observable<string> {
return this.httpClient.get(path, {responseType: 'text'}).pipe(
tap(
() => {
if (isDevMode()) {
console.log('GET success ' + path);
}

}
)
);
}

getBlob(url: string, options: {
headers?: HttpHeaders | {
[header: string]: string | string[];
};
observe: 'response';
params?: HttpParams | {
[param: string]: string | string[];
};
reportProgress?: boolean;
responseType: 'blob';
withCredentials?: boolean;
}): Observable<HttpResponse<Blob>> {
return this.httpClient.get(url, options)
}

/**
* Constructs a `GET` request that interprets the body as a JSON object and
* returns the full `HTTPResponse`.
*
* @param url The endpoint URL.
* @param options The HTTP options to send with the request.
*
* @return An `Observable` of the full `HTTPResponse` for the request,
* with a response body in the requested type.
*/
getWithHeaders<T>(path: string, options: {
headers?: HttpHeaders | {
[header: string]: string | string[];
};
observe: 'response';
params?: HttpParams | {
[param: string]: string | string[];
};
reportProgress?: boolean;
responseType?: 'json';
withCredentials?: boolean;
}): Observable<HttpResponse<T>> {
return this.httpClient.get<T>(path, options).pipe(
tap(
() => {
if (isDevMode()) {
console.log('GET success ' + path);
}

}
)
);
}

/**
* Sends given data with a HTTP-POST request to a given path
* @param path the path the request is fired to
* @param data the post data which is send to the given path
* Returns Observable which can be subscribed to
*/
public post<T>(path: string, data: any, options?: {
headers?: HttpHeaders | {
[header: string]: string | string[];
};
observe?: 'body';
params?: HttpParams | {
[param: string]: string | string[];
};
reportProgress?: boolean;
responseType?: 'json';
withCredentials?: boolean;
}): Observable<T> {
return this.httpClient.post<T>(path, data, options).pipe(
tap(
() => {
if (isDevMode()) {
console.log('POST success' + path);
}
}
)
);
}

/**
* Sends given data with a HTTP-PUT request to a given path.
* @param path the path the request is fired to
* @param data the put data which is send to the given path
* Returns Observable which can be subscribed to
*/
public put<T>(path: string, data: any, options?: {
headers?: HttpHeaders | {
[header: string]: string | string[];
};
observe?: 'body';
params?: HttpParams | {
[param: string]: string | string[];
};
reportProgress?: boolean;
responseType?: 'json';
withCredentials?: boolean;
}): Observable<T> {
return this.httpClient.put<T>(path, data, options).pipe(
tap(
() => {
if (isDevMode()) {
console.log('Put success' + path);
}
}
)
);
}

/**
* Sends a HTTP-DELETE requst to a given path
* @param path the path the request is fired to
* Returns Observable which can be subscribed to
*/
public delete<T>(path: string, options?: {
headers?: HttpHeaders | {
[header: string]: string | string[];
};
observe?: 'body';
params?: HttpParams | {
[param: string]: string | string[];
};
reportProgress?: boolean;
responseType?: 'json';
withCredentials?: boolean;
}): Observable<T> {
return this.httpClient.delete<T>(path, options).pipe(
tap(
() => {
if (isDevMode()) {
console.log('DELETE success' + path);
}
}
)
);
}
}

0 comments on commit ed47936

Please sign in to comment.