forked from microsoft/CDM
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGithubAdapter.ts
More file actions
99 lines (75 loc) · 2.81 KB
/
Copy pathGithubAdapter.ts
File metadata and controls
99 lines (75 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
import { CdmHttpClient, CdmHttpRequest, CdmHttpResponse } from '../Utilities/Network';
import { NetworkAdapter } from './NetworkAdapter';
import { configObjectType } from '../internal';
/**
* @deprecated Please use the CdmStandardsAdapter instead.
*/
export class GithubAdapter extends NetworkAdapter {
private static readonly ghHost: string = 'raw.githubusercontent.com';
private static readonly ghPath: string = '/Microsoft/CDM/master/schemaDocuments';
/**
* @internal
*/
public readonly type: string = 'github';
private readonly url: string;
constructor() {
super();
this.httpClient = new CdmHttpClient(`https://${GithubAdapter.ghHost}`);
}
private static ghRawRoot(): string {
return `https://${this.ghHost}${this.ghPath}`;
}
public canRead(): boolean {
return true;
}
public async readAsync(corpusPath: string): Promise<string> {
const cdmHttpRequest: CdmHttpRequest =
this.setUpCdmRequest(
`${GithubAdapter.ghPath}${corpusPath}`,
new Map<string, string>(),
'GET'
);
const cdmHttpResponse: CdmHttpResponse = await super.executeRequest(cdmHttpRequest);
return cdmHttpResponse.content;
}
public createAdapterPath(corpusPath: string): string {
return `${GithubAdapter.ghRawRoot()}${corpusPath}`;
}
public createCorpusPath(adapterPath: string): string {
const ghRoot: string = GithubAdapter.ghRawRoot();
// might not be an adapterPath that we understand. check that first
if (ghRoot && adapterPath.startsWith(ghRoot)) {
return adapterPath.slice(ghRoot.length);
}
return undefined;
}
public fetchConfig(): string {
const resultConfig: configObjectType = {
type: this.type
};
// Construct network configs.
const configObject: configObjectType = this.fetchNetworkConfig();
if (this.locationHint) {
configObject.locationHint = this.locationHint;
}
resultConfig.config = configObject;
return JSON.stringify(resultConfig);
}
public updateConfig(config: string): void {
if (!config) {
// It is fine just to skip it for GitHub adapter.
return;
}
this.updateNetworkConfig(config);
const configJson: configObjectType = JSON.parse(config);
if (configJson.locationHint) {
this.locationHint = JSON.stringify(configJson.locationHint);
}
}
}
interface FileInfo {
// fill this in with things we need about the file
// size? date/time modified?
}