Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
PID Migrator
Manage
Activity
Members
Labels
Plan
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
Coscine
backend
scripts
PID Migrator
Commits
e807e280
Commit
e807e280
authored
2 years ago
by
Benedikt Heinrichs
Browse files
Options
Downloads
Patches
Plain Diff
Fix migration oddities
parent
dc769cf9
Branches
Issue/2312-oldPIDScript
No related tags found
No related merge requests found
Changes
2
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/PIDMigrator/EpicClient.cs
+192
-0
192 additions, 0 deletions
src/PIDMigrator/EpicClient.cs
src/PIDMigrator/Program.cs
+4
-3
4 additions, 3 deletions
src/PIDMigrator/Program.cs
with
196 additions
and
3 deletions
src/PIDMigrator/EpicClient.cs
0 → 100644
+
192
−
0
View file @
e807e280
using
Coscine.ProxyApi.Utils
;
using
Microsoft.AspNetCore.WebUtilities
;
using
Newtonsoft.Json
;
using
System.Net
;
using
System.Net.Http.Headers
;
using
System.Text
;
using
System.Web
;
namespace
PIDMigrator
{
public
class
EpicClient
:
IEpicClient
{
private
readonly
HttpClient
_httpClient
=
new
();
public
const
int
errorPrefix
=
12000
;
private
string
ServiceEndpoint
{
get
;
set
;
}
public
string
Prefix
{
get
;
private
set
;
}
public
EpicClient
(
string
url
,
string
prefix
,
string
user
,
string
password
)
{
ServiceEndpoint
=
url
;
Prefix
=
prefix
;
var
byteArray
=
Encoding
.
ASCII
.
GetBytes
(
$"
{
user
}
:
{
password
}
"
);
_httpClient
.
DefaultRequestHeaders
.
Authorization
=
new
AuthenticationHeaderValue
(
"Basic"
,
Convert
.
ToBase64String
(
byteArray
));
_httpClient
.
DefaultRequestHeaders
.
Add
(
"Accept"
,
"application/json, */*"
);
}
private
void
ReplacePayload
(
string
suffix
,
List
<
EpicData
>
payload
)
{
foreach
(
var
entry
in
payload
)
{
if
(
entry
.
ParsedData
is
string
)
{
entry
.
ParsedData
=
(
entry
.
ParsedData
as
string
)
.
Replace
(
"{{pid}}"
,
$"http://hdl.handle.net/
{
Prefix
}
/
{
suffix
}
"
);
}
}
}
public
EpicData
Update
(
string
suffix
,
List
<
EpicData
>
payload
)
{
ReplacePayload
(
suffix
,
payload
);
using
var
requestContent
=
new
StringContent
(
JsonConvert
.
SerializeObject
(
payload
),
Encoding
.
UTF8
,
"application/json"
);
var
result
=
EpicRequestWrapper
(()
=>
_httpClient
.
PutAsync
(
ServiceEndpoint
+
suffix
,
requestContent
));
if
(
result
.
IsSuccessStatusCode
)
{
var
content
=
result
.
Content
.
ReadAsStringAsync
().
Result
;
return
JsonConvert
.
DeserializeObject
<
EpicData
>(
content
);
}
return
null
;
}
public
EpicData
Create
(
List
<
EpicData
>
payload
,
string
guid
=
null
)
{
string
suffix
=
guid
;
if
(
suffix
is
null
)
{
suffix
=
Guid
.
NewGuid
().
ToString
();
}
return
Update
(
suffix
,
payload
);
}
public
IEnumerable
<
EpicData
>
Search
(
string
searchUrl
,
int
limit
=
0
)
{
var
pidList
=
new
List
<
EpicData
>();
// Handle additional query parameters
var
parameters
=
new
Dictionary
<
string
,
string
>();
if
(!
string
.
IsNullOrWhiteSpace
(
searchUrl
))
{
parameters
.
Add
(
"URL"
,
searchUrl
);
}
if
(
limit
>
0
)
{
parameters
.
Add
(
"limit"
,
limit
.
ToString
());
}
// Build the request URL
var
url
=
new
Uri
(
QueryHelpers
.
AddQueryString
(
ServiceEndpoint
,
parameters
));
// Execute the request
var
result
=
EpicRequestWrapper
(()
=>
_httpClient
.
GetAsync
(
url
));
if
(
result
is
not
null
&&
result
.
IsSuccessStatusCode
)
{
var
content
=
result
.
Content
.
ReadAsStringAsync
().
Result
;
content
.
Split
(
'\n'
).
ToList
().
ForEach
(
e
=>
pidList
.
Add
(
new
EpicData
()
{
EpicPid
=
e
}));
}
return
pidList
;
}
public
IEnumerable
<
EpicData
>
List
(
int
limit
=
0
,
int
page
=
0
)
{
var
pidList
=
new
List
<
EpicData
>();
// Handle additional query parameters
var
parameters
=
new
Dictionary
<
string
,
string
>();
if
(
limit
>
0
)
{
parameters
.
Add
(
"limit"
,
limit
.
ToString
());
}
if
(
page
>
0
)
{
parameters
.
Add
(
"page"
,
page
.
ToString
());
}
// Build the request URL
var
url
=
new
Uri
(
QueryHelpers
.
AddQueryString
(
ServiceEndpoint
,
parameters
));
// Execute the request
var
result
=
EpicRequestWrapper
(()
=>
_httpClient
.
GetAsync
(
url
));
if
(
result
.
IsSuccessStatusCode
)
{
var
content
=
result
.
Content
.
ReadAsStringAsync
().
Result
;
content
.
Split
(
'\n'
).
ToList
().
ForEach
(
e
=>
pidList
.
Add
(
new
EpicData
()
{
EpicPid
=
e
}));
}
else
{
Console
.
WriteLine
();
}
return
pidList
;
}
public
IEnumerable
<
EpicData
>
ListAll
(
int
limit
=
1000
,
int
startPage
=
1
)
{
var
pidList
=
new
List
<
EpicData
>();
var
tempList
=
List
(
limit
,
startPage
);
while
(
tempList
.
Any
())
{
pidList
.
AddRange
(
tempList
);
startPage
++;
tempList
=
List
(
limit
,
startPage
);
}
return
pidList
;
}
public
void
Delete
(
string
suffix
)
{
var
result
=
EpicRequestWrapper
(()
=>
_httpClient
.
DeleteAsync
(
ServiceEndpoint
+
suffix
));
}
public
IEnumerable
<
EpicData
>
Get
(
string
suffix
)
{
try
{
var
pidList
=
new
List
<
EpicData
>();
var
result
=
EpicRequestWrapper
(()
=>
_httpClient
.
GetAsync
(
ServiceEndpoint
+
suffix
));
if
(
result
.
IsSuccessStatusCode
)
{
var
content
=
result
.
Content
.
ReadAsStringAsync
().
Result
;
return
JsonConvert
.
DeserializeObject
<
List
<
EpicData
>>(
content
);
}
return
pidList
;
}
catch
(
WebException
e
)
{
var
response
=
(
HttpWebResponse
)
e
.
Response
;
if
(
response
.
StatusCode
==
HttpStatusCode
.
NotFound
)
{
return
new
List
<
EpicData
>(
0
);
}
else
{
throw
e
;
}
}
}
// Wrapper for requests to the Epic server since sometimes it gives a 500, however the request was valid
public
static
HttpResponseMessage
EpicRequestWrapper
(
Func
<
Task
<
HttpResponseMessage
>>
request
)
{
var
count
=
0
;
var
maxCount
=
10
;
Exception
lastException
;
do
{
try
{
var
requestTask
=
request
();
return
requestTask
.
Result
;
}
catch
(
Exception
e
)
{
lastException
=
e
;
count
++;
}
}
while
(
count
<
maxCount
);
throw
lastException
;
}
}
}
This diff is collapsed.
Click to expand it.
src/PIDMigrator/Program.cs
+
4
−
3
View file @
e807e280
...
...
@@ -39,9 +39,10 @@ public static class Program
{
if
(
pidMatch
.
EpicPid
!=
""
)
{
Console
.
WriteLine
(
$"┌ PID \"
{
pidMatch
.
EpicPid
}
\""
);
var
pid
=
pidMatch
.
EpicPid
.
Replace
(
"\r"
,
""
);
Console
.
WriteLine
(
$"┌ PID \"
{
pid
}
\""
);
// Get it from the EpicClient
var
epicClientPid
=
epicClient
.
Get
(
pid
Match
.
EpicPid
);
var
epicClientPid
=
epicClient
.
Get
(
pid
);
if
(!
epicClientPid
.
Any
(
e
=>
{
var
parsedData
=
e
.
ParsedData
.
ToString
();
...
...
@@ -56,7 +57,7 @@ public static class Program
continue
;
}
MigratePid
(
pid
Match
.
EpicPid
);
MigratePid
(
pid
);
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment