Skip to content

Commit

Permalink
fix: move bitly code to backend
Browse files Browse the repository at this point in the history
  • Loading branch information
edewit committed Nov 7, 2019
1 parent 1cd2d32 commit b8b7887
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 22 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<version>1.0.0</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<kotlin.compiler.jvmTarget>1.8</kotlin.compiler.jvmTarget>
<version.kotlin-stdlib>1.3.21</version.kotlin-stdlib>
<version.maven-failsafe-plugin>2.22.2</version.maven-failsafe-plugin>
<version.rest-assured>3.3.0</version.rest-assured>
Expand Down Expand Up @@ -80,6 +81,11 @@
<artifactId>commons-compress</artifactId>
<version>${version.commons-compress}</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.2.2</version>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,7 @@ jest.mock('../backend-api', () => ({
},
]),
fetchConfig: () => { throw new Error("not used"); },
shortenUrl: async (url: string) => Promise.resolve(
{
link: 'http://bit.ly/2Mzdn9Z'
})
shortenUrl: async (params: string) => Promise.resolve('http://bit.ly/2Mzdn9Z')
}));


Expand Down
17 changes: 3 additions & 14 deletions src/main/frontend/src/code-quarkus/backend-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,7 @@ export async function fetchConfig() {
}
}

export async function shortenUrl(downloadLink: string) {
const response = await fetch('https://api-ssl.bitly.com/v4/shorten', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer a3badec1a128e46e42f36a138165cbbd21ed6cee'
},
body: JSON.stringify({
'group_guid': 'Bjam8omLBjB',
'long_url': downloadLink
})
});
return response.ok ? await response.json() : undefined
export async function shortenUrl(params: string) {
const response = await fetch(`${backendUrl}/api/shorten?${params}`);
return response.ok ? await response.text() : undefined
}
4 changes: 2 additions & 2 deletions src/main/frontend/src/code-quarkus/code-quarkus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ async function generateProject(project: QuarkusProject): Promise<{ downloadLink:
const backendUrl = process.env.REACT_APP_BACKEND_URL || publicUrl;
const downloadLink = `${backendUrl}/api/download?${stringify(params)}`;

const data = await shortenUrl(downloadLink);
const link = await shortenUrl(stringify(params));
window.open(downloadLink, '_blank');
return { downloadLink: data ? data.link : downloadLink };
return { downloadLink: link || downloadLink };
}

const DEFAULT_PROJECT = {
Expand Down
38 changes: 37 additions & 1 deletion src/main/kotlin/io/quarkus/code/CodeQuarkusResource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ import org.eclipse.microprofile.openapi.annotations.media.Content
import org.eclipse.microprofile.openapi.annotations.media.Schema
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse
import javax.inject.Inject
import javax.json.bind.JsonbBuilder
import javax.validation.Valid
import javax.ws.rs.BeanParam
import javax.ws.rs.GET
import javax.ws.rs.Path
import javax.ws.rs.Produces
import javax.ws.rs.core.MediaType.APPLICATION_JSON
import javax.ws.rs.core.MediaType.TEXT_PLAIN
import javax.ws.rs.core.Response

import okhttp3.*
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody.Companion.toRequestBody

@Path("/")
class CodeQuarkusResource {
Expand All @@ -32,6 +37,8 @@ class CodeQuarkusResource {
@Inject
lateinit var projectCreator: QuarkusProjectCreator

private val httpClient = OkHttpClient()

@GET
@Path("/config")
@Produces(APPLICATION_JSON)
Expand All @@ -57,6 +64,36 @@ class CodeQuarkusResource {
return extensionCatalog.extensions
}

@GET
@Path("/shorten")
@Produces(TEXT_PLAIN)
@Operation(summary = "Create a short url based on the parameters")
fun shorten(@Valid @BeanParam project: QuarkusProject): Response {
val JSON = "application/json; charset=utf-8".toMediaType()
val body = """
{
"group_guid": "$bitlyGroupId",
"long_url": "https://code.quarkus.io/api/download?g=${project.groupId}&a=${project.artifactId}&v=${project.version}&c=${project.className}&e=${project.extensions}"
}
""".toRequestBody(JSON)

val request = Request.Builder()
.url("https://api-ssl.bitly.com/v4/shorten")
.addHeader("Authorization", "Bearer $bitlyAccessToken")
.post(body)
.build()

httpClient.newCall(request).execute().use { response ->

if (!response.isSuccessful) throw IOException("Unexpected code $response")

val jsonb = JsonbBuilder.create()
val obj = jsonb.fromJson(response.body!!.string(), BitlyResponse::class.java)

return Response.ok(obj.link).build()
}
}

@GET
@Path("/download")
@Produces("application/zip")
Expand All @@ -67,6 +104,5 @@ class CodeQuarkusResource {
.type("application/zip")
.header("Content-Disposition", "attachment; filename=\"${project.artifactId}.zip\"")
.build()

}
}
5 changes: 5 additions & 0 deletions src/main/kotlin/io/quarkus/code/model/BitlyResponse.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.quarkus.code

class BitlyResponse {
var link: String? = null
}
5 changes: 4 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ quarkus.log.file.enable=false
quarkus.http.cors=true
io.quarkus.code.quarkus-version=${version.quarkus}
io.quarkus.code.quarkus-platform-version=${version.quarkus-platform}
io.quarkus.code.git-commit-id=${git.commit.id.abbrev}
io.quarkus.code.git-commit-id=${git.commit.id.abbrev}

io.quarkus.code.bitly.generic-access-token=a3badec1a128e46e42f36a138165cbbd21ed6cee
io.quarkus.code.bitly.group-id=Bjam8omLBjB

0 comments on commit b8b7887

Please sign in to comment.