diff --git a/README.md b/README.md index b7972572..3e7f1a56 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,21 @@ To use the xAPI Java Client include the appropriate XML in the `dependencies` se ``` +### Configuration + +The xAPI Java Client has a Spring AutoConfiguration bean which picks up the following properties: + +| Property | Description | +| ----------------------------- | ------------------------------------------------------------------ | +| xapi.client.baseUrl | The base url of the LRS endpoint | +| xapi.client.username | Username for basic authorization header | +| xapi.client.password | Password for basic authorization header | +| xapi.client.authorization | Authorization header (has precedence over the username and password properties) | + +Properties can be set using any [external configuration](https://docs.spring.io/spring-boot/docs/3.0.4/reference/htmlsingle/#features.external-config.files) method supported by Spring Boot. + +If you need more specific customization (eg. your LRS needs specific headers, or you want to set the authorization header dynamically) you can create a custom configurer by implementing the `XapiClientConfigurer` interface. + ### Statement Resource The xAPI Java Client allows applications to store and fetch xAPI [Statements](https://github.com/adlnet/xAPI-Spec/blob/master/xAPI-Data.md#statements). diff --git a/samples/delete-activity-profile/src/main/java/dev/learning/xapi/samples/deleteactivityprofile/DeleteActivityProfileApplication.java b/samples/delete-activity-profile/src/main/java/dev/learning/xapi/samples/deleteactivityprofile/DeleteActivityProfileApplication.java index 2ed768ed..cad056d0 100644 --- a/samples/delete-activity-profile/src/main/java/dev/learning/xapi/samples/deleteactivityprofile/DeleteActivityProfileApplication.java +++ b/samples/delete-activity-profile/src/main/java/dev/learning/xapi/samples/deleteactivityprofile/DeleteActivityProfileApplication.java @@ -7,10 +7,10 @@ import dev.learning.xapi.client.XapiClient; import dev.learning.xapi.samples.core.ExampleState; import java.time.Instant; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.web.reactive.function.client.WebClient; /** * Sample using xAPI client to delete an activity profile. @@ -20,24 +20,11 @@ @SpringBootApplication public class DeleteActivityProfileApplication implements CommandLineRunner { - private final XapiClient client; - /** - * Constructor for application. In this sample the WebClient.Builder instance is injected by the - * Spring Framework. + * Default xAPI client. Properties are picked automatically from application.properties. */ - public DeleteActivityProfileApplication(WebClient.Builder webClientBuilder) { - - webClientBuilder - // Change for the URL of your LRS - .baseUrl("https://example.com/xapi/") - // Set the Authorization value - .defaultHeader("Authorization", "") - - .build(); - - client = new XapiClient(webClientBuilder); - } + @Autowired + private XapiClient client; public static void main(String[] args) { SpringApplication.run(DeleteActivityProfileApplication.class, args).close(); diff --git a/samples/delete-activity-profile/src/main/resources/application.properties b/samples/delete-activity-profile/src/main/resources/application.properties new file mode 100644 index 00000000..de20217a --- /dev/null +++ b/samples/delete-activity-profile/src/main/resources/application.properties @@ -0,0 +1,3 @@ +xapi.client.username = admin +xapi.client.password = password +xapi.client.baseUrl = https://example.com/xapi/ diff --git a/samples/delete-agent-profile/src/main/java/dev/learning/xapi/samples/deleteagentprofile/DeleteAgentProfileApplication.java b/samples/delete-agent-profile/src/main/java/dev/learning/xapi/samples/deleteagentprofile/DeleteAgentProfileApplication.java index 18dce5a6..f22a5486 100644 --- a/samples/delete-agent-profile/src/main/java/dev/learning/xapi/samples/deleteagentprofile/DeleteAgentProfileApplication.java +++ b/samples/delete-agent-profile/src/main/java/dev/learning/xapi/samples/deleteagentprofile/DeleteAgentProfileApplication.java @@ -7,10 +7,10 @@ import dev.learning.xapi.client.XapiClient; import dev.learning.xapi.samples.core.ExampleState; import java.time.Instant; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.web.reactive.function.client.WebClient; /** * Sample using xAPI client to delete an agent profile. @@ -20,24 +20,11 @@ @SpringBootApplication public class DeleteAgentProfileApplication implements CommandLineRunner { - private final XapiClient client; - /** - * Constructor for application. In this sample the WebClient.Builder instance is injected by the - * Spring Framework. + * Default xAPI client. Properties are picked automatically from application.properties. */ - public DeleteAgentProfileApplication(WebClient.Builder webClientBuilder) { - - webClientBuilder - // Change for the URL of your LRS - .baseUrl("https://example.com/xapi/") - // Set the Authorization value - .defaultHeader("Authorization", "") - - .build(); - - client = new XapiClient(webClientBuilder); - } + @Autowired + private XapiClient client; public static void main(String[] args) { SpringApplication.run(DeleteAgentProfileApplication.class, args).close(); diff --git a/samples/delete-agent-profile/src/main/resources/application.properties b/samples/delete-agent-profile/src/main/resources/application.properties new file mode 100644 index 00000000..de20217a --- /dev/null +++ b/samples/delete-agent-profile/src/main/resources/application.properties @@ -0,0 +1,3 @@ +xapi.client.username = admin +xapi.client.password = password +xapi.client.baseUrl = https://example.com/xapi/ diff --git a/samples/delete-state/src/main/java/dev/learning/xapi/samples/deletestate/DeleteStateApplication.java b/samples/delete-state/src/main/java/dev/learning/xapi/samples/deletestate/DeleteStateApplication.java index d980a2ef..fda47d8b 100644 --- a/samples/delete-state/src/main/java/dev/learning/xapi/samples/deletestate/DeleteStateApplication.java +++ b/samples/delete-state/src/main/java/dev/learning/xapi/samples/deletestate/DeleteStateApplication.java @@ -7,10 +7,10 @@ import dev.learning.xapi.client.XapiClient; import dev.learning.xapi.samples.core.ExampleState; import java.time.Instant; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.web.reactive.function.client.WebClient; /** * Sample using xAPI client to delete a state. @@ -20,24 +20,11 @@ @SpringBootApplication public class DeleteStateApplication implements CommandLineRunner { - private final XapiClient client; - /** - * Constructor for application. In this sample the WebClient.Builder instance is injected by the - * Spring Framework. + * Default xAPI client. Properties are picked automatically from application.properties. */ - public DeleteStateApplication(WebClient.Builder webClientBuilder) { - - webClientBuilder - // Change for the URL of your LRS - .baseUrl("https://example.com/xapi/") - // Set the Authorization value - .defaultHeader("Authorization", "") - - .build(); - - client = new XapiClient(webClientBuilder); - } + @Autowired + private XapiClient client; public static void main(String[] args) { SpringApplication.run(DeleteStateApplication.class, args).close(); diff --git a/samples/delete-state/src/main/resources/application.properties b/samples/delete-state/src/main/resources/application.properties new file mode 100644 index 00000000..de20217a --- /dev/null +++ b/samples/delete-state/src/main/resources/application.properties @@ -0,0 +1,3 @@ +xapi.client.username = admin +xapi.client.password = password +xapi.client.baseUrl = https://example.com/xapi/ diff --git a/samples/delete-states/src/main/java/dev/learning/xapi/samples/deletestates/DeleteStatesApplication.java b/samples/delete-states/src/main/java/dev/learning/xapi/samples/deletestates/DeleteStatesApplication.java index 865f63bb..596cd656 100644 --- a/samples/delete-states/src/main/java/dev/learning/xapi/samples/deletestates/DeleteStatesApplication.java +++ b/samples/delete-states/src/main/java/dev/learning/xapi/samples/deletestates/DeleteStatesApplication.java @@ -7,10 +7,10 @@ import dev.learning.xapi.client.XapiClient; import dev.learning.xapi.samples.core.ExampleState; import java.time.Instant; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.web.reactive.function.client.WebClient; /** * Sample using xAPI client to delete multiple states. @@ -20,24 +20,11 @@ @SpringBootApplication public class DeleteStatesApplication implements CommandLineRunner { - private final XapiClient client; - /** - * Constructor for application. In this sample the WebClient.Builder instance is injected by the - * Spring Framework. + * Default xAPI client. Properties are picked automatically from application.properties. */ - public DeleteStatesApplication(WebClient.Builder webClientBuilder) { - - webClientBuilder - // Change for the URL of your LRS - .baseUrl("https://example.com/xapi/") - // Set the Authorization value - .defaultHeader("Authorization", "") - - .build(); - - client = new XapiClient(webClientBuilder); - } + @Autowired + private XapiClient client; public static void main(String[] args) { SpringApplication.run(DeleteStatesApplication.class, args).close(); diff --git a/samples/delete-states/src/main/resources/application.properties b/samples/delete-states/src/main/resources/application.properties new file mode 100644 index 00000000..de20217a --- /dev/null +++ b/samples/delete-states/src/main/resources/application.properties @@ -0,0 +1,3 @@ +xapi.client.username = admin +xapi.client.password = password +xapi.client.baseUrl = https://example.com/xapi/ diff --git a/samples/get-about/src/main/java/dev/learning/xapi/samples/getabout/GetAboutApplication.java b/samples/get-about/src/main/java/dev/learning/xapi/samples/getabout/GetAboutApplication.java index 266b9b1b..dabe3866 100644 --- a/samples/get-about/src/main/java/dev/learning/xapi/samples/getabout/GetAboutApplication.java +++ b/samples/get-about/src/main/java/dev/learning/xapi/samples/getabout/GetAboutApplication.java @@ -6,11 +6,11 @@ import dev.learning.xapi.client.XapiClient; import dev.learning.xapi.model.About; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.http.ResponseEntity; -import org.springframework.web.reactive.function.client.WebClient; /** * Sample using xAPI client to get about. @@ -20,24 +20,11 @@ @SpringBootApplication public class GetAboutApplication implements CommandLineRunner { - private final XapiClient client; - /** - * Constructor for application. In this sample the WebClient.Builder instance is injected by the - * Spring Framework. + * Default xAPI client. Properties are picked automatically from application.properties. */ - public GetAboutApplication(WebClient.Builder webClientBuilder) { - - webClientBuilder - // Change for the URL of your LRS - .baseUrl("https://example.com/xapi/") - // Set the Authorization value - .defaultHeader("Authorization", "") - - .build(); - - client = new XapiClient(webClientBuilder); - } + @Autowired + private XapiClient client; public static void main(String[] args) { SpringApplication.run(GetAboutApplication.class, args).close(); diff --git a/samples/get-about/src/main/resources/application.properties b/samples/get-about/src/main/resources/application.properties new file mode 100644 index 00000000..de20217a --- /dev/null +++ b/samples/get-about/src/main/resources/application.properties @@ -0,0 +1,3 @@ +xapi.client.username = admin +xapi.client.password = password +xapi.client.baseUrl = https://example.com/xapi/ diff --git a/samples/get-activity-profile/src/main/java/dev/learning/xapi/samples/getactivityprofile/GetActivityProfileApplication.java b/samples/get-activity-profile/src/main/java/dev/learning/xapi/samples/getactivityprofile/GetActivityProfileApplication.java index ffeb0a91..af7ccde5 100644 --- a/samples/get-activity-profile/src/main/java/dev/learning/xapi/samples/getactivityprofile/GetActivityProfileApplication.java +++ b/samples/get-activity-profile/src/main/java/dev/learning/xapi/samples/getactivityprofile/GetActivityProfileApplication.java @@ -7,11 +7,11 @@ import dev.learning.xapi.client.XapiClient; import dev.learning.xapi.samples.core.ExampleState; import java.time.Instant; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.http.ResponseEntity; -import org.springframework.web.reactive.function.client.WebClient; /** * Sample using xAPI client to get an activity profile. @@ -21,24 +21,11 @@ @SpringBootApplication public class GetActivityProfileApplication implements CommandLineRunner { - private final XapiClient client; - /** - * Constructor for application. In this sample the WebClient.Builder instance is injected by the - * Spring Framework. + * Default xAPI client. Properties are picked automatically from application.properties. */ - public GetActivityProfileApplication(WebClient.Builder webClientBuilder) { - - webClientBuilder - // Change for the URL of your LRS - .baseUrl("https://example.com/xapi/") - // Set the Authorization value - .defaultHeader("Authorization", "") - - .build(); - - client = new XapiClient(webClientBuilder); - } + @Autowired + private XapiClient client; public static void main(String[] args) { SpringApplication.run(GetActivityProfileApplication.class, args).close(); diff --git a/samples/get-activity-profile/src/main/resources/application.properties b/samples/get-activity-profile/src/main/resources/application.properties new file mode 100644 index 00000000..de20217a --- /dev/null +++ b/samples/get-activity-profile/src/main/resources/application.properties @@ -0,0 +1,3 @@ +xapi.client.username = admin +xapi.client.password = password +xapi.client.baseUrl = https://example.com/xapi/ diff --git a/samples/get-activity-profiles/src/main/java/dev/learning/xapi/samples/getactivityprofiles/GetActivityProfilesApplication.java b/samples/get-activity-profiles/src/main/java/dev/learning/xapi/samples/getactivityprofiles/GetActivityProfilesApplication.java index 372d01ac..8fe82a06 100644 --- a/samples/get-activity-profiles/src/main/java/dev/learning/xapi/samples/getactivityprofiles/GetActivityProfilesApplication.java +++ b/samples/get-activity-profiles/src/main/java/dev/learning/xapi/samples/getactivityprofiles/GetActivityProfilesApplication.java @@ -8,11 +8,11 @@ import dev.learning.xapi.samples.core.ExampleState; import java.time.Instant; import java.util.List; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.http.ResponseEntity; -import org.springframework.web.reactive.function.client.WebClient; /** * Sample using xAPI client to get activity profiles. @@ -22,24 +22,11 @@ @SpringBootApplication public class GetActivityProfilesApplication implements CommandLineRunner { - private final XapiClient client; - /** - * Constructor for application. In this sample the WebClient.Builder instance is injected by the - * Spring Framework. + * Default xAPI client. Properties are picked automatically from application.properties. */ - public GetActivityProfilesApplication(WebClient.Builder webClientBuilder) { - - webClientBuilder - // Change for the URL of your LRS - .baseUrl("https://example.com/xapi/") - // Set the Authorization value - .defaultHeader("Authorization", "") - - .build(); - - client = new XapiClient(webClientBuilder); - } + @Autowired + private XapiClient client; public static void main(String[] args) { SpringApplication.run(GetActivityProfilesApplication.class, args).close(); diff --git a/samples/get-activity-profiles/src/main/resources/application.properties b/samples/get-activity-profiles/src/main/resources/application.properties new file mode 100644 index 00000000..de20217a --- /dev/null +++ b/samples/get-activity-profiles/src/main/resources/application.properties @@ -0,0 +1,3 @@ +xapi.client.username = admin +xapi.client.password = password +xapi.client.baseUrl = https://example.com/xapi/ diff --git a/samples/get-activity/src/main/java/dev/learning/xapi/samples/getactivity/GetActivityApplication.java b/samples/get-activity/src/main/java/dev/learning/xapi/samples/getactivity/GetActivityApplication.java index e286739c..2a88c2a0 100644 --- a/samples/get-activity/src/main/java/dev/learning/xapi/samples/getactivity/GetActivityApplication.java +++ b/samples/get-activity/src/main/java/dev/learning/xapi/samples/getactivity/GetActivityApplication.java @@ -10,11 +10,11 @@ import java.net.URI; import java.util.Locale; import java.util.UUID; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.http.ResponseEntity; -import org.springframework.web.reactive.function.client.WebClient; /** * Sample using xAPI client to get an activity. @@ -24,25 +24,11 @@ @SpringBootApplication public class GetActivityApplication implements CommandLineRunner { - private final XapiClient client; - /** - * Constructor for application. In this sample the WebClient.Builder instance is injected by the - * Spring Framework. + * Default xAPI client. Properties are picked automatically from application.properties. */ - public GetActivityApplication(WebClient.Builder webClientBuilder) { - - webClientBuilder - // Change for the URL of your LRS - .baseUrl("https://example.com/xapi/") - // Set the Authorization value - .defaultHeader("Authorization", "") - - .build(); - - - client = new XapiClient(webClientBuilder); - } + @Autowired + private XapiClient client; public static void main(String[] args) { SpringApplication.run(GetActivityApplication.class, args).close(); diff --git a/samples/get-activity/src/main/resources/application.properties b/samples/get-activity/src/main/resources/application.properties new file mode 100644 index 00000000..de20217a --- /dev/null +++ b/samples/get-activity/src/main/resources/application.properties @@ -0,0 +1,3 @@ +xapi.client.username = admin +xapi.client.password = password +xapi.client.baseUrl = https://example.com/xapi/ diff --git a/samples/get-agent-profile/src/main/java/dev/learning/xapi/samples/getagentprofile/GetAgentProfileApplication.java b/samples/get-agent-profile/src/main/java/dev/learning/xapi/samples/getagentprofile/GetAgentProfileApplication.java index 680aa707..d66d5a09 100644 --- a/samples/get-agent-profile/src/main/java/dev/learning/xapi/samples/getagentprofile/GetAgentProfileApplication.java +++ b/samples/get-agent-profile/src/main/java/dev/learning/xapi/samples/getagentprofile/GetAgentProfileApplication.java @@ -7,11 +7,11 @@ import dev.learning.xapi.client.XapiClient; import dev.learning.xapi.samples.core.ExampleState; import java.time.Instant; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.http.ResponseEntity; -import org.springframework.web.reactive.function.client.WebClient; /** * Sample using xAPI client to get a agent profile. @@ -21,24 +21,11 @@ @SpringBootApplication public class GetAgentProfileApplication implements CommandLineRunner { - private final XapiClient client; - /** - * Constructor for application. In this sample the WebClient.Builder instance is injected by the - * Spring Framework. + * Default xAPI client. Properties are picked automatically from application.properties. */ - public GetAgentProfileApplication(WebClient.Builder webClientBuilder) { - - webClientBuilder - // Change for the URL of your LRS - .baseUrl("https://example.com/xapi/") - // Set the Authorization value - .defaultHeader("Authorization", "") - - .build(); - - client = new XapiClient(webClientBuilder); - } + @Autowired + private XapiClient client; public static void main(String[] args) { SpringApplication.run(GetAgentProfileApplication.class, args).close(); diff --git a/samples/get-agent-profile/src/main/resources/application.properties b/samples/get-agent-profile/src/main/resources/application.properties new file mode 100644 index 00000000..de20217a --- /dev/null +++ b/samples/get-agent-profile/src/main/resources/application.properties @@ -0,0 +1,3 @@ +xapi.client.username = admin +xapi.client.password = password +xapi.client.baseUrl = https://example.com/xapi/ diff --git a/samples/get-agent-profiles/src/main/java/dev/learning/xapi/samples/getagentprofiles/GetAgentProfilesApplication.java b/samples/get-agent-profiles/src/main/java/dev/learning/xapi/samples/getagentprofiles/GetAgentProfilesApplication.java index 42f53b03..a5dc1005 100644 --- a/samples/get-agent-profiles/src/main/java/dev/learning/xapi/samples/getagentprofiles/GetAgentProfilesApplication.java +++ b/samples/get-agent-profiles/src/main/java/dev/learning/xapi/samples/getagentprofiles/GetAgentProfilesApplication.java @@ -6,11 +6,11 @@ import dev.learning.xapi.client.XapiClient; import java.util.List; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.http.ResponseEntity; -import org.springframework.web.reactive.function.client.WebClient; /** * Sample using xAPI client to get a agent profile. @@ -20,24 +20,11 @@ @SpringBootApplication public class GetAgentProfilesApplication implements CommandLineRunner { - private final XapiClient client; - /** - * Constructor for application. In this sample the WebClient.Builder instance is injected by the - * Spring Framework. + * Default xAPI client. Properties are picked automatically from application.properties. */ - public GetAgentProfilesApplication(WebClient.Builder webClientBuilder) { - - webClientBuilder - // Change for the URL of your LRS - .baseUrl("https://example.com/xapi/") - // Set the Authorization value - .defaultHeader("Authorization", "") - - .build(); - - client = new XapiClient(webClientBuilder); - } + @Autowired + private XapiClient client; public static void main(String[] args) { SpringApplication.run(GetAgentProfilesApplication.class, args).close(); diff --git a/samples/get-agent-profiles/src/main/resources/application.properties b/samples/get-agent-profiles/src/main/resources/application.properties new file mode 100644 index 00000000..de20217a --- /dev/null +++ b/samples/get-agent-profiles/src/main/resources/application.properties @@ -0,0 +1,3 @@ +xapi.client.username = admin +xapi.client.password = password +xapi.client.baseUrl = https://example.com/xapi/ diff --git a/samples/get-agents/src/main/java/dev/learning/xapi/samples/getagents/GetAgentsApplication.java b/samples/get-agents/src/main/java/dev/learning/xapi/samples/getagents/GetAgentsApplication.java index 1ea23bfc..fa42db31 100644 --- a/samples/get-agents/src/main/java/dev/learning/xapi/samples/getagents/GetAgentsApplication.java +++ b/samples/get-agents/src/main/java/dev/learning/xapi/samples/getagents/GetAgentsApplication.java @@ -9,11 +9,11 @@ import dev.learning.xapi.model.Verb; import java.util.Locale; import java.util.UUID; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.http.ResponseEntity; -import org.springframework.web.reactive.function.client.WebClient; /** * Sample using xAPI client to get agents. @@ -23,25 +23,11 @@ @SpringBootApplication public class GetAgentsApplication implements CommandLineRunner { - private final XapiClient client; - /** - * Constructor for application. In this sample the WebClient.Builder instance is injected by the - * Spring Framework. + * Default xAPI client. Properties are picked automatically from application.properties. */ - public GetAgentsApplication(WebClient.Builder webClientBuilder) { - - webClientBuilder - // Change for the URL of your LRS - .baseUrl("https://example.com/xapi/") - // Set the Authorization value - .defaultHeader("Authorization", "") - - .build(); - - - client = new XapiClient(webClientBuilder); - } + @Autowired + private XapiClient client; public static void main(String[] args) { SpringApplication.run(GetAgentsApplication.class, args).close(); diff --git a/samples/get-agents/src/main/resources/application.properties b/samples/get-agents/src/main/resources/application.properties new file mode 100644 index 00000000..de20217a --- /dev/null +++ b/samples/get-agents/src/main/resources/application.properties @@ -0,0 +1,3 @@ +xapi.client.username = admin +xapi.client.password = password +xapi.client.baseUrl = https://example.com/xapi/ diff --git a/samples/get-more-statements/src/main/java/dev/learning/xapi/samples/getmorestatements/GetMoreStatementsApplication.java b/samples/get-more-statements/src/main/java/dev/learning/xapi/samples/getmorestatements/GetMoreStatementsApplication.java index 87306915..87b1fddb 100644 --- a/samples/get-more-statements/src/main/java/dev/learning/xapi/samples/getmorestatements/GetMoreStatementsApplication.java +++ b/samples/get-more-statements/src/main/java/dev/learning/xapi/samples/getmorestatements/GetMoreStatementsApplication.java @@ -7,11 +7,11 @@ import dev.learning.xapi.client.XapiClient; import dev.learning.xapi.model.StatementResult; import java.util.Arrays; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.http.ResponseEntity; -import org.springframework.web.reactive.function.client.WebClient; /** * Sample using xAPI client to get more multiple statements. @@ -21,25 +21,11 @@ @SpringBootApplication public class GetMoreStatementsApplication implements CommandLineRunner { - private final XapiClient client; - /** - * Constructor for application. In this sample the WebClient.Builder instance is injected by the - * Spring Framework. + * Default xAPI client. Properties are picked automatically from application.properties. */ - public GetMoreStatementsApplication(WebClient.Builder webClientBuilder) { - - webClientBuilder - // Change for the URL of your LRS - .baseUrl("https://example.com/xapi/") - // Set the Authorization value - .defaultHeader("Authorization", "") - - .build(); - - - client = new XapiClient(webClientBuilder); - } + @Autowired + private XapiClient client; public static void main(String[] args) { SpringApplication.run(GetMoreStatementsApplication.class, args).close(); diff --git a/samples/get-more-statements/src/main/resources/application.properties b/samples/get-more-statements/src/main/resources/application.properties new file mode 100644 index 00000000..de20217a --- /dev/null +++ b/samples/get-more-statements/src/main/resources/application.properties @@ -0,0 +1,3 @@ +xapi.client.username = admin +xapi.client.password = password +xapi.client.baseUrl = https://example.com/xapi/ diff --git a/samples/get-state/src/main/java/dev/learning/xapi/samples/getstate/GetStateApplication.java b/samples/get-state/src/main/java/dev/learning/xapi/samples/getstate/GetStateApplication.java index 53a81a01..a5b57372 100644 --- a/samples/get-state/src/main/java/dev/learning/xapi/samples/getstate/GetStateApplication.java +++ b/samples/get-state/src/main/java/dev/learning/xapi/samples/getstate/GetStateApplication.java @@ -7,11 +7,11 @@ import dev.learning.xapi.client.XapiClient; import dev.learning.xapi.samples.core.ExampleState; import java.time.Instant; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.http.ResponseEntity; -import org.springframework.web.reactive.function.client.WebClient; /** * Sample using xAPI client to get a state. @@ -21,25 +21,11 @@ @SpringBootApplication public class GetStateApplication implements CommandLineRunner { - private final XapiClient client; - /** - * Constructor for application. In this sample the WebClient.Builder instance is injected by the - * Spring Framework. + * Default xAPI client. Properties are picked automatically from application.properties. */ - public GetStateApplication(WebClient.Builder webClientBuilder) { - - webClientBuilder - // Change for the URL of your LRS - .baseUrl("https://example.com/xapi/") - // Set the Authorization value - .defaultHeader("Authorization", "") - - .build(); - - - client = new XapiClient(webClientBuilder); - } + @Autowired + private XapiClient client; public static void main(String[] args) { SpringApplication.run(GetStateApplication.class, args).close(); diff --git a/samples/get-state/src/main/resources/application.properties b/samples/get-state/src/main/resources/application.properties new file mode 100644 index 00000000..de20217a --- /dev/null +++ b/samples/get-state/src/main/resources/application.properties @@ -0,0 +1,3 @@ +xapi.client.username = admin +xapi.client.password = password +xapi.client.baseUrl = https://example.com/xapi/ diff --git a/samples/get-statement/src/main/java/dev/learning/xapi/samples/getstatement/GetStatementApplication.java b/samples/get-statement/src/main/java/dev/learning/xapi/samples/getstatement/GetStatementApplication.java index 66a4a4ba..392a33eb 100644 --- a/samples/get-statement/src/main/java/dev/learning/xapi/samples/getstatement/GetStatementApplication.java +++ b/samples/get-statement/src/main/java/dev/learning/xapi/samples/getstatement/GetStatementApplication.java @@ -9,11 +9,11 @@ import dev.learning.xapi.model.Verb; import java.util.Locale; import java.util.UUID; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.http.ResponseEntity; -import org.springframework.web.reactive.function.client.WebClient; /** * Sample using xAPI client to get a statement. @@ -23,25 +23,11 @@ @SpringBootApplication public class GetStatementApplication implements CommandLineRunner { - private final XapiClient client; - /** - * Constructor for application. In this sample the WebClient.Builder instance is injected by the - * Spring Framework. + * Default xAPI client. Properties are picked automatically from application.properties. */ - public GetStatementApplication(WebClient.Builder webClientBuilder) { - - webClientBuilder - // Change for the URL of your LRS - .baseUrl("https://example.com/xapi/") - // Set the Authorization value - .defaultHeader("Authorization", "") - - .build(); - - - client = new XapiClient(webClientBuilder); - } + @Autowired + private XapiClient client; public static void main(String[] args) { SpringApplication.run(GetStatementApplication.class, args).close(); diff --git a/samples/get-statement/src/main/resources/application.properties b/samples/get-statement/src/main/resources/application.properties new file mode 100644 index 00000000..de20217a --- /dev/null +++ b/samples/get-statement/src/main/resources/application.properties @@ -0,0 +1,3 @@ +xapi.client.username = admin +xapi.client.password = password +xapi.client.baseUrl = https://example.com/xapi/ diff --git a/samples/get-statements/src/main/java/dev/learning/xapi/samples/getstatements/GetStatementsApplication.java b/samples/get-statements/src/main/java/dev/learning/xapi/samples/getstatements/GetStatementsApplication.java index 507919cd..e04a6de7 100644 --- a/samples/get-statements/src/main/java/dev/learning/xapi/samples/getstatements/GetStatementsApplication.java +++ b/samples/get-statements/src/main/java/dev/learning/xapi/samples/getstatements/GetStatementsApplication.java @@ -8,11 +8,11 @@ import dev.learning.xapi.model.StatementResult; import dev.learning.xapi.model.Verb; import java.util.Arrays; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.http.ResponseEntity; -import org.springframework.web.reactive.function.client.WebClient; /** * Sample using xAPI client to get multiple statements. @@ -22,25 +22,11 @@ @SpringBootApplication public class GetStatementsApplication implements CommandLineRunner { - private final XapiClient client; - /** - * Constructor for application. In this sample the WebClient.Builder instance is injected by the - * Spring Framework. + * Default xAPI client. Properties are picked automatically from application.properties. */ - public GetStatementsApplication(WebClient.Builder webClientBuilder) { - - webClientBuilder - // Change for the URL of your LRS - .baseUrl("https://example.com/xapi/") - // Set the Authorization value - .defaultHeader("Authorization", "") - - .build(); - - - client = new XapiClient(webClientBuilder); - } + @Autowired + private XapiClient client; public static void main(String[] args) { SpringApplication.run(GetStatementsApplication.class, args).close(); diff --git a/samples/get-statements/src/main/resources/application.properties b/samples/get-statements/src/main/resources/application.properties new file mode 100644 index 00000000..de20217a --- /dev/null +++ b/samples/get-statements/src/main/resources/application.properties @@ -0,0 +1,3 @@ +xapi.client.username = admin +xapi.client.password = password +xapi.client.baseUrl = https://example.com/xapi/ diff --git a/samples/get-states/src/main/java/dev/learning/xapi/samples/getstates/GetStatesApplication.java b/samples/get-states/src/main/java/dev/learning/xapi/samples/getstates/GetStatesApplication.java index eeea9379..7a3b6dba 100644 --- a/samples/get-states/src/main/java/dev/learning/xapi/samples/getstates/GetStatesApplication.java +++ b/samples/get-states/src/main/java/dev/learning/xapi/samples/getstates/GetStatesApplication.java @@ -8,11 +8,11 @@ import dev.learning.xapi.samples.core.ExampleState; import java.time.Instant; import java.util.List; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.http.ResponseEntity; -import org.springframework.web.reactive.function.client.WebClient; /** * Sample using xAPI client to get states. @@ -22,25 +22,11 @@ @SpringBootApplication public class GetStatesApplication implements CommandLineRunner { - private final XapiClient client; - /** - * Constructor for application. In this sample the WebClient.Builder instance is injected by the - * Spring Framework. + * Default xAPI client. Properties are picked automatically from application.properties. */ - public GetStatesApplication(WebClient.Builder webClientBuilder) { - - webClientBuilder - // Change for the URL of your LRS - .baseUrl("https://example.com/xapi/") - // Set the Authorization value - .defaultHeader("Authorization", "") - - .build(); - - - client = new XapiClient(webClientBuilder); - } + @Autowired + private XapiClient client; public static void main(String[] args) { SpringApplication.run(GetStatesApplication.class, args).close(); diff --git a/samples/get-states/src/main/resources/application.properties b/samples/get-states/src/main/resources/application.properties new file mode 100644 index 00000000..de20217a --- /dev/null +++ b/samples/get-states/src/main/resources/application.properties @@ -0,0 +1,3 @@ +xapi.client.username = admin +xapi.client.password = password +xapi.client.baseUrl = https://example.com/xapi/ diff --git a/samples/get-voided-statement/src/main/java/dev/learning/xapi/samples/getvoidedstatement/GetVoidedStatementApplication.java b/samples/get-voided-statement/src/main/java/dev/learning/xapi/samples/getvoidedstatement/GetVoidedStatementApplication.java index 00b19f0a..e9cb54b2 100644 --- a/samples/get-voided-statement/src/main/java/dev/learning/xapi/samples/getvoidedstatement/GetVoidedStatementApplication.java +++ b/samples/get-voided-statement/src/main/java/dev/learning/xapi/samples/getvoidedstatement/GetVoidedStatementApplication.java @@ -9,11 +9,11 @@ import dev.learning.xapi.model.Verb; import java.util.Locale; import java.util.UUID; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.http.ResponseEntity; -import org.springframework.web.reactive.function.client.WebClient; /** * Sample using xAPI client to get a voided statement. @@ -23,25 +23,11 @@ @SpringBootApplication public class GetVoidedStatementApplication implements CommandLineRunner { - private final XapiClient client; - /** - * Constructor for application. In this sample the WebClient.Builder instance is injected by the - * Spring Framework. + * Default xAPI client. Properties are picked automatically from application.properties. */ - public GetVoidedStatementApplication(WebClient.Builder webClientBuilder) { - - webClientBuilder - // Change for the URL of your LRS - .baseUrl("https://example.com/xapi/") - // Set the Authorization value - .defaultHeader("Authorization", "") - - .build(); - - - client = new XapiClient(webClientBuilder); - } + @Autowired + private XapiClient client; public static void main(String[] args) { SpringApplication.run(GetVoidedStatementApplication.class, args).close(); diff --git a/samples/get-voided-statement/src/main/resources/application.properties b/samples/get-voided-statement/src/main/resources/application.properties new file mode 100644 index 00000000..de20217a --- /dev/null +++ b/samples/get-voided-statement/src/main/resources/application.properties @@ -0,0 +1,3 @@ +xapi.client.username = admin +xapi.client.password = password +xapi.client.baseUrl = https://example.com/xapi/ diff --git a/samples/post-activity-profile/src/main/java/dev/learning/xapi/samples/postactivityprofile/PostActivityProfileApplication.java b/samples/post-activity-profile/src/main/java/dev/learning/xapi/samples/postactivityprofile/PostActivityProfileApplication.java index c5f01176..a3953064 100644 --- a/samples/post-activity-profile/src/main/java/dev/learning/xapi/samples/postactivityprofile/PostActivityProfileApplication.java +++ b/samples/post-activity-profile/src/main/java/dev/learning/xapi/samples/postactivityprofile/PostActivityProfileApplication.java @@ -7,10 +7,10 @@ import dev.learning.xapi.client.XapiClient; import dev.learning.xapi.samples.core.ExampleState; import java.time.Instant; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.web.reactive.function.client.WebClient; /** * Sample using xAPI client to post an activity profile. @@ -20,24 +20,11 @@ @SpringBootApplication public class PostActivityProfileApplication implements CommandLineRunner { - private final XapiClient client; - /** - * Constructor for application. In this sample the WebClient.Builder instance is injected by the - * Spring Framework. + * Default xAPI client. Properties are picked automatically from application.properties. */ - public PostActivityProfileApplication(WebClient.Builder webClientBuilder) { - - webClientBuilder - // Change for the URL of your LRS - .baseUrl("https://example.com/xapi/") - // Set the Authorization value - .defaultHeader("Authorization", "") - - .build(); - - client = new XapiClient(webClientBuilder); - } + @Autowired + private XapiClient client; public static void main(String[] args) { SpringApplication.run(PostActivityProfileApplication.class, args).close(); diff --git a/samples/post-activity-profile/src/main/resources/application.properties b/samples/post-activity-profile/src/main/resources/application.properties new file mode 100644 index 00000000..de20217a --- /dev/null +++ b/samples/post-activity-profile/src/main/resources/application.properties @@ -0,0 +1,3 @@ +xapi.client.username = admin +xapi.client.password = password +xapi.client.baseUrl = https://example.com/xapi/ diff --git a/samples/post-agent-profile/src/main/java/dev/learning/xapi/samples/postagentprofile/PostAgentProfileApplication.java b/samples/post-agent-profile/src/main/java/dev/learning/xapi/samples/postagentprofile/PostAgentProfileApplication.java index 1a51d47b..a7a6cc2e 100644 --- a/samples/post-agent-profile/src/main/java/dev/learning/xapi/samples/postagentprofile/PostAgentProfileApplication.java +++ b/samples/post-agent-profile/src/main/java/dev/learning/xapi/samples/postagentprofile/PostAgentProfileApplication.java @@ -7,10 +7,10 @@ import dev.learning.xapi.client.XapiClient; import dev.learning.xapi.samples.core.ExampleState; import java.time.Instant; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.web.reactive.function.client.WebClient; /** * Sample using xAPI client to post an agent profile. @@ -20,25 +20,11 @@ @SpringBootApplication public class PostAgentProfileApplication implements CommandLineRunner { - private final XapiClient client; - /** - * Constructor for application. In this sample the WebClient.Builder instance is injected by the - * Spring Framework. + * Default xAPI client. Properties are picked automatically from application.properties. */ - public PostAgentProfileApplication(WebClient.Builder webClientBuilder) { - - webClientBuilder - // Change for the URL of your LRS - .baseUrl("https://example.com/xapi/") - // Set the Authorization value - .defaultHeader("Authorization", "") - - .build(); - - - client = new XapiClient(webClientBuilder); - } + @Autowired + private XapiClient client; public static void main(String[] args) { SpringApplication.run(PostAgentProfileApplication.class, args).close(); diff --git a/samples/post-agent-profile/src/main/resources/application.properties b/samples/post-agent-profile/src/main/resources/application.properties new file mode 100644 index 00000000..de20217a --- /dev/null +++ b/samples/post-agent-profile/src/main/resources/application.properties @@ -0,0 +1,3 @@ +xapi.client.username = admin +xapi.client.password = password +xapi.client.baseUrl = https://example.com/xapi/ diff --git a/samples/post-state/src/main/java/dev/learning/xapi/samples/poststate/PostStateApplication.java b/samples/post-state/src/main/java/dev/learning/xapi/samples/poststate/PostStateApplication.java index 8b24c0be..8ec13ef9 100644 --- a/samples/post-state/src/main/java/dev/learning/xapi/samples/poststate/PostStateApplication.java +++ b/samples/post-state/src/main/java/dev/learning/xapi/samples/poststate/PostStateApplication.java @@ -7,10 +7,10 @@ import dev.learning.xapi.client.XapiClient; import dev.learning.xapi.samples.core.ExampleState; import java.time.Instant; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.web.reactive.function.client.WebClient; /** * Sample using xAPI client to post a state. @@ -20,25 +20,11 @@ @SpringBootApplication public class PostStateApplication implements CommandLineRunner { - private final XapiClient client; - /** - * Constructor for application. In this sample the WebClient.Builder instance is injected by the - * Spring Framework. + * Default xAPI client. Properties are picked automatically from application.properties. */ - public PostStateApplication(WebClient.Builder webClientBuilder) { - - webClientBuilder - // Change for the URL of your LRS - .baseUrl("https://example.com/xapi/") - // Set the Authorization value - .defaultHeader("Authorization", "") - - .build(); - - - client = new XapiClient(webClientBuilder); - } + @Autowired + private XapiClient client; public static void main(String[] args) { SpringApplication.run(PostStateApplication.class, args).close(); diff --git a/samples/post-state/src/main/resources/application.properties b/samples/post-state/src/main/resources/application.properties new file mode 100644 index 00000000..de20217a --- /dev/null +++ b/samples/post-state/src/main/resources/application.properties @@ -0,0 +1,3 @@ +xapi.client.username = admin +xapi.client.password = password +xapi.client.baseUrl = https://example.com/xapi/ diff --git a/samples/post-statement/src/main/java/dev/learning/xapi/samples/poststatement/PostStatementApplication.java b/samples/post-statement/src/main/java/dev/learning/xapi/samples/poststatement/PostStatementApplication.java index 161b01dd..9ee12747 100644 --- a/samples/post-statement/src/main/java/dev/learning/xapi/samples/poststatement/PostStatementApplication.java +++ b/samples/post-statement/src/main/java/dev/learning/xapi/samples/poststatement/PostStatementApplication.java @@ -8,11 +8,11 @@ import dev.learning.xapi.model.Verb; import java.util.Locale; import java.util.UUID; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.http.ResponseEntity; -import org.springframework.web.reactive.function.client.WebClient; /** * Sample using xAPI client to post a statement. @@ -22,24 +22,11 @@ @SpringBootApplication public class PostStatementApplication implements CommandLineRunner { - private final XapiClient client; - /** - * Constructor for application. In this sample the WebClient.Builder instance is injected by the - * Spring Framework. + * Default xAPI client. Properties are picked automatically from application.properties. */ - public PostStatementApplication(WebClient.Builder webClientBuilder) { - - webClientBuilder - // Change for the URL of your LRS - .baseUrl("https://example.com/xapi/") - // Set the Authorization value - .defaultHeader("Authorization", "") - - .build(); - - client = new XapiClient(webClientBuilder); - } + @Autowired + private XapiClient client; public static void main(String[] args) { SpringApplication.run(PostStatementApplication.class, args).close(); diff --git a/samples/post-statement/src/main/resources/application.properties b/samples/post-statement/src/main/resources/application.properties new file mode 100644 index 00000000..de20217a --- /dev/null +++ b/samples/post-statement/src/main/resources/application.properties @@ -0,0 +1,3 @@ +xapi.client.username = admin +xapi.client.password = password +xapi.client.baseUrl = https://example.com/xapi/ diff --git a/samples/post-statements/src/main/java/dev/learning/xapi/samples/poststatements/PostStatementsApplication.java b/samples/post-statements/src/main/java/dev/learning/xapi/samples/poststatements/PostStatementsApplication.java index 2b6c717f..f94615ba 100644 --- a/samples/post-statements/src/main/java/dev/learning/xapi/samples/poststatements/PostStatementsApplication.java +++ b/samples/post-statements/src/main/java/dev/learning/xapi/samples/poststatements/PostStatementsApplication.java @@ -8,10 +8,10 @@ import dev.learning.xapi.model.Statement; import dev.learning.xapi.model.Verb; import java.util.Locale; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.web.reactive.function.client.WebClient; /** * Sample using xAPI client to post multiple statements. @@ -21,24 +21,11 @@ @SpringBootApplication public class PostStatementsApplication implements CommandLineRunner { - private final XapiClient client; - /** - * Constructor for application. In this sample the WebClient.Builder instance is injected by the - * Spring Framework. + * Default xAPI client. Properties are picked automatically from application.properties. */ - public PostStatementsApplication(WebClient.Builder webClientBuilder) { - - webClientBuilder - // Change for the URL of your LRS - .baseUrl("https://example.com/xapi/") - // Set the Authorization value - .defaultHeader("Authorization", "") - - .build(); - - client = new XapiClient(webClientBuilder); - } + @Autowired + private XapiClient client; public static void main(String[] args) { SpringApplication.run(PostStatementsApplication.class, args).close(); diff --git a/samples/post-statements/src/main/resources/application.properties b/samples/post-statements/src/main/resources/application.properties new file mode 100644 index 00000000..de20217a --- /dev/null +++ b/samples/post-statements/src/main/resources/application.properties @@ -0,0 +1,3 @@ +xapi.client.username = admin +xapi.client.password = password +xapi.client.baseUrl = https://example.com/xapi/ diff --git a/samples/put-activity-profile/src/main/java/dev/learning/xapi/samples/putactivityprofile/PutActivityProfileApplication.java b/samples/put-activity-profile/src/main/java/dev/learning/xapi/samples/putactivityprofile/PutActivityProfileApplication.java index 8c3850b6..1fc185e0 100644 --- a/samples/put-activity-profile/src/main/java/dev/learning/xapi/samples/putactivityprofile/PutActivityProfileApplication.java +++ b/samples/put-activity-profile/src/main/java/dev/learning/xapi/samples/putactivityprofile/PutActivityProfileApplication.java @@ -7,10 +7,10 @@ import dev.learning.xapi.client.XapiClient; import dev.learning.xapi.samples.core.ExampleState; import java.time.Instant; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.web.reactive.function.client.WebClient; /** * Sample using xAPI client to put an activity profile. @@ -20,24 +20,11 @@ @SpringBootApplication public class PutActivityProfileApplication implements CommandLineRunner { - private final XapiClient client; - /** - * Constructor for application. In this sample the WebClient.Builder instance is injected by the - * Spring Framework. + * Default xAPI client. Properties are picked automatically from application.properties. */ - public PutActivityProfileApplication(WebClient.Builder webClientBuilder) { - - webClientBuilder - // Change for the URL of your LRS - .baseUrl("https://example.com/xapi/") - // Set the Authorization value - .defaultHeader("Authorization", "") - - .build(); - - client = new XapiClient(webClientBuilder); - } + @Autowired + private XapiClient client; public static void main(String[] args) { SpringApplication.run(PutActivityProfileApplication.class, args).close(); diff --git a/samples/put-activity-profile/src/main/resources/application.properties b/samples/put-activity-profile/src/main/resources/application.properties new file mode 100644 index 00000000..de20217a --- /dev/null +++ b/samples/put-activity-profile/src/main/resources/application.properties @@ -0,0 +1,3 @@ +xapi.client.username = admin +xapi.client.password = password +xapi.client.baseUrl = https://example.com/xapi/ diff --git a/samples/put-agent-profile/src/main/java/dev/learning/xapi/samples/putagentprofile/PutAgentProfileApplication.java b/samples/put-agent-profile/src/main/java/dev/learning/xapi/samples/putagentprofile/PutAgentProfileApplication.java index 524f70ab..7e435046 100644 --- a/samples/put-agent-profile/src/main/java/dev/learning/xapi/samples/putagentprofile/PutAgentProfileApplication.java +++ b/samples/put-agent-profile/src/main/java/dev/learning/xapi/samples/putagentprofile/PutAgentProfileApplication.java @@ -7,10 +7,10 @@ import dev.learning.xapi.client.XapiClient; import dev.learning.xapi.samples.core.ExampleState; import java.time.Instant; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.web.reactive.function.client.WebClient; /** * Sample using xAPI client to put an agent profile. @@ -20,25 +20,11 @@ @SpringBootApplication public class PutAgentProfileApplication implements CommandLineRunner { - private final XapiClient client; - /** - * Constructor for application. In this sample the WebClient.Builder instance is injected by the - * Spring Framework. + * Default xAPI client. Properties are picked automatically from application.properties. */ - public PutAgentProfileApplication(WebClient.Builder webClientBuilder) { - - webClientBuilder - // Change for the URL of your LRS - .baseUrl("https://example.com/xapi/") - // Set the Authorization value - .defaultHeader("Authorization", "") - - .build(); - - - client = new XapiClient(webClientBuilder); - } + @Autowired + private XapiClient client; public static void main(String[] args) { SpringApplication.run(PutAgentProfileApplication.class, args).close(); diff --git a/samples/put-agent-profile/src/main/resources/application.properties b/samples/put-agent-profile/src/main/resources/application.properties new file mode 100644 index 00000000..de20217a --- /dev/null +++ b/samples/put-agent-profile/src/main/resources/application.properties @@ -0,0 +1,3 @@ +xapi.client.username = admin +xapi.client.password = password +xapi.client.baseUrl = https://example.com/xapi/ diff --git a/samples/put-state/src/main/java/dev/learning/xapi/samples/putstate/PutStateApplication.java b/samples/put-state/src/main/java/dev/learning/xapi/samples/putstate/PutStateApplication.java index 4ec7209e..e40b0658 100644 --- a/samples/put-state/src/main/java/dev/learning/xapi/samples/putstate/PutStateApplication.java +++ b/samples/put-state/src/main/java/dev/learning/xapi/samples/putstate/PutStateApplication.java @@ -7,10 +7,10 @@ import dev.learning.xapi.client.XapiClient; import dev.learning.xapi.samples.core.ExampleState; import java.time.Instant; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.web.reactive.function.client.WebClient; /** * Sample using xAPI client to put a state. @@ -20,24 +20,11 @@ @SpringBootApplication public class PutStateApplication implements CommandLineRunner { - private final XapiClient client; - /** - * Constructor for application. In this sample the WebClient.Builder instance is injected by the - * Spring Framework. + * Default xAPI client. Properties are picked automatically from application.properties. */ - public PutStateApplication(WebClient.Builder webClientBuilder) { - - webClientBuilder - // Change for the URL of your LRS - .baseUrl("https://example.com/xapi/") - // Set the Authorization value - .defaultHeader("Authorization", "") - - .build(); - - client = new XapiClient(webClientBuilder); - } + @Autowired + private XapiClient client; public static void main(String[] args) { SpringApplication.run(PutStateApplication.class, args).close(); diff --git a/samples/put-state/src/main/resources/application.properties b/samples/put-state/src/main/resources/application.properties new file mode 100644 index 00000000..de20217a --- /dev/null +++ b/samples/put-state/src/main/resources/application.properties @@ -0,0 +1,3 @@ +xapi.client.username = admin +xapi.client.password = password +xapi.client.baseUrl = https://example.com/xapi/ diff --git a/xapi-client/src/main/java/dev/learning/xapi/client/GetStatementRequest.java b/xapi-client/src/main/java/dev/learning/xapi/client/GetStatementRequest.java index 0a284073..ec25885b 100644 --- a/xapi-client/src/main/java/dev/learning/xapi/client/GetStatementRequest.java +++ b/xapi-client/src/main/java/dev/learning/xapi/client/GetStatementRequest.java @@ -42,7 +42,7 @@ public HttpMethod getMethod() { @Override public UriBuilder url(UriBuilder uriBuilder, Map queryParams) { - return uriBuilder.path("statements") + return uriBuilder.path("/statements") .queryParam("statementId", id) diff --git a/xapi-client/src/main/java/dev/learning/xapi/client/GetStatementsRequest.java b/xapi-client/src/main/java/dev/learning/xapi/client/GetStatementsRequest.java index 4eb1a5d4..4b5bd133 100644 --- a/xapi-client/src/main/java/dev/learning/xapi/client/GetStatementsRequest.java +++ b/xapi-client/src/main/java/dev/learning/xapi/client/GetStatementsRequest.java @@ -68,7 +68,7 @@ public UriBuilder url(UriBuilder uriBuilder, Map queryParams) { // All queryParams are optional - uriBuilder.path("statements"); + uriBuilder.path("/statements"); if (agent != null) { queryParams.put("agent", agentToJsonString()); diff --git a/xapi-client/src/main/java/dev/learning/xapi/client/GetVoidedStatementRequest.java b/xapi-client/src/main/java/dev/learning/xapi/client/GetVoidedStatementRequest.java index 8164bce6..49e21ee6 100644 --- a/xapi-client/src/main/java/dev/learning/xapi/client/GetVoidedStatementRequest.java +++ b/xapi-client/src/main/java/dev/learning/xapi/client/GetVoidedStatementRequest.java @@ -29,7 +29,7 @@ public class GetVoidedStatementRequest extends GetStatementRequest { @Override public UriBuilder url(UriBuilder uriBuilder, Map queryParams) { - return uriBuilder.path("statements") + return uriBuilder.path("/statements") .queryParam("voidedStatementId", id) diff --git a/xapi-client/src/main/java/dev/learning/xapi/client/PostStatementRequest.java b/xapi-client/src/main/java/dev/learning/xapi/client/PostStatementRequest.java index 864e66ec..1cafb1b6 100644 --- a/xapi-client/src/main/java/dev/learning/xapi/client/PostStatementRequest.java +++ b/xapi-client/src/main/java/dev/learning/xapi/client/PostStatementRequest.java @@ -35,7 +35,7 @@ public HttpMethod getMethod() { @Override public UriBuilder url(UriBuilder uriBuilder, Map queryParams) { - return uriBuilder.path("statements"); + return uriBuilder.path("/statements"); } diff --git a/xapi-client/src/main/java/dev/learning/xapi/client/PostStatementsRequest.java b/xapi-client/src/main/java/dev/learning/xapi/client/PostStatementsRequest.java index 054241da..8e9751d9 100644 --- a/xapi-client/src/main/java/dev/learning/xapi/client/PostStatementsRequest.java +++ b/xapi-client/src/main/java/dev/learning/xapi/client/PostStatementsRequest.java @@ -36,7 +36,7 @@ public HttpMethod getMethod() { @Override public UriBuilder url(UriBuilder uriBuilder, Map queryParams) { - return uriBuilder.path("statements"); + return uriBuilder.path("/statements"); } diff --git a/xapi-client/src/main/java/dev/learning/xapi/client/StatesRequest.java b/xapi-client/src/main/java/dev/learning/xapi/client/StatesRequest.java index cbe165a4..07fee652 100644 --- a/xapi-client/src/main/java/dev/learning/xapi/client/StatesRequest.java +++ b/xapi-client/src/main/java/dev/learning/xapi/client/StatesRequest.java @@ -55,7 +55,7 @@ public UriBuilder url(UriBuilder uriBuilder, Map queryParams) { queryParams.put("activityId", activityId); queryParams.put("agent", agentToJsonString()); - return uriBuilder.path("activities/state") + return uriBuilder.path("/activities/state") .queryParam("activityId", "{activityId}") diff --git a/xapi-client/src/main/java/dev/learning/xapi/client/configuration/XapiClientAutoConfiguration.java b/xapi-client/src/main/java/dev/learning/xapi/client/configuration/XapiClientAutoConfiguration.java new file mode 100644 index 00000000..a3868b9e --- /dev/null +++ b/xapi-client/src/main/java/dev/learning/xapi/client/configuration/XapiClientAutoConfiguration.java @@ -0,0 +1,53 @@ +/* + * Copyright 2016-2023 Berry Cloud Ltd. All rights reserved. + */ + +package dev.learning.xapi.client.configuration; + +import dev.learning.xapi.client.XapiClient; +import java.util.Base64; +import java.util.List; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.HttpHeaders; +import org.springframework.web.reactive.function.client.WebClient; + +/** + * Auto-configure {@link XapiClient}. + * + * @author István Rátkai (Selindek) + */ +@Configuration +@EnableConfigurationProperties(XapiClientProperties.class) +public class XapiClientAutoConfiguration { + + /** + * Creates a default xAPI client bean. + */ + @Bean + @ConditionalOnMissingBean + public XapiClient xapiClient(XapiClientProperties properties, WebClient.Builder builder, + List configurers) { + + if (properties.getAuthorization() != null) { + builder.defaultHeader(HttpHeaders.AUTHORIZATION, properties.getAuthorization()); + + } else if (properties.getUsername() != null && properties.getPassword() != null) { + final var auth = "basic " + Base64.getEncoder() + .encodeToString((properties.getUsername() + ":" + properties.getPassword()).getBytes()); + builder.defaultHeader(HttpHeaders.AUTHORIZATION, auth); + } + + if (properties.getBaseUrl() != null) { + builder.baseUrl(properties.getBaseUrl().toString()); + } + + configurers.forEach(c -> c.accept(builder)); + + return new XapiClient(builder); + + } + +} diff --git a/xapi-client/src/main/java/dev/learning/xapi/client/configuration/XapiClientConfigurer.java b/xapi-client/src/main/java/dev/learning/xapi/client/configuration/XapiClientConfigurer.java new file mode 100644 index 00000000..eb5ef2d7 --- /dev/null +++ b/xapi-client/src/main/java/dev/learning/xapi/client/configuration/XapiClientConfigurer.java @@ -0,0 +1,24 @@ +/* + * Copyright 2016-2019 Berry Cloud Ltd. All rights reserved. + */ + +package dev.learning.xapi.client.configuration; + +import dev.learning.xapi.client.XapiClient; +import io.netty.util.internal.shaded.org.jctools.queues.MessagePassingQueue.Consumer; +import org.springframework.stereotype.Component; +import org.springframework.web.reactive.function.client.WebClient; + +/** + * Interface for {@link XapiClient} configurers. + *

+ * {@link XapiClient} is basically an extension of {@link WebClient}, so all custom configuration + * can be done via a {@link Builder} object. + *

+ * + * @author István Rátkai (Selindek) + */ +@Component +public interface XapiClientConfigurer extends Consumer { + +} diff --git a/xapi-client/src/main/java/dev/learning/xapi/client/configuration/XapiClientProperties.java b/xapi-client/src/main/java/dev/learning/xapi/client/configuration/XapiClientProperties.java new file mode 100644 index 00000000..53570525 --- /dev/null +++ b/xapi-client/src/main/java/dev/learning/xapi/client/configuration/XapiClientProperties.java @@ -0,0 +1,54 @@ +/* + * Copyright 2016-2023 Berry Cloud Ltd. All rights reserved. + */ + +package dev.learning.xapi.client.configuration; + +import java.net.URI; +import lombok.Getter; +import lombok.Setter; +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * Xapi client properties. + * + * @author István Rátkai (Selindek) + */ +@Getter +@Setter +@ConfigurationProperties(prefix = "xapi.client") +public class XapiClientProperties { + + /** + * The base URL for the xAPI client requests. + */ + private URI baseUrl; + + /** + * Username for basic authorization header. + *

+ * Used only if {@link XapiClientProperties#password} is also set, AND + * {@link XapiClientProperties#authorization} is NOT set. + *

+ */ + private String username; + + /** + * Password for basic authorization header. + *

+ * Used only if {@link XapiClientProperties#username} is also set, AND + * {@link XapiClientProperties#authorization} is NOT set. + *

+ */ + private String password; + + /** + * Authorization header. + *

+ * This property has precedence over the {@link XapiClientProperties#username} and + * {@link XapiClientProperties#password} properties. + *

+ */ + private String authorization; + +} diff --git a/xapi-client/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/xapi-client/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 00000000..51915d65 --- /dev/null +++ b/xapi-client/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ +dev.learning.xapi.client.configuration.XapiClientAutoConfiguration diff --git a/xapi-client/src/test/java/dev/learning/xapi/client/XapiClientTests.java b/xapi-client/src/test/java/dev/learning/xapi/client/XapiClientTests.java index 29642283..2300b3ee 100644 --- a/xapi-client/src/test/java/dev/learning/xapi/client/XapiClientTests.java +++ b/xapi-client/src/test/java/dev/learning/xapi/client/XapiClientTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2016rue-2023 Berry Cloud Ltd. All rights reserved. + * Copyright 2016-2023 Berry Cloud Ltd. All rights reserved. */ package dev.learning.xapi.client; @@ -30,7 +30,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; -import org.springframework.http.ResponseEntity; import org.springframework.web.reactive.function.client.WebClient; /** @@ -105,8 +104,8 @@ void whenGettingStatementThenBodyIsInstanceOfStatement() throws InterruptedExcep .addHeader("Content-Type", "application/json; charset=utf-8")); // When Getting Statement - final var response = client.getStatement(r -> r.id("4df42866-40e7-45b6-bf7c-8d5fccbdccd6")) - .block(); + final var response = + client.getStatement(r -> r.id("4df42866-40e7-45b6-bf7c-8d5fccbdccd6")).block(); // Then Body Is Instance Of Statement assertThat(response.getBody(), instanceOf(Statement.class)); @@ -153,7 +152,7 @@ void whenPostingStatementsThenMethodIsPost() throws InterruptedException { mockWebServer.enqueue(new MockResponse().setStatus("HTTP/1.1 200 OK")); - final Statement attemptedStatement = Statement.builder() + final var attemptedStatement = Statement.builder() .actor(a -> a.name("A N Other").mbox("mailto:another@example.com")) @@ -164,7 +163,7 @@ void whenPostingStatementsThenMethodIsPost() throws InterruptedException { .build(); - final Statement passedStatement = attemptedStatement.toBuilder().verb(Verb.PASSED).build(); + final var passedStatement = attemptedStatement.toBuilder().verb(Verb.PASSED).build(); final List statements = Arrays.asList(attemptedStatement, passedStatement); @@ -177,12 +176,13 @@ void whenPostingStatementsThenMethodIsPost() throws InterruptedException { assertThat(recordedRequest.getMethod(), is("POST")); } + @Test void whenPostingStatementsThenBodyIsExpected() throws InterruptedException { mockWebServer.enqueue(new MockResponse().setStatus("HTTP/1.1 200 OK")); - final Statement attemptedStatement = Statement.builder() + final var attemptedStatement = Statement.builder() .actor(a -> a.name("A N Other").mbox("mailto:another@example.com")) @@ -193,11 +193,40 @@ void whenPostingStatementsThenBodyIsExpected() throws InterruptedException { .build(); - final Statement passedStatement = attemptedStatement.toBuilder().verb(Verb.PASSED).build(); + final var passedStatement = attemptedStatement.toBuilder().verb(Verb.PASSED).build(); + + // When Posting Statements + client.postStatements(r -> r.statements(attemptedStatement, passedStatement)).block(); + + final RecordedRequest recordedRequest = mockWebServer.takeRequest(); + + // Then Body Is Expected + assertThat(recordedRequest.getBody().readUtf8(), is( + "[{\"actor\":{\"objectType\":\"Agent\",\"name\":\"A N Other\",\"mbox\":\"mailto:another@example.com\"},\"verb\":{\"id\":\"http://adlnet.gov/expapi/verbs/attempted\",\"display\":{\"und\":\"attempted\"}},\"object\":{\"objectType\":\"Activity\",\"id\":\"https://example.com/activity/simplestatement\",\"definition\":{\"name\":{\"en\":\"Simple Statement\"}}}},{\"actor\":{\"objectType\":\"Agent\",\"name\":\"A N Other\",\"mbox\":\"mailto:another@example.com\"},\"verb\":{\"id\":\"http://adlnet.gov/expapi/verbs/passed\",\"display\":{\"und\":\"passed\"}},\"object\":{\"objectType\":\"Activity\",\"id\":\"https://example.com/activity/simplestatement\",\"definition\":{\"name\":{\"en\":\"Simple Statement\"}}}}]")); + } + + + @Test + void whenPostingStatementsArrayThenBodyIsExpected() throws InterruptedException { + + mockWebServer.enqueue(new MockResponse().setStatus("HTTP/1.1 200 OK")); + + final var attemptedStatement = Statement.builder() + + .actor(a -> a.name("A N Other").mbox("mailto:another@example.com")) + + .verb(Verb.ATTEMPTED) + + .activityObject(o -> o.id("https://example.com/activity/simplestatement") + .definition(d -> d.addName(Locale.ENGLISH, "Simple Statement"))) + + .build(); + + final var passedStatement = attemptedStatement.toBuilder().verb(Verb.PASSED).build(); final List statements = Arrays.asList(attemptedStatement, passedStatement); - // When Posting Statements + // When Posting Statements Array client.postStatements(r -> r.statements(statements)).block(); final RecordedRequest recordedRequest = mockWebServer.takeRequest(); @@ -212,7 +241,7 @@ void whenPostingStatementsThenContentTypeHeaderIsApplicationJson() throws Interr mockWebServer.enqueue(new MockResponse().setStatus("HTTP/1.1 200 OK")); - final Statement attemptedStatement = Statement.builder() + final var attemptedStatement = Statement.builder() .actor(a -> a.name("A N Other").mbox("mailto:another@example.com")) @@ -223,7 +252,7 @@ void whenPostingStatementsThenContentTypeHeaderIsApplicationJson() throws Interr .build(); - final Statement passedStatement = attemptedStatement.toBuilder().verb(Verb.PASSED).build(); + final var passedStatement = attemptedStatement.toBuilder().verb(Verb.PASSED).build(); final List statements = Arrays.asList(attemptedStatement, passedStatement); @@ -244,7 +273,7 @@ void whenPostingStatementsThenResponseBodyIsInstanceOfUUIDArray() throws Interru "[\"2eb84e56-441a-492c-9d7b-f8e9ddd3e15d\",\"19a74a3f-7354-4254-aa4a-1c39ab4f2ca7\"]") .addHeader("Content-Type", "application/json")); - final Statement attemptedStatement = Statement.builder() + final var attemptedStatement = Statement.builder() .actor(a -> a.name("A N Other").mbox("mailto:another@example.com")) @@ -255,13 +284,12 @@ void whenPostingStatementsThenResponseBodyIsInstanceOfUUIDArray() throws Interru .build(); - final Statement passedStatement = attemptedStatement.toBuilder().verb(Verb.PASSED).build(); + final var passedStatement = attemptedStatement.toBuilder().verb(Verb.PASSED).build(); final List statements = Arrays.asList(attemptedStatement, passedStatement); // When Posting Statements - final ResponseEntity< - List> response = client.postStatements(r -> r.statements(statements)).block(); + final var response = client.postStatements(r -> r.statements(statements)).block(); // Then Response Body Is Instance Of UUID Array assertThat(response.getBody(), instanceOf(List.class)); @@ -664,14 +692,13 @@ void givenStateContentTypeIsTextPlainWhenGettingStateThenBodyIsInstanceOfString( .addHeader("Content-Type", "text/plain; charset=utf-8")); // When Getting State - final ResponseEntity response = client - .getState(r -> r.activityId("https://example.com/activity/1") + final var response = client.getState(r -> r.activityId("https://example.com/activity/1") - .agent(a -> a.name("A N Other").mbox("mailto:another@example.com")) + .agent(a -> a.name("A N Other").mbox("mailto:another@example.com")) - .registration("67828e3a-d116-4e18-8af3-2d2c59e27be6") + .registration("67828e3a-d116-4e18-8af3-2d2c59e27be6") - .stateId("bookmark"), String.class) + .stateId("bookmark"), String.class) .block(); @@ -688,14 +715,13 @@ void givenStateContentTypeIsTextPlainWhenGettingStateThenBodyIsExpected() .addHeader("Content-Type", "text/plain; charset=utf-8")); // When Getting State - final ResponseEntity response = client - .getState(r -> r.activityId("https://example.com/activity/1") + final var response = client.getState(r -> r.activityId("https://example.com/activity/1") - .agent(a -> a.name("A N Other").mbox("mailto:another@example.com")) + .agent(a -> a.name("A N Other").mbox("mailto:another@example.com")) - .registration("67828e3a-d116-4e18-8af3-2d2c59e27be6") + .registration("67828e3a-d116-4e18-8af3-2d2c59e27be6") - .stateId("bookmark"), String.class) + .stateId("bookmark"), String.class) .block(); @@ -1180,12 +1206,11 @@ void givenMultipleStatesExistWhenGettingMultipleStatesThenBodyIsInstanceOfString .addHeader("Content-Type", "application/json; charset=utf-8")); // When Getting Multiple States - final ResponseEntity> response = client - .getStates(r -> r.activityId("https://example.com/activity/1") + final var response = client.getStates(r -> r.activityId("https://example.com/activity/1") - .agent(a -> a.name("A N Other").mbox("mailto:another@example.com")) + .agent(a -> a.name("A N Other").mbox("mailto:another@example.com")) - .registration("67828e3a-d116-4e18-8af3-2d2c59e27be6")) + .registration("67828e3a-d116-4e18-8af3-2d2c59e27be6")) .block(); @@ -1203,17 +1228,16 @@ void givenMultipleStatesExistWhenGettingMultipleStatesThenBodyIsExpected() .addHeader("Content-Type", "application/json; charset=utf-8")); // When Getting Multiple States - final ResponseEntity> response = client - .getStates(r -> r.activityId("https://example.com/activity/1") + final var response = client.getStates(r -> r.activityId("https://example.com/activity/1") - .agent(a -> a.name("A N Other").mbox("mailto:another@example.com")) + .agent(a -> a.name("A N Other").mbox("mailto:another@example.com")) - .registration("67828e3a-d116-4e18-8af3-2d2c59e27be6")) + .registration("67828e3a-d116-4e18-8af3-2d2c59e27be6")) .block(); // Then Body Is Expected - assertThat(response.getBody(), is(Arrays.asList( "State1", "State2", "State3" ))); + assertThat(response.getBody(), is(Arrays.asList("State1", "State2", "State3"))); } // Deleting Multiple States @@ -1774,8 +1798,8 @@ void whenGettingAgentsThenBodyIsInstanceOfPerson() throws InterruptedException { .addHeader("Content-Type", "application/json; charset=utf-8")); // When Getting Agents - final var response = client.getAgents(r -> r.agent(a -> a.mbox("mailto:another@example.com"))) - .block(); + final var response = + client.getAgents(r -> r.agent(a -> a.mbox("mailto:another@example.com"))).block(); // Then Body Is Instance Of Activity assertThat(response.getBody(), instanceOf(Person.class)); @@ -1879,7 +1903,7 @@ void givenActivityProfileContentTypeIsTextPlainWhenGettingActivityProfileThenBod .addHeader("Content-Type", "text/plain; charset=utf-8")); // When Getting ActivityProfile - final ResponseEntity response = client + final var response = client .getActivityProfile(r -> r.activityId("https://example.com/activity/1") .profileId("bookmark"), String.class) @@ -1899,7 +1923,7 @@ void givenActivityProfileContentTypeIsTextPlainWhenGettingActivityProfileThenBod .addHeader("Content-Type", "text/plain; charset=utf-8")); // When Getting ActivityProfile - final ResponseEntity response = client + final var response = client .getActivityProfile(r -> r.activityId("https://example.com/activity/1") .profileId("bookmark"), String.class) diff --git a/xapi-client/src/test/java/dev/learning/xapi/client/configuration/XapiClientAutoConfigurationAuthorizationTest.java b/xapi-client/src/test/java/dev/learning/xapi/client/configuration/XapiClientAutoConfigurationAuthorizationTest.java new file mode 100644 index 00000000..b2138ce9 --- /dev/null +++ b/xapi-client/src/test/java/dev/learning/xapi/client/configuration/XapiClientAutoConfigurationAuthorizationTest.java @@ -0,0 +1,72 @@ +/* + * Copyright 2016-2023 Berry Cloud Ltd. All rights reserved. + */ +package dev.learning.xapi.client.configuration; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; + +import dev.learning.xapi.client.XapiClient; +import dev.learning.xapi.client.configuration.XapiClientAutoConfigurationAuthorizationTest.XapiTestClientConfiguration2; +import java.io.IOException; +import okhttp3.mockwebserver.MockResponse; +import okhttp3.mockwebserver.MockWebServer; +import okhttp3.mockwebserver.RecordedRequest; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.HttpHeaders; +import org.springframework.web.reactive.function.client.WebClient.Builder; + +/** + * XapiClientAutoConfigurationAuthorization Test. + * + * @author István Rátkai (Selindek) + */ +@DisplayName("XapiClientAutoConfigurationAuthorization Test") +@SpringBootTest( + classes = { XapiClientAutoConfiguration.class, WebClientAutoConfiguration.class, + XapiTestClientConfiguration2.class }, + properties = "xapi.client.authorization = bearer 1234") +class XapiClientAutoConfigurationAuthorizationTest { + + @Autowired + private XapiClient client; + + private static MockWebServer mockWebServer; + + static class XapiTestClientConfiguration2 implements XapiClientConfigurer { + + @Override + public void accept(Builder builder) { + mockWebServer = new MockWebServer(); + try { + mockWebServer.start(); + } catch (final IOException e) { + } + builder.baseUrl(mockWebServer.url("").toString()); + } + + } + + @AfterAll + static void tearDown() throws Exception { + mockWebServer.shutdown(); + } + + @Test + void whenConfiguringXapiClientThenAuthenticationIsSet() throws InterruptedException { + + // When Configuring XapiClient + mockWebServer.enqueue(new MockResponse().setStatus("HTTP/1.1 200 OK")); + client.getStatement(r -> r.id("4df42866-40e7-45b6-bf7c-8d5fccbdccd6")).block(); + final RecordedRequest recordedRequest = mockWebServer.takeRequest(); + + // Then Authorization Is Set + assertThat(recordedRequest.getHeaders().get(HttpHeaders.AUTHORIZATION), is("bearer 1234")); + } + +} diff --git a/xapi-client/src/test/java/dev/learning/xapi/client/configuration/XapiClientAutoConfigurationBaseUrlTest.java b/xapi-client/src/test/java/dev/learning/xapi/client/configuration/XapiClientAutoConfigurationBaseUrlTest.java new file mode 100644 index 00000000..f8d936c5 --- /dev/null +++ b/xapi-client/src/test/java/dev/learning/xapi/client/configuration/XapiClientAutoConfigurationBaseUrlTest.java @@ -0,0 +1,59 @@ +/* + * Copyright 2016-2023 Berry Cloud Ltd. All rights reserved. + */ +package dev.learning.xapi.client.configuration; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; + +import dev.learning.xapi.client.XapiClient; +import okhttp3.mockwebserver.MockResponse; +import okhttp3.mockwebserver.MockWebServer; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; + +/** + * XapiClientAutoConfigurationBaseUrl Test. + * + * @author István Rátkai (Selindek) + */ +@DisplayName("XapiClientAutoConfigurationBaseUrl Test") +@SpringBootTest(classes = {XapiClientAutoConfiguration.class, WebClientAutoConfiguration.class}, + properties = {"xapi.client.baseUrl = http://127.0.0.1:55123/"}) +class XapiClientAutoConfigurationBaseUrlTest { + + @Autowired + private XapiClient client; + + private static MockWebServer mockWebServer; + + @BeforeAll + static void setUp() throws Exception { + mockWebServer = new MockWebServer(); + mockWebServer.start(55123); + } + + @AfterAll + static void tearDown() throws Exception { + mockWebServer.shutdown(); + } + + @Test + void whenConfiguringXapiClientThenBaseUrlIsSet() throws InterruptedException { + + // When Configuring XapiClient + mockWebServer.enqueue(new MockResponse().setStatus("HTTP/1.1 200 OK")); + client.getStatement(r -> r.id("4df42866-40e7-45b6-bf7c-8d5fccbdccd6")).block(); + final var recordedRequest = mockWebServer.takeRequest(); + + // Then BaseUrl Is Set (Request was sent to the proper url) + assertThat(recordedRequest.getRequestUrl().toString(), + is("http://localhost:55123/statements?statementId=4df42866-40e7-45b6-bf7c-8d5fccbdccd6")); + } + +} diff --git a/xapi-client/src/test/java/dev/learning/xapi/client/configuration/XapiClientAutoConfigurationUsernamePasswordTest.java b/xapi-client/src/test/java/dev/learning/xapi/client/configuration/XapiClientAutoConfigurationUsernamePasswordTest.java new file mode 100644 index 00000000..a2942a65 --- /dev/null +++ b/xapi-client/src/test/java/dev/learning/xapi/client/configuration/XapiClientAutoConfigurationUsernamePasswordTest.java @@ -0,0 +1,74 @@ +/* + * Copyright 2016-2023 Berry Cloud Ltd. All rights reserved. + */ +package dev.learning.xapi.client.configuration; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; + +import dev.learning.xapi.client.XapiClient; +import dev.learning.xapi.client.configuration.XapiClientAutoConfigurationUsernamePasswordTest.XapiTestClientConfiguration; +import java.io.IOException; +import okhttp3.mockwebserver.MockResponse; +import okhttp3.mockwebserver.MockWebServer; +import okhttp3.mockwebserver.RecordedRequest; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.HttpHeaders; +import org.springframework.web.reactive.function.client.WebClient.Builder; + +/** + * XapiClientAutoConfigurationUsernamePassword Test. + * + * @author István Rátkai (Selindek) + */ +@DisplayName("XapiClientAutoConfigurationUsernamePassword Test") +@SpringBootTest( + classes = { XapiClientAutoConfiguration.class, WebClientAutoConfiguration.class, + XapiTestClientConfiguration.class }, + properties = { "xapi.client.username = username", "xapi.client.password = password" }) +class XapiClientAutoConfigurationUsernamePasswordTest { + + @Autowired + private XapiClient client; + + private static MockWebServer mockWebServer; + + static class XapiTestClientConfiguration implements XapiClientConfigurer { + + @Override + public void accept(Builder builder) { + mockWebServer = new MockWebServer(); + try { + mockWebServer.start(); + } catch (final IOException e) { + } + builder.baseUrl(mockWebServer.url("").toString()); + + } + + } + + @AfterAll + static void tearDown() throws Exception { + mockWebServer.shutdown(); + } + + @Test + void whenConfiguringXapiClientThenAuthenticationIsSet() throws InterruptedException { + + // When Configuring XapiClient + mockWebServer.enqueue(new MockResponse().setStatus("HTTP/1.1 200 OK")); + client.getStatement(r -> r.id("4df42866-40e7-45b6-bf7c-8d5fccbdccd6")).block(); + final RecordedRequest recordedRequest = mockWebServer.takeRequest(); + + // Then Authorization Is Set + assertThat(recordedRequest.getHeaders().get(HttpHeaders.AUTHORIZATION), + is("basic dXNlcm5hbWU6cGFzc3dvcmQ=")); + } + +}