I am trying to consume a stream of json objects in akka-http. ( akka http version "10.0.9", akka-http-play-json version 1.10.1)
I follow examples on web but, for String I am getting:
could not find implicit value for parameter um: akka.http.scaladsl.unmarshalling.FromByteStringUnmarshaller[String]
and for my user defined Foo case class (for which I provided the json protocol):
could not find implicit value for parameter um: akka.http.scaladsl.unmarshalling.FromByteStringUnmarshaller[server.Foo]
This is the code that is simplified. I provide a EntityStreamingSupport.json() and for the Foo object a Json Format. I don't think I need one for String. If I don't put the asSourceOf and read a simple String object or a Foo case class object the code works. What am I missing?
package server
import akka.http.scaladsl.common.EntityStreamingSupport
import akka.http.scaladsl.server.{ Directives, Route }
import akka.http.scaladsl.model.StatusCodes
import de.heikoseeberger.akkahttpplayjson.PlayJsonSupport._
import play.api.libs.json._
case class Foo(bar: String)
class StreamingMarketDataUpload extends Directives {
implicit val jsonStreamingSupport = EntityStreamingSupport.json()
implicit val jsonFooFormat = Json.format[Foo]
lazy val routes: Route =
path("upload1") {
post {
entity(as[String]) { input =>
complete(StatusCodes.OK)
}
}
} ~ path("upload2") {
post {
// Compile error here
entity(asSourceOf[String]) { marks =>
complete(StatusCodes.OK)
}
}
} ~ path("upload3") {
post {
entity(as[Foo]) { input =>
complete(StatusCodes.OK)
}
}
} ~ path("upload4") {
post {
// Compile error here
entity(asSourceOf[Foo]) { marks =>
complete(StatusCodes.OK)
}
}
}
}
asSourceOf[T] tries to consume the incoming data as a Source[T, _]. As its method signature indicates, asSourceOf[T] takes an implicit FromByteStringUnmarshaller[T] parameter. The de.heikoseeberger.akkahttpplayjson.PlayJsonSupport utility doesn't provide an implementation of this unmarshaller (as of version 1.19.0), but it does provide the unmarshallers necessary for consuming a simple String or Foo. This is why your code works when it's not using asSourceOf.
The examples in the documentation use SprayJsonSupport, which is shipped as part of Akka HTTP. If you don't want to use SprayJsonSupport, you'll have to implement a FromByteStringUnmarshaller to use asSourceOf: the linked source code can give you an idea of how to do this.
Related
I've recently taken upon myself to add setter and getter methods to my class.
Since doing this, many parts of my code got broken and I'm unable to access getter methods.
Take the example below:
private loadInputs() : Input[] {
var inputs = <Input[]>this.get('inputs');
inputs.sort((a,b) => a.QuoteRef().localeCompare(b.QuoteRef()))
return( inputs || [] );
}
My input class has 2 variables,
_Project: string
_line: string
Which I access using a method QuoteRef()
public QuoteRef(): string {
return this._Project.concat('-' + this._Line.toString().padStart(3,'0'));
}
Whenever I try to access a method or a getter from my class on an item that is casted as an Input, I can see the variables (though not access them as they are private), but the prototype section doesn't contain any of the methods.
This triggers the following error in the website console:
TypeError: a.QuoteRef is not a function
What am I doing wrong?
Update
I got it to work by updating the code as follows:
inputs.sort((a,b) => {
let first = new Input(a);
let second = new Input(b);
return first.QuoteRef().localeCompare(second.QuoteRef());
});
Without seeing your complete class I can only guess, but I think that a and b in your sort are not of the type you expect. I can't see what this.get('inputs') does, but I suspect it is not returning an array with Input class objects. Hence the function cannot be found (is not a function). You could try:
inputs.sort((a,b) => {
console.log(typeof a);
console.log(typeof b);
a.QuoteRef().localeCompare(b.QuoteRef());
})
and check what the type is. Then check what your this.get actually returns.
Edit: forgot to mention that your IDE probably does not warn you because you cast the output of this.get to <Input[]>.
In the Flutter/Dart app that I am currently working on need to download large files from my servers. However, instead of storing the file in local storage what I need to do is to parse its contents and consume it one-off. I thought the best way to accomplish this was by implementing my own StreamConsumer and overriding the relvant methods. Here is what I have done thus far
import 'dart:io';
import 'dart:async';
class Accumulator extends StreamConsumer<List<int>>
{
String text = '';
#override
Future<void> addStream(Stream<List<int>> s) async
{
print('Adding');
//print(s.length);
return;
}
#override
Future<dynamic> close() async
{
print('closed');
return Future.value(text);
}
}
Future<String> fileFetch() async
{
String url = 'https://file.io/bse4moAYc7gW';
final HttpClientRequest request = await HttpClient().getUrl(Uri.parse(url));
final HttpClientResponse response = await request.close();
return await response.pipe(Accumulator());
}
Future<void> simpleFetch() async
{
String url = 'https://file.io/bse4moAYc7gW';
final HttpClientRequest request = await HttpClient().getUrl(Uri.parse(url));
final HttpClientResponse response = await request.close();
await response.pipe(File('sample.txt').openWrite());
print('Simple done!!');
}
void main() async
{
print('Starting');
await simpleFetch();
String text = await fileFetch();
print('Finished! $text');
}
When I run this in VSCode here is the output I get
Starting
Simple done!! //the contents of the file at https://file.io/bse4moAYc7gW are duly saved in the file
sample.txt
Adding //clearly addStream is being called
Instance of 'Future<int>' //I had expected to see the length of the available data here
closed //close is clearly being called BUT
Finished! //back in main()
My understanding of the underlying issues here is still rather limited. My expectation
I had thought that I would use addStream to accumulate the contents being downloaded until
There is nothing more to download at which point close would be called and the program would display exited
Why is addStream showing instance of... rather than the length of available content?
Although the VSCode debug console does display exited this happens several seconds after closed is displayed. I thought this might be an issue with having to call super.close() but not so. What am I doing wrong here?
I was going to delete this question but decided to leave it here with an answer for the benefit of anyone else trying to do something similar.
The key point to note is that the call to Accumulator.addStream does just that - it furnishes a stream to be listened to, no actual data to be read. What you do next is this
void whenData(List<int> data)
{
//you will typically get a sequence of one or more bytes here.
for(int value in data)
{
//accumulate the incoming data here
}
return;
}
function void whenDone()
{
//now that you have all the file data *accumulated* do what you like with it
}
#override
Future<void> addStream(Stream<List<int>> s) async
{
s.listen(whenData,onDone:whenDone);
//you can optionally ahandler for `onError`
}
I have this query defined, and use it succesfully in my app:
export const GET_TEAM = gql`
query($id: ID!) {
getTeam(id: $id) {
...CompleteTeam
}
}
${fragments.team}
`
But would like to use it for mocking purpose, and for that I need this representation:
getTeam(id: 3) {
id
name
isActivated
}
Is there any easy way to call gql with variables to accomplish?
There are suggestions how to do this with an instance of ApolloClient. But if possible I'd rather skip involvning the client as I will only mock the resulting data.
To follow up a bit here: The gql function returns a GraphQL Document AST (a parsed tree representation of the query). ASTs are much easier to work with than strings - at least when it gets more complicated.
For your question in the comment: Once you are in the AST space you can do all sorts of transforms. This can be done for example using the visitor patter. GraphQL.js also comes with a visit function that allows you to replace nodes. This code should serve as inspiration, no guarantees that it works ;)
function valueToNode(value) {
if (typeof value === 'string') {
return { kind: 'StringValue', value };
} else if (typeof value === 'number' && Number.isInteger(value)) {
// return integer value node
}
// all other value nodes...
}
visit(ast, {
VariableNode: {
enter(node) {
return valueToNode(variables[node.name.value]);
}
}
}
I am not sure if you should leave the AST space but as described in the comment you can use the printer as mentioned in the comment. Not sure if it prints things that are not documents.
Please, help me solve the problem.
"no-use-before-declare" in tslint.json is true. And I am not allowed to change it.
The problem is following - "variable 'foo' used before declaration" build error.
The code may be simplified to:
export class One {
toSecond() : Two {
return new Two();
}
}
export class Two {
toFirst() : One {
return new One();
}
}
Could it be hacked somehow to overcome the linter warning and have the same result. Any workaround?
You could do:
let Two_forward: typeofTwo;
export class One {
toSecond() : Two {
return new Two_forward();
}
}
export class Two {
toFirst() : One {
return new One();
}
}
// Work around https://github.com/palantir/tslint/issues/3655
type typeofTwo = typeof Two;
Two_forward = Two;
but IMO this is unreasonable compared to just suppressing the lint error with // tslint:disable-next-line:no-use-before-declare. (And it might need further changes if the strictLocalInitialization option proposed here becomes part of strict.)
This was previously filed as a bug on tslint and the resolution was that classes are not hoisted and cannot be used before declaration. The rule is correct in this case.
If I have a struct and some code that processes arrays of this struct, where would be the place to put this code?
struct Thing {
var id : String
let type : ThingType
}
things:[Thing]?
I have code to retrieve values from a server which returns an array of 100 Thing. Where should the code go?
I've currently defined it as a static function of the Struct but would like to follow a convention, if there is one.
A function that retrieves Thing instances from a server most certainly should not be a member of Thing.
It's best to make a new protocol that declares the function, like so:
protocol ThingProvider {
func fetchThings() -> [Thing]
}
and a conforming type:
class DataBaseThingProvider: ThingProvider {
init() {
// open database connection
}
deinit() {
// close database connection
}
func fetchThings() -> [Thing] {
// fetch Things from database
}
}
This way, you can change the provider you use (Firebase, Parse, SQLite, CoreData, JSON, CSV, whatever.) by just swapping out the concrete provider class you use, and all other code can stay the same.
It also makes your code much more testable, because you can easily construct a mock provider, which decouples your tests from your production backend.
class MockThingProvider: ThingProvider {
func fetchThings() -> [Thing] {
return [
Thing(id: "MockThing1", type: thingType1),
Thing(id: "MockThing2", type: thingType2),
Thing(id: "MockThing3", type: thingType3)
]
}
}