The sunbird-mobile-sdk follows a basic typescript library code structure that is modular. The following diagram shows the folder structure of the SDK.
This folder contains the definition file of all the plugins used in the sunbird-mobile-sdk.
Copy ```
interface HttpResponse {
status: number;
headers: any;
url: string;
data?: any;
error?: string;
}
interface Cordova {
plugin: {
http: {
setDataSerializer: (string) => void;
setHeader: (host: string, header: string, value: string) => void;
get: (url: string, parameters: any, headers: { [key: string]: string },
successCallback: (response: HttpResponse) => void,
errorCallback: (response: HttpResponse) => void) => void;
patch: (url: string, data: any, headers: { [key: string]: string },
successCallback: (response: HttpResponse) => void,
errorCallback: (response: HttpResponse) => void) => void;
post: (url: string, data: any, headers: { [key: string]: string },
successCallback: (response: HttpResponse) => void,
errorCallback: (response: HttpResponse) => void) => void;
}
};
}
```
This folder contains all the modules of sunbird-mobile-sdk. Each module folder contains majorly 5 sub-folders(config, def, errors, handlers, impl) and one index file.
Copy ```
export interface CourseServiceConfig {
apiPath: string;
}
```
Copy ```
export interface CourseService {
getBatchDetails(request: CourseBatchDetailsRequest): Observable<Batch>;
updateContentState(request: UpdateContentStateRequest): Observable<boolean>;
getCourseBatches(request: CourseBatchesRequest): Observable<Batch[]>;
getUserEnrolledCourses(request: GetUserEnrolledCoursesRequest): Observable<Course[]>;
enrollCourse(request: EnrollCourseRequest): Observable<boolean>;
unenrollCourse(unenrollCourseRequest: UnenrollCourseRequest): Observable<boolean>;
.......................
}
```
Copy ```
@injectable()
export class CourseServiceImpl implements CourseService {
constructor(
@inject(InjectionTokens.SDK_CONFIG) private sdkConfig: SdkConfig,
@inject(InjectionTokens.API_SERVICE) private apiService: ApiService,
......
) {
}
getBatchDetails(request: CourseBatchDetailsRequest): Observable<Batch> {
return new GetBatchDetailsHandler(this.apiService, this.courseServiceConfig)
.handle(request);
}
getCourseBatches(request: CourseBatchesRequest): Observable<Batch[]> {
return new GetCourseBatchesHandler(this.apiService, this.courseServiceConfig).handle(request);
}
getUserEnrolledCourses({request, from}: GetUserEnrolledCoursesRequest): Observable<Course[]> {
return this.cachedItemStore[from === CachedItemRequestSourceFrom.SERVER ? 'get' : 'getCached'](
request.userId + (request.filters ? '_' + JSON.stringify(request.filters) : ''),
CourseServiceImpl.USER_ENROLLMENT_LIST_KEY_PREFIX,
'ttl_' + CourseServiceImpl.USER_ENROLLMENT_LIST_KEY_PREFIX,
() => this.csCourseService.getUserEnrolledCourses(request, {}, {apiPath: '/api/course/v2', certRegistrationApiPath: ''}),
);
}
enrollCourse(request: EnrollCourseRequest): Observable<boolean> {
return new EnrollCourseHandler(this.apiService, this.courseServiceConfig)
.handle(request)
.pipe(
mergeMap((isEnrolled) => {
if (isEnrolled) {
const courseContext: { [key: string]: any } = {};
courseContext['userId'] = request.userId;
courseContext['batchStatus'] = request.batchStatus;
return this.sharedPreferences.putString(ContentKeys.COURSE_CONTEXT, JSON.stringify(courseContext)).pipe(
delay(2000),
concatMap(() => {
return this.getEnrolledCourses({userId: request.userId, returnFreshCourses: true});
}),
mapTo(isEnrolled)
);
}
return of(isEnrolled);
})
);
}
......
```