@@ -7,9 +7,12 @@ import kotlinx.coroutines.CoroutineDispatcher
77import kotlinx.coroutines.Dispatchers
88import kotlinx.coroutines.withContext
99import me.uport.sdk.core.hexToByteArray
10- import okhttp3.*
10+ import okhttp3.Call
1111import okhttp3.MediaType.Companion.toMediaType
12- import okio.ByteString
12+ import okhttp3.OkHttpClient
13+ import okhttp3.Request
14+ import okhttp3.RequestBody
15+ import okhttp3.RequestBody.Companion.toRequestBody
1316import okio.ByteString.Companion.encodeUtf8
1417import okio.ByteString.Companion.toByteString
1518import org.blockstack.android.sdk.ecies.signContent
@@ -23,7 +26,6 @@ import org.kethereum.model.ECKeyPair
2326import org.kethereum.model.PrivateKey
2427import org.kethereum.model.PublicKey
2528import org.komputing.khash.sha256.extensions.sha256
26- import org.komputing.khex.extensions.hexToByteArray
2729import org.komputing.khex.model.HexString
2830import java.io.IOException
2931import java.math.BigInteger
@@ -82,7 +84,7 @@ class BlockstackSession(private val sessionStore: ISessionStore, private val app
8284
8385 val userData = authResponseToUserData(tokenPayload, nameLookupUrl, appPrivateKey, coreSessionToken, authResponse)
8486
85- this @BlockstackSession.appPrivateKey = appPrivateKey
87+ this @BlockstackSession.appPrivateKey = appPrivateKey
8688 sessionStore.updateUserData(userData)
8789
8890 return @withContext Result (userData)
@@ -333,7 +335,7 @@ class BlockstackSession(private val sessionStore: ISessionStore, private val app
333335 * @property a result object wiht a `String` representation of a url from
334336 * which you can read the file that was just put.
335337 */
336- suspend fun putFile (path : String , content : Any , options : PutFileOptions ): Result <out String > = withContext(dispatcher){
338+ suspend fun putFile (path : String , content : Any , options : PutFileOptions ): Result <out String > = withContext(dispatcher) {
337339 Log .d(TAG , " putFile: path: ${path} options: ${options} " )
338340 val gaiaHubConfiguration = getOrSetLocalGaiaHubConnection()
339341 val valid = content is String || content is ByteArray
@@ -379,35 +381,35 @@ class BlockstackSession(private val sessionStore: ISessionStore, private val app
379381 }
380382
381383
382- try {
383- val response = hub.uploadToGaiaHub(path, requestContent, gaiaHubConfiguration, contentType)
384- if (! response.isSuccessful) {
385- return @withContext Result (null , ResultError (ErrorCode .NetworkError , " upload to gaia failed: ${response.code} " , response.code.toString()))
386- }
387- val responseText = response.body?.string()
388- if (responseText != = null ) {
389- if (! options.shouldEncrypt() && options.shouldSign()) {
390- val signedContent = signContent(requestContent.toByteArray(), getSignKey(options))
391- try {
392- val sigResponse = hub.uploadToGaiaHub(" $path$SIGNATURE_FILE_EXTENSION " , signedContent.toJSONByteString(), gaiaHubConfig!! , " application/json" )
393- if (! sigResponse.isSuccessful) {
394- return @withContext Result (null , ResultError (ErrorCode .NetworkError , " failed to upload putFile signature $responseText " , sigResponse.code.toString()))
395- }
396- } catch (e: Exception ) {
397- return @withContext Result (null , ResultError (ErrorCode .UnknownError , " invalid response from putFile signature $responseText " ))
384+ try {
385+ val response = hub.uploadToGaiaHub(path, requestContent, gaiaHubConfiguration, contentType)
386+ if (! response.isSuccessful) {
387+ return @withContext Result (null , ResultError (ErrorCode .NetworkError , " upload to gaia failed: ${response.code} " , response.code.toString()))
388+ }
389+ val responseText = response.body?.string()
390+ if (responseText != = null ) {
391+ if (! options.shouldEncrypt() && options.shouldSign()) {
392+ val signedContent = signContent(requestContent.toByteArray(), getSignKey(options))
393+ try {
394+ val sigResponse = hub.uploadToGaiaHub(" $path$SIGNATURE_FILE_EXTENSION " , signedContent.toJSONByteString(), gaiaHubConfig!! , " application/json" )
395+ if (! sigResponse.isSuccessful) {
396+ return @withContext Result (null , ResultError (ErrorCode .NetworkError , " failed to upload putFile signature $responseText " , sigResponse.code.toString()))
398397 }
398+ } catch (e: Exception ) {
399+ return @withContext Result (null , ResultError (ErrorCode .UnknownError , " invalid response from putFile signature $responseText " ))
399400 }
400-
401- val responseJSON = JSONObject (responseText)
402- return @withContext Result (responseJSON.getString(" publicURL" ))
403- } else {
404- return @withContext Result (null , ResultError (ErrorCode .UnknownError , " invalid response from putFile $responseText " ))
405401 }
406- } catch (e: Exception ) {
407- Log .d(TAG , e.message, e)
408- return @withContext Result (null , ResultError (ErrorCode .UnknownError , e.message
409- ? : e.toString()))
402+
403+ val responseJSON = JSONObject (responseText)
404+ return @withContext Result (responseJSON.getString(" publicURL" ))
405+ } else {
406+ return @withContext Result (null , ResultError (ErrorCode .UnknownError , " invalid response from putFile $responseText " ))
410407 }
408+ } catch (e: Exception ) {
409+ Log .d(TAG , e.message, e)
410+ return @withContext Result (null , ResultError (ErrorCode .UnknownError , e.message
411+ ? : e.toString()))
412+ }
411413
412414
413415 }
@@ -440,7 +442,7 @@ class BlockstackSession(private val sessionStore: ISessionStore, private val app
440442 }
441443
442444
443- suspend fun deleteFile (path : String , options : DeleteFileOptions = DeleteFileOptions ()): Result <out Unit > = withContext(dispatcher){
445+ suspend fun deleteFile (path : String , options : DeleteFileOptions = DeleteFileOptions ()): Result <out Unit > = withContext(dispatcher) {
444446 try {
445447 val response = hub.deleteFromGaiaHub(path, options.gaiaHubConfig ? : gaiaHubConfig!! )
446448 if (response != null ) {
@@ -492,7 +494,7 @@ class BlockstackSession(private val sessionStore: ISessionStore, private val app
492494 hub.getFullReadUrl(path, gaiaHubConfig)
493495 }
494496
495- return if (readUrl == Blockstack .NO_URL ){
497+ return if (readUrl == Blockstack .NO_URL ) {
496498 Result (value = null , error = ResultError (ErrorCode .MissingReadUrl , " ${options.username} has not yet used the app" ))
497499 } else {
498500 Result (readUrl)
@@ -559,7 +561,7 @@ class BlockstackSession(private val sessionStore: ISessionStore, private val app
559561 .addHeader(" Authorization" , " bearer ${hubConfig.token} " )
560562 .addHeader(" Referrer-Policy" , " no-referrer" )
561563
562- .method(" POST" , RequestBody .create (CONTENT_TYPE_JSON .toMediaType(), pageRequest ))
564+ .method(" POST" , pageRequest.toRequestBody (CONTENT_TYPE_JSON .toMediaType()))
563565 .build()
564566 }
565567
0 commit comments