Siesta configureTransformer for Resource with parameters

时间:2019-02-06 22:11:59

标签: swift siesta-swift

我正在尝试使用参数为端点配置转换器。

我已成功为不包含任何参数的端点配置了转换器。但是似乎当我尝试对带有参数的Resource进行相同操作时,将无法正常工作。

这是我配置变压器的方式:

var getExampleData: Resource { return resource(exampleDataEndpoint) }
configureTransformer(getExampleData) {
            try jsonDecoder.decode(ExampleDataResponse.self, from: $0.content)
}

这是我发送请求的方式:

Api.getExampleData.withParam("foo", "bar").addObserver(self).loadIfNeeded()

我得到了响应,但它从未通过变压器管道。

Response:  200 ← GET http:example.com/enpoint?foo=bar
Siesta:pipeline │ [thread ᎠᏔᎼᎼ]  └╴Response after pipeline: success: 28 bytes

编辑

测试了.withParam()方法在可以正常运行的转换上的作用,并且看来URL必须相同才能使Siesta转换器正常工作,这真是令人沮丧。

1 个答案:

答案 0 :(得分:0)

不同的查询字符串表示不同的URL,并且different URLs mean different Siesta resources

  

请注意,“ URL”包括整个URL:协议,主机,路径和查询字符串。但是,它不包含标题。不同的查询字符串?不同的资源。 http vs https?不同的资源。

因此:

class MainClass
{
    public static IEnumerable<DatabaseRow> rows = new List<DatabaseRow>
    {
        new DatabaseRow
        {
            CategoryId = 1,
            CategoryName = "Potatoes",
            CountryId = 2,
            CountryName = "Latvia",
            Offset = 2001,
            Year0 = 123,
            Year1 = 123,
            Year2 = 123,
            Year3 = 123,
            Year4 = 123
        }
    };

    public static void Main (string[] args)
    {
        var transformed = rows.Select(row => new Experiment
        {
            Category = new Category
            {
                Id = row.CategoryId,
                Name = row.CategoryName
            },
            Country = new Country
            {
                Id = row.CountryId,
                Name = row.CountryName
            },
            Datum = new List<Datum>
            {
                new Datum
                {
                  Year = row.Offset,
                  Sales = row.Year0
                },
                new Datum
                {
                  Year = row.Offset + 1,
                  Sales = row.Year1
                },
                new Datum
                {
                  Year = row.Offset + 2,
                  Sales = row.Year2
                },
                new Datum
                {
                  Year = row.Offset + 3,
                  Sales = row.Year3
                },
                new Datum
                {
                  Year = row.Offset + 4,
                  Sales = row.Year4
                }
            }
        });
    }
}

class DatabaseRow
{
    public int CategoryId { get; set; }
    public string CategoryName { get; set; }

    public int CountryId { get; set; }
    public string CountryName { get; set; }

    public int Offset { get; set; }
    public decimal Year0 { get; set; }
    public decimal Year1 { get; set; }
    public decimal Year2 { get; set; }
    public decimal Year3 { get; set; }
    public decimal Year4 { get; set; }
}

因此,当您这样做时:

let resource0 = Api.getExampleData
let resource1 = Api.getExampleData.withParam("foo", "bar”)
resource0 == resource1  // → false

…仅适用于configureTransformer(getExampleData) { ... } ,不适用于具有不同参数的任何变体。

如果要将配置应用于共享路径的所有资源,请在路径上进行匹配:

getExampleData