Skip to content

Commit 1ed7c81

Browse files
committed
Code cleanup
1 parent 7277d40 commit 1ed7c81

File tree

3 files changed

+27
-52
lines changed

3 files changed

+27
-52
lines changed

src/main/kotlin/com/jetpackduba/gitnuro/lfs/LfsNetworkDataSource.kt

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,10 @@ interface ILfsNetworkDataSource {
2525
suspend fun postBatchObjects(
2626
remoteUrl: String,
2727
lfsPrepareUploadObjectBatch: LfsPrepareUploadObjectBatch,
28-
onCreateHeaders: HttpRequestBuilder.() -> Unit,
29-
): Result<LfsObjects, LfsError>
30-
31-
suspend fun uploadBatchObjects(
32-
remoteUrl: String,
33-
lfsPrepareUploadObjectBatch: LfsPrepareUploadObjectBatch,
28+
objHeaders: Map<String, String>,
3429
username: String?,
3530
password: String?,
36-
)
31+
): Result<LfsObjects, LfsError>
3732

3833
suspend fun uploadObject(
3934
uploadUrl: String,
@@ -71,15 +66,28 @@ class LfsNetworkDataSource @Inject constructor(
7166
override suspend fun postBatchObjects(
7267
remoteUrl: String,
7368
lfsPrepareUploadObjectBatch: LfsPrepareUploadObjectBatch,
74-
onCreateHeaders: HttpRequestBuilder.() -> Unit,
69+
objHeaders: Map<String, String>,
70+
username: String?,
71+
password: String?,
7572
): Result<LfsObjects, LfsError> {
7673
val response = client.post("${remoteUrl.removeSuffix("/")}/objects/batch") {
74+
for (header in objHeaders) {
75+
header(header.key, header.value)
76+
}
77+
7778
this.headers {
78-
this["Accept"] = "application/vnd.git-lfs+json"
79-
this["Content-Type"] = "application/vnd.git-lfs+json"
79+
if (username != null && password != null && !headers.contains(NetworkConstants.AUTH_HEADER)) {
80+
basicAuth(username, password)
81+
}
82+
83+
if (!this.contains(NetworkConstants.ACCEPT_HEADER)) {
84+
this[NetworkConstants.ACCEPT_HEADER] = "application/vnd.git-lfs"
85+
}
8086
}
8187

82-
onCreateHeaders()
88+
this.headers {
89+
contentType(ContentType("application", "vnd.git-lfs+json"))
90+
}
8391

8492
setBody(json.encodeToString(lfsPrepareUploadObjectBatch))
8593
}
@@ -91,31 +99,6 @@ class LfsNetworkDataSource @Inject constructor(
9199
return Result.Ok(json.decodeFromString(response.bodyAsText()))
92100
}
93101

94-
override suspend fun uploadBatchObjects(
95-
remoteUrl: String,
96-
lfsPrepareUploadObjectBatch: LfsPrepareUploadObjectBatch,
97-
username: String?,
98-
password: String?,
99-
) {
100-
val response = client.post("$remoteUrl/objects/batch") {
101-
if (username != null && password != null) {
102-
basicAuth(username, password)
103-
}
104-
105-
this.headers {
106-
this["Accept"] = "application/vnd.git-lfs+json"
107-
this["Content-Type"] = "application/vnd.git-lfs+json"
108-
}
109-
110-
111-
this.setBody(json.encodeToString(lfsPrepareUploadObjectBatch))
112-
}
113-
114-
if (response.status.value != 200) {
115-
throw Exception("Code is ${response.status.value}...")
116-
}
117-
}
118-
119102
override suspend fun uploadObject(
120103
uploadUrl: String,
121104
oid: String,
@@ -175,7 +158,7 @@ class LfsNetworkDataSource @Inject constructor(
175158
}
176159

177160
if (!this.contains(NetworkConstants.ACCEPT_HEADER)) {
178-
this["Accept"] = "application/vnd.git-lfs"
161+
this[NetworkConstants.ACCEPT_HEADER] = "application/vnd.git-lfs"
179162
}
180163
}
181164

@@ -207,8 +190,8 @@ class LfsNetworkDataSource @Inject constructor(
207190
basicAuth(username, password)
208191
}
209192

210-
if (!this.contains("Accept")) {
211-
this["Accept"] = "application/vnd.git-lfs"
193+
if (!this.contains(NetworkConstants.ACCEPT_HEADER)) {
194+
this[NetworkConstants.ACCEPT_HEADER] = "application/vnd.git-lfs"
212195
}
213196
}
214197
}

src/main/kotlin/com/jetpackduba/gitnuro/lfs/LfsRepository.kt

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,10 @@ class LfsRepository @Inject constructor(
3636
branch = branch,
3737
objects = objects,
3838
),
39-
) {
40-
for (header in headers) {
41-
this.header(header.key, header.value)
42-
}
43-
44-
if (
45-
!headers.containsKey(NetworkConstants.AUTH_HEADER) &&
46-
(username != null && password != null)
47-
) {
48-
basicAuth(username, password)
49-
}
50-
}
39+
objHeaders = headers,
40+
username,
41+
password,
42+
)
5143
}
5244

5345
private fun createLfsPrepareUploadObjectBatch(

src/main/kotlin/com/jetpackduba/gitnuro/lfs/LfsSmudgeFilter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class LfsSmudgeFilter @AssistedInject constructor(
105105
}
106106

107107
when (lfsObjects) {
108-
is Result.Err -> throw LfsException("Gettings LFS objects failed with error: {lfsObjects.error}")
108+
is Result.Err -> throw LfsException("Gettings LFS objects failed with error: ${lfsObjects.error}")
109109
is Result.Ok -> {
110110

111111
val lfsObject = lfsObjects.value.objects.firstOrNull() // There should be only one LFS object

0 commit comments

Comments
 (0)